167 lines
6.3 KiB
C#
167 lines
6.3 KiB
C#
using DG.Tweening;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
using UnityEngine.UI;
|
|
|
|
public class PvpWinPanel : MonoBehaviour, IControlable
|
|
{
|
|
[SerializeField, FormerlySerializedAs("panel")] private RectTransform _panel;
|
|
[SerializeField, FormerlySerializedAs("startPanelX")] private float _startPanelX;
|
|
[SerializeField, FormerlySerializedAs("shownPanelX")] private float _shownPanelX;
|
|
[SerializeField, FormerlySerializedAs("hiddenPanelX")] private float _hiddenPanelX;
|
|
[SerializeField, FormerlySerializedAs("description")] private Transform _description;
|
|
[SerializeField, FormerlySerializedAs("revengeButton")] private RevengeButton _restartButton;
|
|
[SerializeField, FormerlySerializedAs("menuButton")] private GotoMenuButton _menuButton;
|
|
[SerializeField, FormerlySerializedAs("background")] private Image _background;
|
|
[SerializeField, FormerlySerializedAs("backgroundEnabledColor")] private Color _backgroundEnabledColor;
|
|
[SerializeField, FormerlySerializedAs("backgroundDisabledColor")] private Color _backgroundDisabledColor;
|
|
[SerializeField, FormerlySerializedAs("elasticCurve")] private AnimationCurve _elasticCurve;
|
|
[SerializeField, FormerlySerializedAs("buttons")] private List<PveLossButton> _buttons;
|
|
|
|
private Sequence _animation;
|
|
private int _buttonIndex;
|
|
private Action _restartAction;
|
|
|
|
public bool ControlsEnabled { get; set; }
|
|
|
|
private void OnDisable()
|
|
{
|
|
_animation?.Kill();
|
|
_animation = null;
|
|
_restartAction = null;
|
|
ControlsEnabled = false;
|
|
}
|
|
|
|
public void UpdateControls()
|
|
{
|
|
int direction = In.GetAxis(JoystickSide.Both, InteractMode.Discrete).x;
|
|
int newButtonIndex = Mathf.Clamp(_buttonIndex + direction, 0, _buttons.Count - 1);
|
|
|
|
if (newButtonIndex != _buttonIndex)
|
|
{
|
|
_buttonIndex = newButtonIndex;
|
|
RefreshButtons();
|
|
}
|
|
|
|
if (In.GetAction(JoystickSide.Both, InteractMode.Discrete))
|
|
Close();
|
|
}
|
|
|
|
public void Open(Team winner, bool isMatchWin, int player1Score, int player2Score, Action restartAction)
|
|
{
|
|
ResultPanelTimeout timeout = GetComponent<ResultPanelTimeout>();
|
|
timeout?.Cancel();
|
|
_animation?.Kill();
|
|
_restartAction = restartAction;
|
|
Initialize(winner, isMatchWin, player1Score, player2Score);
|
|
|
|
_animation = GetOpenSequence();
|
|
_animation.OnComplete(() =>
|
|
{
|
|
ControlsEnabled = true;
|
|
RefreshButtons();
|
|
timeout?.StartCountdown();
|
|
});
|
|
_animation.PlayForward();
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
int selectedButtonIndex = _buttonIndex;
|
|
if (selectedButtonIndex == 0 && !_restartButton.TryPay(GameMode.pvp))
|
|
return;
|
|
|
|
GetComponent<ResultPanelTimeout>()?.Cancel();
|
|
ControlsEnabled = false;
|
|
ControlsPerformer.Instance.SetControlable(null);
|
|
|
|
_animation?.Kill();
|
|
_animation = GetCloseSequence();
|
|
_animation.OnComplete(() => CompleteClose(selectedButtonIndex));
|
|
_animation.PlayForward();
|
|
}
|
|
|
|
private void Initialize(Team winner, bool isMatchWin, int player1Score, int player2Score)
|
|
{
|
|
ControlsEnabled = false;
|
|
gameObject.SetActive(true);
|
|
ControlsPerformer.Instance.SetControlable(this);
|
|
|
|
_panel.anchoredPosition = new Vector2(_startPanelX, _panel.anchoredPosition.y);
|
|
_description.localScale = Vector3.zero;
|
|
_description.localRotation = Quaternion.identity;
|
|
_description.GetComponent<TextMeshProUGUI>().text = GetDescription(winner, isMatchWin, player1Score, player2Score);
|
|
|
|
_restartButton.Initialize(GameMode.pvp);
|
|
_restartButton.transform.localScale = Vector3.zero;
|
|
_menuButton.transform.localScale = Vector3.zero;
|
|
_background.color = _backgroundDisabledColor;
|
|
_buttonIndex = 0;
|
|
}
|
|
|
|
private Sequence GetOpenSequence()
|
|
{
|
|
Sequence sequence = DOTween.Sequence();
|
|
sequence.Append(_panel.DOAnchorPosX(_shownPanelX, 1f).SetEase(Ease.OutBack));
|
|
sequence.Join(_background.DOColor(_backgroundEnabledColor, 1f).SetEase(Ease.OutCubic));
|
|
sequence.Append(_description.DOScale(1f, 0.5f).SetEase(_elasticCurve));
|
|
sequence.AppendInterval(0.2f);
|
|
sequence.Append(_description.DOLocalRotate(new Vector3(0f, 0f, 2f), 0.2f));
|
|
sequence.Append(_description.DOLocalRotate(new Vector3(0f, 0f, -2f), 0.2f));
|
|
sequence.Append(_description.DOLocalRotate(Vector3.zero, 0.1f));
|
|
sequence.Join(_restartButton.transform.DOScale(1.05f, 0.5f).SetEase(Ease.OutBack));
|
|
sequence.Append(_menuButton.transform.DOScale(0.9f, 0.5f).SetEase(Ease.OutBack));
|
|
return sequence;
|
|
}
|
|
|
|
private Sequence GetCloseSequence()
|
|
{
|
|
Sequence sequence = DOTween.Sequence();
|
|
|
|
foreach (PveLossButton button in _buttons)
|
|
sequence.Join(button.transform.DOScale(0f, 0.4f).SetEase(Ease.InBack));
|
|
|
|
sequence.Append(_panel.DOAnchorPosX(_hiddenPanelX, 1f).SetEase(Ease.InBack));
|
|
sequence.Join(_background.DOColor(_backgroundDisabledColor, 2f).SetEase(Ease.InCubic));
|
|
return sequence;
|
|
}
|
|
|
|
private void CompleteClose(int selectedButtonIndex)
|
|
{
|
|
ControlsEnabled = false;
|
|
Action restartAction = _restartAction;
|
|
_restartAction = null;
|
|
gameObject.SetActive(false);
|
|
|
|
if (selectedButtonIndex == 0)
|
|
restartAction?.Invoke();
|
|
else
|
|
GameManager.Instance.GotoMenu();
|
|
}
|
|
|
|
private void RefreshButtons()
|
|
{
|
|
for (int index = 0; index < _buttons.Count; index++)
|
|
{
|
|
if (index == _buttonIndex)
|
|
_buttons[index].Select();
|
|
else
|
|
_buttons[index].Deselect();
|
|
}
|
|
}
|
|
|
|
private string GetDescription(Team winner, bool isMatchWin, int player1Score, int player2Score)
|
|
{
|
|
if (winner == Team.None)
|
|
return $"ничья · счёт {player1Score}:{player2Score}";
|
|
|
|
int playerNumber = winner == Team.Player1 ? 1 : 2;
|
|
return isMatchWin
|
|
? $"поздравляем! игрок {playerNumber} победил {player1Score}:{player2Score}"
|
|
: $"игрок {playerNumber} победил · счёт {player1Score}:{player2Score}";
|
|
}
|
|
}
|