| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- using UnityEngine;
- using System.Collections;
- /// <summary>
- /// Central manager for game state, death, and respawn logic.
- /// Handles party wipe detection, death sequence, and respawn coordination.
- /// </summary>
- public class GameManager : MonoBehaviour
- {
- public static GameManager Instance { get; private set; }
- [Header("References")]
- public TeamCohesionManager cohesionManager;
- public CheckpointSystem checkpointSystem;
- public DeathScreenUI deathScreenUI;
- public GridMovement playerMovement;
-
- [Header("Death & Respawn Settings")]
- public float deathDelay = 2f;
- public float respawnFadeTime = 1f;
- public bool resetMonstersOnRespawn = false;
-
- [Header("Optional Penalties")]
- public bool applyGoldPenalty = false;
- public float goldPenaltyPercent = 0f;
-
- private bool isPlayerDead = false;
- private CanvasGroup fadeCanvasGroup;
- #region Initialization
- private void Awake()
- {
- if (Instance != null && Instance != this)
- {
- Destroy(gameObject);
- return;
- }
- Instance = this;
-
- CreateFadeOverlay();
- }
- private void Start()
- {
- FindReferences();
- ValidateSetup();
- }
- private void FindReferences()
- {
- if (cohesionManager == null)
- cohesionManager = FindFirstObjectByType<TeamCohesionManager>();
-
- if (checkpointSystem == null)
- checkpointSystem = FindFirstObjectByType<CheckpointSystem>();
-
- if (deathScreenUI == null)
- deathScreenUI = FindFirstObjectByType<DeathScreenUI>();
-
- if (playerMovement == null)
- playerMovement = FindFirstObjectByType<GridMovement>();
- }
-
- private void ValidateSetup()
- {
- bool hasAllReferences = cohesionManager != null && checkpointSystem != null &&
- deathScreenUI != null && playerMovement != null;
-
- if (!hasAllReferences)
- {
- Debug.LogError("[GameManager] Missing references! Respawn system may not work.");
- }
- }
- #endregion
- #region Death & Respawn
- public void OnPartyWiped()
- {
- if (isPlayerDead) return;
-
- isPlayerDead = true;
- StartCoroutine(HandleDeathSequence());
- }
- private IEnumerator HandleDeathSequence()
- {
- if (playerMovement != null)
- playerMovement.enabled = false;
-
- StopAllMonsterAttacks();
-
- yield return new WaitForSeconds(deathDelay);
- yield return StartCoroutine(FadeToBlack(respawnFadeTime));
-
- if (deathScreenUI != null)
- {
- deathScreenUI.Show();
- }
- else
- {
- Debug.LogWarning("[GameManager] Death Screen UI not found, auto-respawning");
- RespawnParty();
- }
- }
- public void RespawnParty()
- {
- StartCoroutine(RespawnSequence());
- }
- private IEnumerator RespawnSequence()
- {
- if (deathScreenUI != null)
- deathScreenUI.Hide();
-
- yield return new WaitForSeconds(0.2f);
-
- if (checkpointSystem != null)
- {
- checkpointSystem.LoadCheckpoint();
- }
- else
- {
- Debug.LogWarning("[GameManager] CheckpointSystem not found, basic respawn");
- BasicRespawn();
- }
-
- if (applyGoldPenalty && goldPenaltyPercent > 0)
- ApplyGoldPenalty();
-
- if (resetMonstersOnRespawn)
- ResetMonsters();
-
- yield return new WaitForSeconds(0.3f);
- yield return StartCoroutine(FadeFromBlack(respawnFadeTime));
-
- if (playerMovement != null)
- playerMovement.enabled = true;
-
- isPlayerDead = false;
- }
- private void BasicRespawn()
- {
- if (cohesionManager == null || cohesionManager.groupMembers == null) return;
-
- foreach (var character in cohesionManager.groupMembers)
- {
- character.currentHP = character.maxHP;
- character.currentFatigue = character.maxFatigue;
- character.currentMana = character.maxMana;
-
- UIUpdater.Instance?.UpdateCharacterHP(character);
- UIUpdater.Instance?.UpdateCharacterFatigue(character);
- }
- }
- #endregion
- #region Monster Management
- private void StopAllMonsterAttacks()
- {
- MonsterFormationGroup[] monsterGroups = FindObjectsByType<MonsterFormationGroup>(FindObjectsSortMode.None);
-
- foreach (var group in monsterGroups)
- {
- group.StopAllCoroutines();
- group.isChasing = false;
- group.hasDetectedPlayer = false;
-
- foreach (var monster in group.frontRow)
- {
- if (monster != null)
- monster.StopMove();
- }
-
- foreach (var monster in group.backRow)
- {
- if (monster != null)
- monster.StopMove();
- }
- }
- }
-
- private void ResetMonsters()
- {
- MonsterFormationGroup[] monsterGroups = FindObjectsByType<MonsterFormationGroup>(FindObjectsSortMode.None);
-
- foreach (var group in monsterGroups)
- {
- group.StopAllCoroutines();
- group.isChasing = false;
- group.hasDetectedPlayer = false;
- }
- }
- private void ApplyGoldPenalty()
- {
- // TODO: Implement gold penalty when currency system exists
- }
- #endregion
- #region Fade Effects
-
- private void CreateFadeOverlay()
- {
- GameObject fadeObj = new GameObject("FadeOverlay");
- fadeObj.transform.SetParent(transform);
-
- Canvas canvas = fadeObj.AddComponent<Canvas>();
- canvas.renderMode = RenderMode.ScreenSpaceOverlay;
- canvas.sortingOrder = 100;
-
- fadeCanvasGroup = fadeObj.AddComponent<CanvasGroup>();
- fadeCanvasGroup.alpha = 0f;
- fadeCanvasGroup.interactable = false;
- fadeCanvasGroup.blocksRaycasts = false;
-
- GameObject panel = new GameObject("BlackPanel");
- panel.transform.SetParent(fadeObj.transform, false);
-
- UnityEngine.UI.Image image = panel.AddComponent<UnityEngine.UI.Image>();
- image.color = Color.black;
-
- RectTransform rect = panel.GetComponent<RectTransform>();
- rect.anchorMin = Vector2.zero;
- rect.anchorMax = Vector2.one;
- rect.sizeDelta = Vector2.zero;
- }
- private IEnumerator FadeToBlack(float duration)
- {
- float elapsed = 0f;
- while (elapsed < duration)
- {
- elapsed += Time.deltaTime;
- fadeCanvasGroup.alpha = Mathf.Lerp(0f, 1f, elapsed / duration);
- yield return null;
- }
- fadeCanvasGroup.alpha = 1f;
- }
- private IEnumerator FadeFromBlack(float duration)
- {
- float elapsed = 0f;
- while (elapsed < duration)
- {
- elapsed += Time.deltaTime;
- fadeCanvasGroup.alpha = Mathf.Lerp(1f, 0f, elapsed / duration);
- yield return null;
- }
- fadeCanvasGroup.alpha = 0f;
- }
-
- #endregion
- #region Public API
- public bool IsPlayerDead() => isPlayerDead;
- #endregion
- }
|