159 lines
4.5 KiB
C#
159 lines
4.5 KiB
C#
using DG.Tweening;
|
||
using System.Collections.Generic;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class PvpWinPanel : 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);
|
||
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()
|
||
{
|
||
if (_animation != null)
|
||
{
|
||
_animation.Kill();
|
||
}
|
||
|
||
_animation = GetCloseSequence();
|
||
_animation.onComplete += () =>
|
||
{
|
||
ControlsEnabled = false;
|
||
revengeButton.Select();
|
||
menuButton.Deselect();
|
||
};
|
||
_animation.PlayForward();
|
||
}
|
||
|
||
private Sequence GetCloseSequence()
|
||
{
|
||
Sequence seq = DOTween.Sequence();
|
||
|
||
foreach (var button in buttons)
|
||
{
|
||
seq.Join(button.transform.DOScale(0, 0.4f).SetEase(Ease.InBack));
|
||
}
|
||
|
||
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;
|
||
|
||
var text = description.GetComponent<TextMeshProUGUI>();
|
||
int winner = GameManager.Instance.currentPlayer == Team.Player1 ? 1 : 2;
|
||
text.text = $"игрок {winner} победил";
|
||
|
||
revengeButton.Initialize();
|
||
|
||
revengeButton.transform.localScale = Vector3.zero;
|
||
menuButton.transform.localScale = Vector3.zero;
|
||
|
||
background.color = backgroundDisabledColor;
|
||
|
||
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();
|
||
}
|
||
}
|
||
}
|