143 lines
4.8 KiB
C#
143 lines
4.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
using RR.Game2D.Core.Managers;
|
|
using TMPro;
|
|
using UnityEngine.EventSystems;
|
|
using Random = System.Random;
|
|
|
|
namespace RR.Common.UI
|
|
{
|
|
public class GameEndUIController : MonoBehaviour
|
|
{
|
|
[Header("Меню выигрыша")]
|
|
[SerializeField] private CanvasGroup winPanel;
|
|
[SerializeField] private Button mainMenuButtonWin;
|
|
[SerializeField] private TextMeshProUGUI winText;
|
|
|
|
[Header("Меню проигрыша")]
|
|
[SerializeField] private CanvasGroup losePanel;
|
|
|
|
public event Action<float> WinBannerShown;
|
|
public event Action<float> LoseBannerShown;
|
|
public event Action BannerDismissed;
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (GameManager.Instance == null) return;
|
|
|
|
GameManager.Instance.GameWonEvent += OnGameWon;
|
|
GameManager.Instance.GameOverEvent += OnGameOver;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (GameManager.Instance == null) return;
|
|
|
|
GameManager.Instance.GameWonEvent -= OnGameWon;
|
|
GameManager.Instance.GameOverEvent -= OnGameOver;
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
if (mainMenuButtonWin) mainMenuButtonWin.onClick.AddListener(OnMainMenuClicked);
|
|
|
|
if (losePanel) losePanel.GetComponent<LoseBannerController>().ContinueClicked += OnContinueClicked;
|
|
|
|
// Изначально скрыто
|
|
SetVisible(winPanel, false, true);
|
|
SetVisible(losePanel, false, true);
|
|
GetComponent<Image>().color = new Color(0, 0,0, 0);
|
|
}
|
|
|
|
private void OnMainMenuClicked()
|
|
{
|
|
if(winPanel.interactable) SetVisible(winPanel, false);
|
|
if(losePanel.interactable) SetVisible(losePanel, false);
|
|
BannerDismissed?.Invoke();
|
|
GameManager.Instance.SetPause(false);
|
|
GameManager.Instance.LoadScene("MainMenu", false);
|
|
}
|
|
|
|
private void OnContinueClicked()
|
|
{
|
|
if(winPanel.interactable) SetVisible(winPanel, false);
|
|
if(losePanel.interactable) SetVisible(losePanel, false);
|
|
GetComponent<Image>().color = new Color(0, 0,0, 0);
|
|
BannerDismissed?.Invoke();
|
|
}
|
|
|
|
private void OnGameWon(float gameTime)
|
|
{
|
|
GameManager.Instance.SetPause(true);
|
|
|
|
GetComponent<Image>().color = new Color(0, 0,0, 100f/255f);
|
|
|
|
winText.text = $"Время игры: {FormatTime(gameTime)}";
|
|
SetVisible(winPanel, true);
|
|
|
|
EventSystem.current.firstSelectedGameObject = mainMenuButtonWin.gameObject;
|
|
EventSystem.current.SetSelectedGameObject(mainMenuButtonWin.gameObject);
|
|
|
|
WinBannerShown?.Invoke(gameTime);
|
|
}
|
|
|
|
private void OnGameOver(float gameTime)
|
|
{
|
|
GameManager.Instance.SetPause(true);
|
|
|
|
GetComponent<Image>().color = new Color(0, 0,0, 100f/255f);
|
|
|
|
SetVisible(losePanel, true);
|
|
|
|
LoseBannerShown?.Invoke(gameTime);
|
|
}
|
|
|
|
private void SetVisible(CanvasGroup canvasGroup, bool visible, bool instant = false)
|
|
{
|
|
if (!canvasGroup) return;
|
|
|
|
canvasGroup.interactable = visible;
|
|
canvasGroup.blocksRaycasts = visible;
|
|
|
|
if (instant)
|
|
{
|
|
canvasGroup.alpha = visible ? 1f : 0f;
|
|
}
|
|
else
|
|
{
|
|
StopAllCoroutines();
|
|
StartCoroutine(Fade(canvasGroup, visible ? 1f : 0f, 0.15f));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Метод для плавного изменения видимости
|
|
/// </summary>
|
|
/// <param name="cg">Группа холстов</param>
|
|
/// <param name="target">Целевое значение альфа-канала</param>
|
|
/// <param name="duration">Продолжительность перехода</param>
|
|
private static IEnumerator Fade(CanvasGroup cg, float target, float duration)
|
|
{
|
|
float start = cg.alpha;
|
|
float t = 0f;
|
|
while (t < duration)
|
|
{
|
|
t += Time.unscaledDeltaTime;
|
|
cg.alpha = Mathf.Lerp(start, target, t / duration);
|
|
yield return null;
|
|
}
|
|
cg.alpha = target;
|
|
}
|
|
|
|
public static string FormatTime(float gameTime)
|
|
{
|
|
TimeSpan interval = TimeSpan.FromSeconds(gameTime);
|
|
|
|
return interval.ToString(@"mm\:ss");
|
|
}
|
|
}
|
|
}
|