Danro/Assets/Scripts/UI/PauseMenu.cs
Danil Iarosh 152066e9a3 feat(game): implement win/lose events and UI updates- Added GameOverEvent and GameWonEvent with total play time
- Updated ContinueGame method to reset lives and coins- Modified GameOver and GameWin methods to invoke events
- Adjusted stage count per world from5 to 3
- Added win banner UI with header text and layout changes
- Updated canvas elements positioning and sizing
- Included new UI elements for win/lose states- Reorganized UI hierarchy in Level UI scene
- Added menu and continue buttons in win panel
- Fixed text components naming and alignment
- Added game start time tracking for play duration
2025-10-31 17:03:20 +03:00

131 lines
4.4 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 System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
using RR.Game2D.Core.Managers;
namespace RR.Common.UI
{
public class PauseMenu : MonoBehaviour
{
[Header("Управление")]
[SerializeField] private InputActionReference close;
[Header("UI")]
[SerializeField] private CanvasGroup panel;
[SerializeField] private Button resumeButton;
[SerializeField] private Button quitButton;
[Header("Связь с другими элементами")]
[SerializeField] private GameEndUIController bannerController;
//[SerializeField] private Button restartButton;
//[SerializeField] private KeyCode toggleKey = KeyCode.Escape; // Esc
private bool isActive = true;
private void OnEnable()
{
if (GameManager.Instance != null)
GameManager.Instance.PauseStateChanged += HandlePauseChanged;
bannerController.WinBannerShown += (f) => { isActive = false; };
bannerController.LoseBannerShown += (f) => { isActive = false; };
bannerController.BannerDismissed += () => { isActive = true; };
}
private void OnDisable()
{
if (GameManager.Instance != null)
GameManager.Instance.PauseStateChanged -= HandlePauseChanged;
}
private void Awake()
{
if (resumeButton) resumeButton.onClick.AddListener(OnResumeClicked);
if (quitButton) quitButton.onClick.AddListener(OnQuitClicked);
//if (restartButton) restartButton.onClick.AddListener(OnRestartClicked);
// Изначально скрыто
SetVisible(false, instant: true);
}
private void Update()
{
if (close.action.WasPressedThisFrame())
GameManager.Instance.TogglePause();
}
/// <summary>
/// Обработка события паузы
/// </summary>
/// <param name="paused">Ставить ли на паузу</param>
private void HandlePauseChanged(bool paused)
{
if (!isActive) return;
SetVisible(paused);
}
/// <summary>
/// Метод для изменения отображения
/// </summary>
/// <param name="visible">Отображать ли меню паузы</param>
/// <param name="instant">Постоянно или нет</param>
private void SetVisible(bool visible, bool instant = false)
{
if (!panel) return;
panel.interactable = visible;
panel.blocksRaycasts = visible;
if (instant)
{
panel.alpha = visible ? 1f : 0f;
}
else
{
StopAllCoroutines();
StartCoroutine(Fade(panel, visible ? 1f : 0f, 0.15f));
}
}
/// <summary>
/// Метод для плавного изменения видимости
/// </summary>
/// <param name="cg">Группа холстов</param>
/// <param name="target">Целевое значение альфа-канала</param>
/// <param name="duration">Продолжительность перехода</param>
private 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;
}
/// <summary>
/// Метод для возобновления игры
/// </summary>
public void OnResumeClicked() => GameManager.Instance.SetPause(false);
//public void OnRestartClicked()
//{
// GameManager.Instance.SetPause(false);
// GameManager.Instance.ResetLevel();
//}
/// <summary>
/// Метод для выхода в главное меню
/// </summary>
public void OnQuitClicked()
{
GameManager.Instance.SetPause(false);
GameManager.Instance.LoadScene("MainMenu", false);
}
}
}