DeathScreenUI.cs 2.6 KB

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