169 lines
4.9 KiB
C#
169 lines
4.9 KiB
C#
using UnityEngine;
|
||
using System;
|
||
using DG.Tweening;
|
||
using System.Collections.Generic;
|
||
using UnityEngine.UI;
|
||
|
||
public class PveLossPanel : MonoBehaviour, IControlable
|
||
{
|
||
[SerializeField] private RectTransform panel;
|
||
[SerializeField] private float startPanelX, shownPanelX, hiddenPanelX;
|
||
[SerializeField] private Transform description;
|
||
[SerializeField] private PveLossButton revengeButton;
|
||
[SerializeField] private GotoMenuButton menuButton;
|
||
[SerializeField] private Image background;
|
||
[SerializeField] private Color backgroundEnabledColor, backgroundDisabledColor;
|
||
|
||
[SerializeField] private AnimationCurve elasticCurve;
|
||
private Sequence _animation;
|
||
|
||
[Header("Работа с кнопками")]
|
||
[SerializeField] private List<PveLossButton> buttons;
|
||
private int buttonIndex;
|
||
private Action _continueAction;
|
||
|
||
public bool ControlsEnabled { get; set; }
|
||
|
||
public void UpdateControls()
|
||
{
|
||
int direction = In.GetAxis(JoystickSide.Both, InteractMode.Discrete).x;
|
||
|
||
int newButtonIndex = Mathf.Clamp(buttonIndex + direction, 0, buttons.Count-1);
|
||
bool indexChanged = newButtonIndex != buttonIndex;
|
||
if (indexChanged)
|
||
{
|
||
buttonIndex = newButtonIndex;
|
||
RefreshButtons();
|
||
}
|
||
|
||
//interact
|
||
if(In.GetAction(JoystickSide.Both, InteractMode.Discrete))
|
||
{
|
||
Close();
|
||
}
|
||
}
|
||
|
||
public void Open(Action continueAction)
|
||
{
|
||
ResultPanelTimeout timeout = GetComponent<ResultPanelTimeout>();
|
||
timeout?.Cancel();
|
||
if (_animation != null)
|
||
{
|
||
_animation.Kill();
|
||
}
|
||
|
||
_continueAction = continueAction;
|
||
Initialize();
|
||
_animation = GetOpenSequence();
|
||
_animation.onComplete += () =>
|
||
{
|
||
ControlsEnabled = true;
|
||
RefreshButtons();
|
||
timeout?.StartCountdown();
|
||
};
|
||
_animation.PlayForward();
|
||
}
|
||
|
||
public void Close()
|
||
{
|
||
int selectedButtonIndex = buttonIndex;
|
||
if (selectedButtonIndex == 0 && !PaymentSystem.TryPayForRevenge(GameMode.pve))
|
||
return;
|
||
|
||
GetComponent<ResultPanelTimeout>()?.Cancel();
|
||
ControlsEnabled = false;
|
||
ControlsPerformer.Instance.SetControlable(null);
|
||
|
||
|
||
if (_animation != null)
|
||
{
|
||
_animation.Kill();
|
||
}
|
||
|
||
_animation = GetCloseSequence();
|
||
_animation.onComplete += () =>
|
||
{
|
||
ControlsEnabled = false;
|
||
revengeButton.Select();
|
||
menuButton.Deselect();
|
||
gameObject.SetActive(false);
|
||
|
||
if (selectedButtonIndex == 0)
|
||
_continueAction?.Invoke();
|
||
else
|
||
GameManager.Instance.GotoMenu();
|
||
|
||
_continueAction = null;
|
||
};
|
||
_animation.PlayForward();
|
||
}
|
||
|
||
private Sequence GetCloseSequence()
|
||
{
|
||
Sequence seq = DOTween.Sequence();
|
||
seq.Append(panel.DOAnchorPosX(hiddenPanelX, 1f).SetEase(Ease.InBack));
|
||
seq.Join(background.DOColor(backgroundDisabledColor, 2).SetEase(Ease.InCubic));
|
||
|
||
return seq;
|
||
}
|
||
|
||
private void Initialize()
|
||
{
|
||
ControlsEnabled = false;
|
||
panel.anchoredPosition = new Vector2(startPanelX, panel.anchoredPosition.y);
|
||
description.localScale = Vector3.zero;
|
||
|
||
revengeButton.Initialize();
|
||
|
||
revengeButton.transform.localScale = Vector3.zero;
|
||
menuButton.transform.localScale = Vector3.zero;
|
||
|
||
background.color = backgroundDisabledColor;
|
||
gameObject.SetActive(true);
|
||
ControlsPerformer.Instance.SetControlable(this);
|
||
|
||
buttonIndex = 0;
|
||
}
|
||
|
||
private Sequence GetOpenSequence()
|
||
{
|
||
Sequence seq = DOTween.Sequence();
|
||
|
||
//приезд панели
|
||
seq.Append(panel.DOAnchorPosX(shownPanelX, 1f).SetEase(Ease.OutBack));
|
||
|
||
//фон
|
||
seq.Join(background.DOColor(backgroundEnabledColor, 1).SetEase(Ease.OutCubic));
|
||
|
||
//текст описания
|
||
seq.Append(description.DOScale(1, 0.5f).SetEase(elasticCurve));
|
||
seq.AppendInterval(0.2f);
|
||
|
||
//вращение описания
|
||
float rotationDuration = 0.2f;
|
||
seq.Append(description.DOLocalRotate(new Vector3(0, 0, 2), rotationDuration));
|
||
seq.Append(description.DOLocalRotate(new Vector3(0, 0, -2), rotationDuration));
|
||
seq.Append(description.DOLocalRotate(new Vector3(0, 0, 0), rotationDuration / 2));
|
||
|
||
//появление кнопок
|
||
seq.Join(revengeButton.transform.DOScale(1.05f, 0.5f).SetEase(Ease.OutBack));
|
||
seq.Append(menuButton.transform.DOScale(0.9f, 0.5f).SetEase(Ease.OutBack));
|
||
|
||
return seq;
|
||
}
|
||
|
||
private void RefreshButtons()
|
||
{
|
||
if (buttonIndex == 0)
|
||
{
|
||
buttons[0].Select();
|
||
buttons[1].Deselect();
|
||
}
|
||
else if (buttonIndex == 1)
|
||
{
|
||
buttons[1].Select();
|
||
buttons[0].Deselect();
|
||
}
|
||
}
|
||
}
|