GameManager.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. /// <summary>
  5. /// Central manager for game state, death, and respawn logic
  6. /// </summary>
  7. public class GameManager : MonoBehaviour
  8. {
  9. public static GameManager Instance { get; private set; }
  10. [Header("References")]
  11. public TeamCohesionManager cohesionManager;
  12. public CheckpointSystem checkpointSystem;
  13. public DeathScreenUI deathScreenUI;
  14. public GridMovement playerMovement;
  15. [Header("Death Settings")]
  16. public float deathDelay = 2f; // Time before showing death screen
  17. public float respawnFadeTime = 1f;
  18. public bool resetMonstersOnRespawn = false;
  19. [Header("Respawn Penalties (Optional)")]
  20. public bool applyGoldPenalty = false;
  21. public float goldPenaltyPercent = 0f; // 0 = no penalty
  22. private bool isPlayerDead = false;
  23. private CanvasGroup fadeCanvasGroup;
  24. private void Awake()
  25. {
  26. if (Instance != null && Instance != this)
  27. {
  28. Destroy(gameObject);
  29. return;
  30. }
  31. Instance = this;
  32. // Create fade overlay if it doesn't exist
  33. CreateFadeOverlay();
  34. }
  35. private void Start()
  36. {
  37. if (cohesionManager == null)
  38. cohesionManager = FindFirstObjectByType<TeamCohesionManager>();
  39. if (checkpointSystem == null)
  40. checkpointSystem = FindFirstObjectByType<CheckpointSystem>();
  41. if (deathScreenUI == null)
  42. deathScreenUI = FindFirstObjectByType<DeathScreenUI>();
  43. if (playerMovement == null)
  44. playerMovement = FindFirstObjectByType<GridMovement>();
  45. // Verify setup
  46. ValidateSetup();
  47. }
  48. private void ValidateSetup()
  49. {
  50. Debug.Log("=== GameManager Setup Validation ===");
  51. Debug.Log($"Cohesion Manager: {(cohesionManager != null ? "✓ Assigned" : "✗ MISSING")}");
  52. Debug.Log($"Checkpoint System: {(checkpointSystem != null ? "✓ Assigned" : "✗ MISSING")}");
  53. Debug.Log($"Death Screen UI: {(deathScreenUI != null ? "✓ Assigned" : "✗ MISSING")}");
  54. Debug.Log($"Player Movement: {(playerMovement != null ? "✓ Assigned" : "✗ MISSING")}");
  55. if (cohesionManager == null || checkpointSystem == null || deathScreenUI == null || playerMovement == null)
  56. {
  57. Debug.LogError("[GameManager] Some references are missing! Respawn system may not work correctly.");
  58. }
  59. else
  60. {
  61. Debug.Log("[GameManager] All references validated successfully!");
  62. }
  63. }
  64. /// <summary>
  65. /// Called when all party members have died
  66. /// </summary>
  67. public void OnPartyWiped()
  68. {
  69. if (isPlayerDead) return; // Prevent multiple triggers
  70. isPlayerDead = true;
  71. Debug.Log("[GameManager] Party wiped! Initiating death sequence...");
  72. StartCoroutine(HandleDeathSequence());
  73. }
  74. private IEnumerator HandleDeathSequence()
  75. {
  76. // Disable player input
  77. if (playerMovement != null)
  78. playerMovement.enabled = false;
  79. // Stop all monster attacks
  80. StopAllMonsterAttacks();
  81. // Wait a moment for dramatic effect
  82. yield return new WaitForSeconds(deathDelay);
  83. // Fade to black
  84. yield return StartCoroutine(FadeToBlack(respawnFadeTime));
  85. // Show death screen
  86. if (deathScreenUI != null)
  87. {
  88. deathScreenUI.Show();
  89. }
  90. else
  91. {
  92. Debug.LogWarning("[GameManager] Death Screen UI not found, auto-respawning...");
  93. RespawnParty();
  94. }
  95. }
  96. /// <summary>
  97. /// Respawn the party at the last checkpoint
  98. /// </summary>
  99. public void RespawnParty()
  100. {
  101. Debug.Log("[GameManager] Respawning party...");
  102. StartCoroutine(RespawnSequence());
  103. }
  104. private IEnumerator RespawnSequence()
  105. {
  106. Debug.Log("[GameManager] RespawnSequence started");
  107. // Hide death screen if visible
  108. if (deathScreenUI != null)
  109. {
  110. deathScreenUI.Hide();
  111. Debug.Log("[GameManager] Death screen hidden");
  112. }
  113. // Small delay for UI to hide
  114. yield return new WaitForSeconds(0.2f);
  115. // Load checkpoint data
  116. if (checkpointSystem != null)
  117. {
  118. Debug.Log("[GameManager] Loading checkpoint...");
  119. checkpointSystem.LoadCheckpoint();
  120. }
  121. else
  122. {
  123. Debug.LogWarning("[GameManager] No CheckpointSystem found, performing basic respawn");
  124. BasicRespawn();
  125. }
  126. // Apply penalties if enabled
  127. if (applyGoldPenalty && goldPenaltyPercent > 0)
  128. {
  129. ApplyGoldPenalty();
  130. }
  131. // Reset monsters if enabled
  132. if (resetMonstersOnRespawn)
  133. {
  134. ResetMonsters();
  135. }
  136. // Small delay before fade in
  137. yield return new WaitForSeconds(0.3f);
  138. Debug.Log("[GameManager] Starting fade from black...");
  139. // Fade back from black
  140. yield return StartCoroutine(FadeFromBlack(respawnFadeTime));
  141. Debug.Log("[GameManager] Fade complete, re-enabling player controls");
  142. // Re-enable player input
  143. if (playerMovement != null)
  144. playerMovement.enabled = true;
  145. isPlayerDead = false;
  146. Debug.Log("[GameManager] Respawn complete!");
  147. }
  148. /// <summary>
  149. /// Basic respawn without checkpoint system - restores party to full health
  150. /// </summary>
  151. private void BasicRespawn()
  152. {
  153. if (cohesionManager == null || cohesionManager.groupMembers == null)
  154. {
  155. Debug.LogError("[GameManager] Cannot respawn: CohesionManager or group members not found");
  156. return;
  157. }
  158. foreach (var character in cohesionManager.groupMembers)
  159. {
  160. character.currentHP = character.maxHP;
  161. character.currentFatigue = character.maxFatigue;
  162. character.currentMana = character.maxMana;
  163. // Update UI
  164. UIUpdater.Instance?.UpdateCharacterHP(character);
  165. UIUpdater.Instance?.UpdateCharacterFatigue(character);
  166. }
  167. }
  168. private void ApplyGoldPenalty()
  169. {
  170. // TODO: Implement gold system penalty if you have a gold/currency system
  171. Debug.Log($"[GameManager] Gold penalty applied: {goldPenaltyPercent}%");
  172. }
  173. private void StopAllMonsterAttacks()
  174. {
  175. // Find all monster formation groups and stop their attacks
  176. MonsterFormationGroup[] monsterGroups = FindObjectsByType<MonsterFormationGroup>(FindObjectsSortMode.None);
  177. foreach (var group in monsterGroups)
  178. {
  179. // Stop their current behavior immediately
  180. group.StopAllCoroutines();
  181. group.isChasing = false;
  182. group.hasDetectedPlayer = false;
  183. // Stop individual monster animations
  184. foreach (var monster in group.frontRow)
  185. {
  186. if (monster != null)
  187. monster.StopMove();
  188. }
  189. foreach (var monster in group.backRow)
  190. {
  191. if (monster != null)
  192. monster.StopMove();
  193. }
  194. Debug.Log($"[GameManager] Stopped attacks from monster group: {group.gameObject.name}");
  195. }
  196. }
  197. private void ResetMonsters()
  198. {
  199. // Find all monster formation groups and reset them
  200. MonsterFormationGroup[] monsterGroups = FindObjectsByType<MonsterFormationGroup>(FindObjectsSortMode.None);
  201. foreach (var group in monsterGroups)
  202. {
  203. // Stop their current behavior
  204. group.StopAllCoroutines();
  205. group.isChasing = false;
  206. group.hasDetectedPlayer = false;
  207. Debug.Log($"[GameManager] Reset monster group: {group.gameObject.name}");
  208. }
  209. }
  210. #region Fade Effects
  211. private void CreateFadeOverlay()
  212. {
  213. // Create a full-screen black overlay for fade effects
  214. GameObject fadeObj = new GameObject("FadeOverlay");
  215. fadeObj.transform.SetParent(transform);
  216. Canvas canvas = fadeObj.AddComponent<Canvas>();
  217. canvas.renderMode = RenderMode.ScreenSpaceOverlay;
  218. canvas.sortingOrder = 100; // Below death screen UI (which should be 200+)
  219. fadeCanvasGroup = fadeObj.AddComponent<CanvasGroup>();
  220. fadeCanvasGroup.alpha = 0f;
  221. fadeCanvasGroup.interactable = false;
  222. fadeCanvasGroup.blocksRaycasts = false;
  223. // Add black panel
  224. GameObject panel = new GameObject("BlackPanel");
  225. panel.transform.SetParent(fadeObj.transform, false);
  226. UnityEngine.UI.Image image = panel.AddComponent<UnityEngine.UI.Image>();
  227. image.color = Color.black;
  228. RectTransform rect = panel.GetComponent<RectTransform>();
  229. rect.anchorMin = Vector2.zero;
  230. rect.anchorMax = Vector2.one;
  231. rect.sizeDelta = Vector2.zero;
  232. }
  233. private IEnumerator FadeToBlack(float duration)
  234. {
  235. float elapsed = 0f;
  236. while (elapsed < duration)
  237. {
  238. elapsed += Time.deltaTime;
  239. fadeCanvasGroup.alpha = Mathf.Lerp(0f, 1f, elapsed / duration);
  240. yield return null;
  241. }
  242. fadeCanvasGroup.alpha = 1f;
  243. }
  244. private IEnumerator FadeFromBlack(float duration)
  245. {
  246. float elapsed = 0f;
  247. while (elapsed < duration)
  248. {
  249. elapsed += Time.deltaTime;
  250. fadeCanvasGroup.alpha = Mathf.Lerp(1f, 0f, elapsed / duration);
  251. yield return null;
  252. }
  253. fadeCanvasGroup.alpha = 0f;
  254. }
  255. #endregion
  256. public bool IsPlayerDead() => isPlayerDead;
  257. }