chip-game/Assets/Scripts/EnterPasswordPanel.cs

176 lines
4.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnterPasswordPanel : MonoBehaviour
{
int enteredSymbols = 0;
int SymbolsCount = 6;
public AudioClip trueSound;
public AudioClip falseSound;
public AnimationCurve fillCurve;
float clearTimer = 0;
public Image ClearImage;
float exitTimer = 0;
public Image ExitImage;
public string symbols = "";
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();
public bool truePassword = false;
private string password;
List<ButtonPinMapping> availableButtons = new List<ButtonPinMapping>
{
ButtonPinMapping.left1,
ButtonPinMapping.left2,
ButtonPinMapping.right1,
ButtonPinMapping.right2,
ButtonPinMapping.down1,
ButtonPinMapping.down2,
};
public void Initialize(string password)
{
truePassword = false;
this.password = password;
}
public void _Update()
{
//p2 очистить
ClearProcess();
ExitProcess();
//получить комбинацию по кнопке
ButtonsProcess();
}
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()
{
clearTimer = 0;
exitTimer = 0;
enteredSymbols = 0;
symbols = string.Empty;
foreach (var go in spawnedObjs)
{
Destroy(go);
}
spawnedObjs.Clear();
}
private void ExitProcess()
{
if (In.GetButton(ButtonPinMapping.startOnePlayer)) exitTimer += Time.deltaTime;
else exitTimer -= Time.deltaTime;
exitTimer = Mathf.Clamp(exitTimer, 0.0f, 2.0f);
ProcessImageTimer(ExitImage, exitTimer, 2);
if (exitTimer == 2)
{
Clear();
GameManager.instance.stateMachine.ChangeState(new MenuState());
}
}
void ButtonsProcess()
{
if (enteredSymbols >= SymbolsCount) return;
foreach (ButtonPinMapping button in availableButtons)
{
if (In.GetButtonDown(button))
{
string combinationStr = GetStrCombination(button);
symbols+=combinationStr;
DisplaySymbol(combinationStr);
enteredSymbols++;
break;
}
}
if(enteredSymbols == SymbolsCount)
{
if(symbols == password)
{
truePassword = true;
SoundManager.instance.playClip(trueSound);
}
else
{
truePassword = false;
SoundManager.instance.playClip(falseSound);
}
Clear();
//введи все символы надо проверка
}
}
void DisplaySymbol(string symbol)
{
GameObject prefab = symbol switch
{
"L1" => L1,
"L2" => L2,
"R1" => R1,
"R2" => R2,
"A1" => D1,
"A2" => D2,
_ => L1
};
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);
}