178 lines
5.4 KiB
C#
178 lines
5.4 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
|
||
namespace PrizeSystem
|
||
{
|
||
public class CharacterPrizeConfigurator : MonoBehaviour
|
||
{
|
||
[SerializeField] TypeSelectionPanel typePanel;
|
||
[SerializeField] PrizeSettingPanel prizePanel;
|
||
[SerializeField] CharacterSelectionPanel characterPanel;
|
||
|
||
[SerializeField] List<GameObject> controlPanels;
|
||
|
||
[SerializeField] private Color AccentColor;
|
||
[SerializeField] private AudioClip ClickClip;
|
||
int controlIndex = 0;
|
||
|
||
|
||
bool readyToWork = false;
|
||
|
||
float exitTimer = 0;
|
||
|
||
private GameObject SelectedPanel => controlPanels[controlIndex];
|
||
IControl SelectedControl => SelectedPanel.GetComponent<IControl>();
|
||
|
||
private List<Prize> prizesCache;
|
||
private Coroutine BlinkCoroutine;
|
||
|
||
|
||
private void OnEnable()
|
||
{
|
||
readyToWork = false;
|
||
controlIndex = 0;
|
||
SetupPanels();
|
||
|
||
prizesCache = PrizeSerializer.LoadPrizes();
|
||
|
||
|
||
Debug.Log($"Загружены призы: \n{PrizeSerializer.SerializePrizes(prizesCache)}");
|
||
|
||
prizePanel.SetPanelByPrize(prizesCache[characterPanel.CharacterIndex]);
|
||
|
||
typePanel.SetType(prizesCache[0].PrizeType);
|
||
|
||
typePanel.OnTypeChanged += TypePanel_OnTypeChanged;
|
||
|
||
characterPanel.BeforeCharacterChange += CharacterPanel_BeforeCharacterChange;
|
||
characterPanel.OnCharacterChanged += CharacterPanel_OnCharacterChanged;
|
||
}
|
||
|
||
|
||
private void OnDisable()
|
||
{
|
||
typePanel.OnTypeChanged -= TypePanel_OnTypeChanged;
|
||
characterPanel.OnCharacterChanged -= CharacterPanel_OnCharacterChanged;
|
||
|
||
LightOnPanel(false);
|
||
}
|
||
|
||
private void TypePanel_OnTypeChanged(PrizeType newType)
|
||
{
|
||
prizePanel.SetPanelByType(newType);
|
||
}
|
||
private void CharacterPanel_BeforeCharacterChange(Character oldCharacter)
|
||
{
|
||
prizesCache[characterPanel.CharacterIndex] = prizePanel.GetPrize();
|
||
}
|
||
private void CharacterPanel_OnCharacterChanged(Character character)
|
||
{
|
||
Prize prize = prizesCache[characterPanel.CharacterIndex];
|
||
|
||
typePanel.SetType(prize.PrizeType);
|
||
prizePanel.SetPanelByPrize(prize);
|
||
}
|
||
|
||
#region ОБРАБОТКА ВВОДА
|
||
private void Update()
|
||
{
|
||
|
||
if (readyToWork)
|
||
{
|
||
var input = GetInputs();
|
||
if (input.Direction != 0) SelectedControl.Swipe(input.Direction);
|
||
if (input.Switch) SelectedControl.Switch();
|
||
if (input.NextPanel) NextPanel();
|
||
SaveProcessing(input.HoldSave);
|
||
}
|
||
else
|
||
{
|
||
if(!In.AnyButton()) readyToWork = true;
|
||
}
|
||
|
||
|
||
}
|
||
private (int Direction, bool Switch, bool NextPanel, bool HoldSave) GetInputs()
|
||
{
|
||
int Direction = 0;
|
||
if (In.GetButtonAnyPlayer(PlayerAction.right)) Direction += 1;
|
||
if (In.GetButtonAnyPlayer(PlayerAction.left)) Direction -= 1;
|
||
|
||
bool Switch = In.GetButtonDown(ButtonPinMapping.down1);
|
||
|
||
bool NextPanel = In.GetButtonDown(ButtonPinMapping.down2);
|
||
|
||
bool HoldExit = In.GetButton(ButtonPinMapping.startOnePlayer) && In.GetButton(ButtonPinMapping.startTwoPlayers);
|
||
|
||
return (Direction, Switch, NextPanel, HoldExit);
|
||
}
|
||
#endregion
|
||
|
||
|
||
private void NextPanel()
|
||
{
|
||
LightOnPanel(false);
|
||
controlIndex = (controlIndex + 1 + controlPanels.Count) % controlPanels.Count;
|
||
LightOnPanel(true);
|
||
if(ClickClip!= null) {SoundManager.instance.playClip(ClickClip); }
|
||
}
|
||
|
||
private void LightOnPanel(bool on)
|
||
{
|
||
if (on && SelectedPanel.TryGetComponent<Graphic>(out var graphic))
|
||
{
|
||
BlinkCoroutine = StartCoroutine(BlinkAnimation(graphic));
|
||
}
|
||
else
|
||
{
|
||
StopCoroutine(BlinkCoroutine);
|
||
BlinkCoroutine = null;
|
||
SelectedPanel.GetComponent<Graphic>().color = Color.black;
|
||
}
|
||
}
|
||
|
||
//вероятно это будет единственный метод для обновления.
|
||
private void SetupPanels()
|
||
{
|
||
LightOnPanel(true);
|
||
}
|
||
|
||
private void SaveProcessing(bool HoldSave)
|
||
{
|
||
if (HoldSave) exitTimer += Time.deltaTime;
|
||
else exitTimer -= Time.deltaTime;
|
||
exitTimer = Mathf.Clamp(exitTimer, 0f, 2f);
|
||
|
||
if (exitTimer >= 2)
|
||
{
|
||
exitTimer = 0;
|
||
Save();
|
||
Exit();
|
||
}
|
||
}
|
||
|
||
private void Save()
|
||
{
|
||
//Кэшнуть текущего персонажа
|
||
prizesCache[characterPanel.CharacterIndex] = prizePanel.GetPrize();
|
||
PrizeSerializer.SavePrizes(prizesCache);
|
||
CharacterController.I.RefreshPrizes();
|
||
}
|
||
|
||
private void Exit()
|
||
{
|
||
GameManager.instance.stateMachine.ChangeState(new SettingsState());
|
||
}
|
||
IEnumerator BlinkAnimation(Graphic graphic)
|
||
{
|
||
while (true)
|
||
{
|
||
graphic.color = Vector4.Lerp(AccentColor, Color.black, Mathf.PingPong(Time.time, 1));
|
||
yield return null;
|
||
}
|
||
}
|
||
}
|
||
} |