162 lines
4.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
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 RevengeButton 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;
public bool ControlsEnabled { get; set; }
public void UpdateControls()
{
int direction = 0;
if (In.GetButtonDownAnyPlayer(PlayerAction.left)) direction -= 1;
if (In.GetButtonDownAnyPlayer(PlayerAction.right)) direction += 1;
int newButtonIndex = Mathf.Clamp(buttonIndex + direction, 0, buttons.Count-1);
bool indexChanged = newButtonIndex != buttonIndex;
if (indexChanged)
{
buttonIndex = newButtonIndex;
RefreshButtons();
}
//interact
if(In.GetButtonDownAnyPlayer(PlayerAction.action))
{
var selectedButton = buttons[buttonIndex];
selectedButton.Interact();
}
}
public void Open()
{
if (_animation != null)
{
_animation.Kill();
}
Initialize();
_animation = GetOpenSequence();
_animation.onComplete += () =>
{
ControlsEnabled = true;
RefreshButtons();
};
_animation.PlayForward();
}
public void Close()
{
ControlsPerformer.Instance.SetControlable(null);
foreach (var button in buttons)
button.OnInteract -= Close;
if(buttonIndex == 0)
GameManager.Instance.PerformRevenge();
else
GameManager.Instance.GotoMenu();
if (_animation != null)
{
_animation.Kill();
}
_animation = GetCloseSequence();
_animation.onComplete += () =>
{
ControlsEnabled = false;
revengeButton.Select();
menuButton.Deselect();
gameObject.SetActive(false);
};
_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()
{
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);
foreach (var button in buttons)
button.OnInteract += Close;
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();
}
}
}