DeathScreenUI.cs 3.3 KB

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