124 lines
3.0 KiB
C#
124 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class VitrinaPanel : MonoBehaviour
|
|
{
|
|
private bool initialized = false;
|
|
//çàïðîñèòü êîðîáêè, ïðîâåðèòü êàêèå òðåáóåòñÿ, íàéòè ïåðåñå÷åíèå. Åñëè íåò, òî âîçâðàò.
|
|
public event Action OnComplete;
|
|
|
|
[SerializeField] private Transform BoxesRoot;
|
|
[SerializeField] private GameObject BoxViewPrefab;
|
|
|
|
private List<VitrinaBox> generatedBoxes;
|
|
private int pointerIndex;
|
|
|
|
[SerializeField] private float timer = 0;
|
|
private float timeToAct = 3;
|
|
|
|
VitrinaBox CurrentBox => generatedBoxes[pointerIndex];
|
|
|
|
private void OnEnable()
|
|
{
|
|
initialized = false;
|
|
}
|
|
private void OnDisable()
|
|
{
|
|
initialized = false;
|
|
ClearBoxes();
|
|
}
|
|
|
|
public void Initialize(int[] requiredBoxed)
|
|
{
|
|
if (initialized) return;
|
|
//òóò ïðîâåðêà ÷òî åñòü òàêèå êîðîáêè
|
|
pointerIndex = 0;
|
|
GenerateBoxes(requiredBoxed);
|
|
|
|
CurrentBox.SetEnabled(true);
|
|
initialized = true;
|
|
}
|
|
|
|
private void GenerateBoxes(int[] indexes)
|
|
{
|
|
if (generatedBoxes == null)
|
|
generatedBoxes = new();
|
|
|
|
foreach (int index in indexes)
|
|
{
|
|
var go = Instantiate(BoxViewPrefab, BoxesRoot);
|
|
|
|
if (go.TryGetComponent<VitrinaBox>(out var Box))
|
|
{
|
|
|
|
generatedBoxes.Add(Box);
|
|
Box.SetNumber(index);
|
|
}
|
|
else
|
|
{
|
|
Destroy(go);
|
|
Debug.Log("Ïðåôàá íå ñîäåðæèò êîìïîíåíòà VitrinaBoxView", gameObject);
|
|
}
|
|
}
|
|
}
|
|
private void ClearBoxes()
|
|
{
|
|
foreach (var box in generatedBoxes)
|
|
Destroy(box.gameObject);
|
|
generatedBoxes.Clear();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!initialized) return;
|
|
var input = CollectInputs();
|
|
|
|
if (input.direction != 0)
|
|
{
|
|
int count = generatedBoxes == null ? 0 : generatedBoxes.Count;
|
|
//îòîáðàçèòü èçìåíåíèÿ
|
|
|
|
CurrentBox.SetEnabled(false);
|
|
CurrentBox.shake = false;
|
|
timer = 0;
|
|
|
|
pointerIndex = (pointerIndex + input.direction + count) % count;
|
|
CurrentBox.SetEnabled(true);
|
|
|
|
}
|
|
|
|
if (input.action)
|
|
{
|
|
CurrentBox.shake = true;
|
|
timer = Mathf.Clamp(timer + Time.deltaTime, 0, timeToAct);
|
|
}
|
|
else
|
|
{
|
|
CurrentBox.shake = false;
|
|
timer = 0;
|
|
}
|
|
|
|
|
|
if (timer >= timeToAct)
|
|
{
|
|
_ = PrizeStandController.OpenBoxAsync(pointerIndex + 1);
|
|
timer = 0;
|
|
|
|
OnComplete?.Invoke();
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
(int direction, bool action) CollectInputs()
|
|
{
|
|
int direction = (In.GetButtonDownAnyPlayer(PlayerAction.right) ? 1 : 0) -
|
|
(In.GetButtonDownAnyPlayer(PlayerAction.left) ? 1 : 0);
|
|
bool action = In.GetButtonAnyPlayer(PlayerAction.down);
|
|
return (direction, action);
|
|
}
|
|
|
|
|
|
|
|
}
|