Files
zeling_v2/Assets/Scripts/UI/Menus/DeathScreenController.cs
2026-05-12 15:34:08 +08:00

48 lines
1.3 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using BaseGames.Core.Events;
namespace BaseGames.UI.Menus
{
public class DeathScreenController : MonoBehaviour
{
[SerializeField] private TMP_Text _deathMessage;
[SerializeField] private Button _btnRespawn;
[Header("Event Channels")]
[SerializeField] private VoidEventChannelSO _onPlayerDied;
[SerializeField] private VoidEventChannelSO _onDeathScreenConfirmed;
private readonly CompositeDisposable _subs = new();
private void OnEnable() => _onPlayerDied?.Subscribe(OnPlayerDied).AddTo(_subs);
private void OnDisable() => _subs.Clear();
private void OnPlayerDied() => StartCoroutine(ShowAfterDelay(1.5f));
private IEnumerator ShowAfterDelay(float delay)
{
yield return new WaitForSeconds(delay);
Show();
}
private void Show()
{
gameObject.SetActive(true);
if (_btnRespawn != null)
{
_btnRespawn.onClick.RemoveAllListeners();
_btnRespawn.onClick.AddListener(Confirm);
}
}
private void Confirm()
{
gameObject.SetActive(false);
_onDeathScreenConfirmed?.Raise();
}
}
}