chip-gameV3/Assets/Scripts/Settings/SetPasswordPanel.cs
sega 8a6d23b77d Реорг Scripts
Разделено на 4 модуля
- Menu
- Gameplay
- Settings
- Common
2026-07-15 10:49:55 +03:00

194 lines
4.5 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SetPasswordPanel : MonoBehaviour
{
[SerializeField] GameObject ModalWindow;
[SerializeField] AnimationCurve fillCurve;
int enteredSymbols = 0;
int SymbolsCount = 6;
float acceptTimer = 0;
public Image AcceptImage;
float clearTimer = 0;
public Image ClearImage;
public List<string> symbols = new List<string>();
public List<RectTransform> slots;
public GameObject L1;
public GameObject L2;
public GameObject R1;
public GameObject R2;
public GameObject D1;
public GameObject D2;
private List<GameObject> spawnedObjs = new();
List<ButtonPinMapping> availableButtons = new List<ButtonPinMapping>
{
ButtonPinMapping.left1,
ButtonPinMapping.left2,
ButtonPinMapping.right1,
ButtonPinMapping.right2,
ButtonPinMapping.down1,
ButtonPinMapping.down2,
};
bool ready = false;
private void OnEnable()
{
ModalWindow.SetActive(true);
}
void Update()
{
if (!ready)
{
ready = AllButtonsUp();
return;
}
//когда готово
//p1 применить
AcceptProcess();
//p2 очистить
ClearProcess();
//получить комбинацию по кнопке
ButtonsProcess();
}
void AcceptProcess()
{
ProcessImageTimer(AcceptImage, acceptTimer, 2);
if (enteredSymbols != SymbolsCount) return;
if (In.GetButton(ButtonPinMapping.startOnePlayer)) acceptTimer += Time.deltaTime;
else acceptTimer -= Time.deltaTime;
acceptTimer = Mathf.Clamp(acceptTimer, 0.0f, 2.0f);
if (acceptTimer == 2)
{
acceptTimer = 0.0f;
Accept();
}
}
private void Accept()
{
string pwd = "";
foreach (var s in symbols)
{
pwd += s;
}
ExternalFilesGetter.SetPassword(pwd);
Clear();
GameManager.instance.stateMachine.ChangeState(new SettingsState());
}
void ClearProcess()
{
if (In.GetButton(ButtonPinMapping.startTwoPlayers)) clearTimer += Time.deltaTime;
else clearTimer -= Time.deltaTime;
clearTimer = Mathf.Clamp(clearTimer, 0.0f, 2.0f);
ProcessImageTimer(ClearImage, clearTimer, 2);
if (clearTimer == 2)
{
clearTimer = 0.0f;
Clear();
}
}
private void Clear()
{
acceptTimer = 0;
clearTimer = 0;
enteredSymbols = 0;
symbols.Clear();
foreach (var go in spawnedObjs)
{
Destroy(go);
}
spawnedObjs.Clear();
ready = false;
}
void ButtonsProcess()
{
if (enteredSymbols >= SymbolsCount) return;
foreach (ButtonPinMapping button in availableButtons)
{
if (In.GetButtonDown(button))
{
string combinationStr = GetStrCombination(button);
symbols.Add(combinationStr);
DisplaySymbol(combinationStr);
enteredSymbols++;
return;
}
}
}
void DisplaySymbol(string symbol)
{
GameObject prefab = symbol switch
{
"L1" => L1,
"L2" => L2,
"R1" => R1,
"R2" => R2,
"A1" => D1,
"A2" => D2,
};
GameObject spawned = Instantiate(prefab, slots[enteredSymbols]);
spawnedObjs.Add(spawned);
}
string GetStrCombination(ButtonPinMapping button)
=> button switch
{
ButtonPinMapping.left1 => "L1",
ButtonPinMapping.left2 => "L2",
ButtonPinMapping.right1 => "R1",
ButtonPinMapping.right2 => "R2",
ButtonPinMapping.down1 => "A1",
ButtonPinMapping.down2 => "A2",
_ => null
};
void ProcessImageTimer(Image image, float time, float maxtime)
=> image.fillAmount = fillCurve.Evaluate(time / maxtime);
bool AllButtonsUp()
{
bool anyButtonDown =
In.GetButtonAnyPlayer(PlayerAction.left) ||
In.GetButtonAnyPlayer(PlayerAction.right) ||
In.GetButtonAnyPlayer(PlayerAction.down) ||
In.GetButtonDown(ButtonPinMapping.startOnePlayer) ||
In.GetButtonDown(ButtonPinMapping.startOnePlayer);
if (anyButtonDown) return false;
else
{
ModalWindow.SetActive(false);
return true;
}
}
}