123 lines
4.3 KiB
C#
123 lines
4.3 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace PrizeSystem
|
|
{
|
|
public class TakeOrRiskPanel : MonoBehaviour
|
|
{
|
|
[Header("Ссылки на панели")]
|
|
[SerializeField] private Image CurrentPrizeFrame;
|
|
[SerializeField] private Image NextPrizeFrame;
|
|
[SerializeField] private PrizeView CurrentPrizeView;
|
|
[SerializeField] private PrizeView NextPrizeView;
|
|
|
|
[Header("Цветовая анимация")]
|
|
[SerializeField] private Color DisabledColor;
|
|
[SerializeField] float HsvSpeed = 0.3f;
|
|
|
|
[Header("Настройка таймера")]
|
|
[SerializeField] private Image TimerRing;
|
|
[SerializeField] private AnimationCurve TimerFillCurve;
|
|
[SerializeField] private float TimeToAccept = 2;
|
|
|
|
[Header("Анимация размера панелей")]
|
|
[SerializeField] private float ScalingSpeed = 1;
|
|
[SerializeField] private float SelectedScale = 1.05f;
|
|
[SerializeField] private float IdleScale = 0.95f;
|
|
|
|
[SerializeField] private AudioClip StartAcceptingClip;
|
|
|
|
private TakeOrRiskOption SelectedOption;
|
|
public event Action<TakeOrRiskOption> OnComplete;
|
|
|
|
private Image activeFrame;
|
|
private Image inactiveFrame;
|
|
private float acceptTimer = 0;
|
|
|
|
private void OnEnable()
|
|
{
|
|
SelectedOption = TakeOrRiskOption.Risk;
|
|
|
|
activeFrame = CurrentPrizeFrame;
|
|
inactiveFrame = NextPrizeFrame;
|
|
|
|
acceptTimer = 0;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
ClearPrizes();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
var inputs = ProcessInput();
|
|
UpdateState(inputs.selectedOption, inputs.timerDelta);
|
|
DisplayGraphics();
|
|
}
|
|
|
|
public void SetPrizes(Prize CurrentPrize, Prize NextPrize)
|
|
{
|
|
CurrentPrizeView.SetPrize(CurrentPrize);
|
|
NextPrizeView.SetPrize(NextPrize);
|
|
}
|
|
|
|
private void ClearPrizes()
|
|
{
|
|
CurrentPrizeView.Clear();
|
|
NextPrizeView.Clear();
|
|
}
|
|
|
|
private (TakeOrRiskOption selectedOption, float timerDelta) ProcessInput()
|
|
{
|
|
if (In.GetButtonAnyPlayer(PlayerAction.left) && SelectedOption != TakeOrRiskOption.Risk)
|
|
{
|
|
SoundManager.instance.playSwitch();
|
|
SelectedOption = TakeOrRiskOption.Risk;
|
|
}
|
|
if (In.GetButtonAnyPlayer(PlayerAction.right) && SelectedOption != TakeOrRiskOption.Take)
|
|
{
|
|
SoundManager.instance.playSwitch();
|
|
SelectedOption = TakeOrRiskOption.Take;
|
|
}
|
|
|
|
if(In.GetButtonDownAnyPlayer(PlayerAction.down)) SoundManager.instance.playClip(StartAcceptingClip);
|
|
float timerDelta = Time.deltaTime * (In.GetButtonAnyPlayer(PlayerAction.down) ? 1 : -1);
|
|
|
|
return (SelectedOption, timerDelta);
|
|
}
|
|
|
|
private void UpdateState(TakeOrRiskOption selectedOption, float timerDelta)
|
|
{
|
|
activeFrame = selectedOption == TakeOrRiskOption.Take ? CurrentPrizeFrame : NextPrizeFrame;
|
|
inactiveFrame = selectedOption == TakeOrRiskOption.Take ? NextPrizeFrame : CurrentPrizeFrame;
|
|
|
|
acceptTimer = Mathf.Clamp(acceptTimer + timerDelta, 0, TimeToAccept);
|
|
if (acceptTimer >= TimeToAccept) Accept();
|
|
}
|
|
|
|
private void DisplayGraphics()
|
|
{
|
|
// Скалирование
|
|
activeFrame.transform.localScale = Vector3.MoveTowards(activeFrame.transform.localScale, Vector3.one * SelectedScale, Time.deltaTime * ScalingSpeed);
|
|
inactiveFrame.transform.localScale = Vector3.MoveTowards(inactiveFrame.transform.localScale, Vector3.one * IdleScale, Time.deltaTime * ScalingSpeed);
|
|
|
|
//Цвет рамки
|
|
float hue = (Time.time * HsvSpeed) % 1f;
|
|
activeFrame.color = Color.HSVToRGB(hue, 1, 1);
|
|
inactiveFrame.color = DisabledColor;
|
|
|
|
//Круговой таймер
|
|
float timerPercent = acceptTimer / TimeToAccept;
|
|
TimerRing.fillAmount = TimerFillCurve.Evaluate(timerPercent);
|
|
}
|
|
|
|
private void Accept()
|
|
{
|
|
OnComplete?.Invoke(SelectedOption);
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|