- Исправил мелкие баки в поведенчеких скриптах - Добавил CreatureAnimationEvents.cs для унификации поведения анимационных ивентов игровых сущностей - Добавил VFX для смерти врагов - Добавил пул VFX, пока много багов, UNSTABLE - Настроил исользование пула в анимационных скриптах
148 lines
5.0 KiB
C#
148 lines
5.0 KiB
C#
using System;
|
||
using System.Collections;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using UnityEngine.InputSystem;
|
||
using UnityEngine.EventSystems;
|
||
|
||
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 OnDestroy()
|
||
{
|
||
// ReSharper disable once EventUnsubscriptionViaAnonymousDelegate
|
||
if (close) close.action.performed -= _ => GameManager.Instance.TogglePause();
|
||
}
|
||
|
||
private void Awake()
|
||
{
|
||
if (resumeButton) resumeButton.onClick.AddListener(OnResumeClicked);
|
||
if (quitButton) quitButton.onClick.AddListener(OnQuitClicked);
|
||
//if (restartButton) restartButton.onClick.AddListener(OnRestartClicked);
|
||
|
||
// Изначально скрыто
|
||
SetVisible(false, true);
|
||
|
||
if (close) close.action.performed += _ => GameManager.Instance.TogglePause();
|
||
}
|
||
|
||
// private void Update()
|
||
// {
|
||
// if (close.action.WasPressedThisFrame() && !GameManager.Instance.IsPaused)
|
||
// GameManager.Instance.TogglePause();
|
||
// }
|
||
|
||
/// <summary>
|
||
/// Обработка события паузы
|
||
/// </summary>
|
||
/// <param name="paused">Ставить ли на паузу</param>
|
||
private void HandlePauseChanged(bool paused)
|
||
{
|
||
if (!isActive) return;
|
||
|
||
if (paused)
|
||
{
|
||
EventSystem.current.firstSelectedGameObject = resumeButton.gameObject;
|
||
EventSystem.current.SetSelectedGameObject(resumeButton.gameObject);
|
||
}
|
||
|
||
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 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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Метод для возобновления игры
|
||
/// </summary>
|
||
public static void OnResumeClicked() => GameManager.Instance.TogglePause();
|
||
|
||
//public void OnRestartClicked()
|
||
//{
|
||
// GameManager.Instance.SetPause(false);
|
||
// GameManager.Instance.ResetLevel();
|
||
//}
|
||
|
||
/// <summary>
|
||
/// Метод для выхода в главное меню
|
||
/// </summary>
|
||
public void OnQuitClicked()
|
||
{
|
||
GameManager.Instance.SetPause(false);
|
||
GameManager.Instance.LoadScene("MainMenu", false);
|
||
}
|
||
}
|
||
} |