DeathScreenUI.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using TMPro;
  4. /// <summary>
  5. /// Manages the death screen UI display and respawn button.
  6. /// Shows random death messages and handles respawn button clicks.
  7. /// </summary>
  8. public class DeathScreenUI : MonoBehaviour
  9. {
  10. public static DeathScreenUI Instance { get; private set; }
  11. [Header("UI References")]
  12. public GameObject deathPanel;
  13. public TextMeshProUGUI deathMessageText;
  14. public Button respawnButton;
  15. public CanvasGroup canvasGroup;
  16. [Header("Death Messages")]
  17. public string[] deathMessages = new string[]
  18. {
  19. "Your party has been defeated...",
  20. "Darkness consumes your party...",
  21. "The dungeon claims another group of heroes...",
  22. "Your quest has come to a tragic end..."
  23. };
  24. [Header("Settings")]
  25. public float fadeInDuration = 1f;
  26. public bool randomizeMessage = true;
  27. #region Initialization
  28. private void Awake()
  29. {
  30. if (Instance != null && Instance != this)
  31. {
  32. Destroy(gameObject);
  33. return;
  34. }
  35. Instance = this;
  36. if (deathPanel != null)
  37. deathPanel.SetActive(false);
  38. if (canvasGroup != null)
  39. canvasGroup.alpha = 0f;
  40. }
  41. private void Start()
  42. {
  43. ValidateReferences();
  44. BindButton();
  45. }
  46. private void ValidateReferences()
  47. {
  48. if (deathPanel == null)
  49. Debug.LogWarning("[DeathScreenUI] Death Panel not assigned in Inspector");
  50. if (respawnButton == null)
  51. Debug.LogWarning("[DeathScreenUI] Respawn Button not assigned in Inspector");
  52. }
  53. private void BindButton()
  54. {
  55. if (respawnButton != null)
  56. {
  57. respawnButton.onClick.AddListener(OnRespawnButtonClicked);
  58. }
  59. }
  60. #endregion
  61. #region Show/Hide
  62. public void Show()
  63. {
  64. if (deathPanel != null)
  65. deathPanel.SetActive(true);
  66. if (deathMessageText != null && randomizeMessage)
  67. {
  68. string message = deathMessages[Random.Range(0, deathMessages.Length)];
  69. deathMessageText.text = message;
  70. }
  71. if (canvasGroup != null)
  72. {
  73. StartCoroutine(FadeIn());
  74. }
  75. }
  76. public void Hide()
  77. {
  78. if (canvasGroup != null)
  79. {
  80. canvasGroup.alpha = 0f;
  81. }
  82. if (deathPanel != null)
  83. deathPanel.SetActive(false);
  84. }
  85. private System.Collections.IEnumerator FadeIn()
  86. {
  87. float elapsed = 0f;
  88. while (elapsed < fadeInDuration)
  89. {
  90. elapsed += Time.deltaTime;
  91. canvasGroup.alpha = Mathf.Lerp(0f, 1f, elapsed / fadeInDuration);
  92. yield return null;
  93. }
  94. canvasGroup.alpha = 1f;
  95. }
  96. #endregion
  97. #region Button Handlers
  98. private void OnRespawnButtonClicked()
  99. {
  100. var gameManagerObj = GameObject.Find("GameManager");
  101. if (gameManagerObj != null)
  102. {
  103. gameManagerObj.SendMessage("RespawnParty", SendMessageOptions.DontRequireReceiver);
  104. }
  105. else
  106. {
  107. Debug.LogError("[DeathScreenUI] GameManager not found!");
  108. }
  109. }
  110. #endregion
  111. }