168 lines
4.3 KiB
C#
168 lines
4.3 KiB
C#
using System;
|
|
using System.Text;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class GetNumberState : IState
|
|
{
|
|
IState prevState;
|
|
const string NUMBER_KEY = "Number";
|
|
GameObject entryPanel;
|
|
Color normalColor = Color.gray;
|
|
Color highlightedColor = new Color(0.7f, 0.7f, 0.7f, 1);
|
|
|
|
string[] digits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",};
|
|
int currentDigitIndex;
|
|
|
|
|
|
Image[] slots;
|
|
|
|
private int currentSlotIndex = 0;
|
|
private int length = 11;
|
|
|
|
public GetNumberState(IState prevState)
|
|
{
|
|
this.prevState = prevState;
|
|
}
|
|
|
|
public void enter()
|
|
{
|
|
//включить панель, получить доступ ко всем слотам
|
|
entryPanel = GameManager.Instance.NumberPanel;
|
|
Debug.Log(entryPanel);
|
|
entryPanel.SetActive(true);
|
|
collectSlots();
|
|
|
|
//подгрузить данные если были
|
|
string currentNumber = getCurrentNumberOrNull();
|
|
if (currentNumber != null) fillNumber(currentNumber);
|
|
|
|
currentDigitIndex = getDigitIndex(text(currentSlotIndex).text);
|
|
if (currentDigitIndex == -1) currentDigitIndex = 0;
|
|
|
|
|
|
switchSlot(1); switchSlot(-1);//чтобы отобразилась подсветка
|
|
colorSlots();
|
|
}
|
|
|
|
public void update()
|
|
{
|
|
|
|
//Меняем слот
|
|
if (In.GetButtonDown(InSignal.left1))
|
|
{
|
|
switchSlot(-1);
|
|
}
|
|
if (In.GetButtonDown(InSignal.right1))
|
|
{
|
|
switchSlot(1);
|
|
}
|
|
|
|
|
|
//меняем букву в слоте
|
|
if (In.GetButtonDown(InSignal.left2))
|
|
{
|
|
switchDigit(-1);
|
|
}
|
|
if (In.GetButtonDown(InSignal.right2))
|
|
{
|
|
switchDigit(1);
|
|
}
|
|
|
|
//записываем и выходим
|
|
if (In.GetButtonDown(InSignal.down2))
|
|
{
|
|
//GameManager.instance.stateMachine.ChangeState(new SettingsState());
|
|
GameManager.Instance.stateMachine.ChangeState(prevState);
|
|
}
|
|
|
|
}
|
|
public void exit()
|
|
{
|
|
//сохранить данные закрыть панельку
|
|
string res = collectResult();
|
|
PlayerPrefs.SetString(NUMBER_KEY, res);
|
|
entryPanel.SetActive(false);
|
|
}
|
|
|
|
|
|
private void collectSlots()
|
|
{
|
|
slots = new Image[length];
|
|
for (int i = 0; i < length; i++)
|
|
{
|
|
slots[i] = entryPanel.transform.GetChild(i).GetComponent<Image>();
|
|
}
|
|
}
|
|
private TextMeshProUGUI text(int index)
|
|
{
|
|
return slots[index].transform.GetChild(0).GetComponent<TextMeshProUGUI>();
|
|
}
|
|
|
|
private string getCurrentNumberOrNull()
|
|
{
|
|
if (PlayerPrefs.HasKey(NUMBER_KEY))
|
|
{
|
|
return PlayerPrefs.GetString(NUMBER_KEY);
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private void fillNumber(string number)
|
|
{
|
|
for (int i = 0; i < length; i++)
|
|
{
|
|
text(i).text = number[i].ToString();
|
|
}
|
|
}
|
|
|
|
private void switchSlot(int direction)
|
|
{
|
|
//старый слот не подсвечиваем
|
|
slots[currentSlotIndex].color = normalColor;
|
|
|
|
//циклично перемещаем индекс
|
|
direction = (int)Mathf.Sign(direction);
|
|
currentSlotIndex = (currentSlotIndex + direction + length) % length;
|
|
|
|
//новый слот подсвечиваем
|
|
slots[currentSlotIndex].color = highlightedColor;
|
|
currentDigitIndex = getDigitIndex(text(currentSlotIndex).text);
|
|
|
|
//??все?
|
|
}
|
|
|
|
private void switchDigit(int direction)
|
|
{
|
|
direction = (int)Mathf.Sign(direction);
|
|
currentDigitIndex = (currentDigitIndex + digits.Length + direction) % digits.Length;
|
|
|
|
text(currentSlotIndex).text = digits[currentDigitIndex];
|
|
}
|
|
|
|
private int getDigitIndex(string symbol) => Array.IndexOf(digits, symbol);
|
|
|
|
private string collectResult()
|
|
{
|
|
StringBuilder sb = new();
|
|
for (int i = 0; i < length; i++)
|
|
{
|
|
sb.Append(text(i).text);
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
void colorSlots()
|
|
{
|
|
for (int i = 0; i < length; i++)
|
|
{
|
|
slots[i].color = normalColor;
|
|
}
|
|
slots[0].color = highlightedColor;
|
|
}
|
|
}
|