| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- /// <summary>
- /// Central manager for game state, death, and respawn logic
- /// </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 Settings")]
- public float deathDelay = 2f; // Time before showing death screen
- public float respawnFadeTime = 1f;
- public bool resetMonstersOnRespawn = false;
-
- [Header("Respawn Penalties (Optional)")]
- public bool applyGoldPenalty = false;
- public float goldPenaltyPercent = 0f; // 0 = no penalty
-
- private bool isPlayerDead = false;
- private CanvasGroup fadeCanvasGroup;
- private void Awake()
- {
- if (Instance != null && Instance != this)
- {
- Destroy(gameObject);
- return;
- }
- Instance = this;
-
- // Create fade overlay if it doesn't exist
- CreateFadeOverlay();
- }
- private void Start()
- {
- if (cohesionManager == null)
- cohesionManager = FindFirstObjectByType<TeamCohesionManager>();
-
- if (checkpointSystem == null)
- checkpointSystem = FindFirstObjectByType<CheckpointSystem>();
-
- if (deathScreenUI == null)
- deathScreenUI = FindFirstObjectByType<DeathScreenUI>();
-
- if (playerMovement == null)
- playerMovement = FindFirstObjectByType<GridMovement>();
-
- // Verify setup
- ValidateSetup();
- }
-
- private void ValidateSetup()
- {
- Debug.Log("=== GameManager Setup Validation ===");
- Debug.Log($"Cohesion Manager: {(cohesionManager != null ? "✓ Assigned" : "✗ MISSING")}");
- Debug.Log($"Checkpoint System: {(checkpointSystem != null ? "✓ Assigned" : "✗ MISSING")}");
- Debug.Log($"Death Screen UI: {(deathScreenUI != null ? "✓ Assigned" : "✗ MISSING")}");
- Debug.Log($"Player Movement: {(playerMovement != null ? "✓ Assigned" : "✗ MISSING")}");
-
- if (cohesionManager == null || checkpointSystem == null || deathScreenUI == null || playerMovement == null)
- {
- Debug.LogError("[GameManager] Some references are missing! Respawn system may not work correctly.");
- }
- else
- {
- Debug.Log("[GameManager] All references validated successfully!");
- }
- }
- /// <summary>
- /// Called when all party members have died
- /// </summary>
- public void OnPartyWiped()
- {
- if (isPlayerDead) return; // Prevent multiple triggers
-
- isPlayerDead = true;
- Debug.Log("[GameManager] Party wiped! Initiating death sequence...");
-
- StartCoroutine(HandleDeathSequence());
- }
- private IEnumerator HandleDeathSequence()
- {
- // Disable player input
- if (playerMovement != null)
- playerMovement.enabled = false;
-
- // Stop all monster attacks
- StopAllMonsterAttacks();
-
- // Wait a moment for dramatic effect
- yield return new WaitForSeconds(deathDelay);
-
- // Fade to black
- yield return StartCoroutine(FadeToBlack(respawnFadeTime));
-
- // Show death screen
- if (deathScreenUI != null)
- {
- deathScreenUI.Show();
- }
- else
- {
- Debug.LogWarning("[GameManager] Death Screen UI not found, auto-respawning...");
- RespawnParty();
- }
- }
- /// <summary>
- /// Respawn the party at the last checkpoint
- /// </summary>
- public void RespawnParty()
- {
- Debug.Log("[GameManager] Respawning party...");
- StartCoroutine(RespawnSequence());
- }
- private IEnumerator RespawnSequence()
- {
- Debug.Log("[GameManager] RespawnSequence started");
-
- // Hide death screen if visible
- if (deathScreenUI != null)
- {
- deathScreenUI.Hide();
- Debug.Log("[GameManager] Death screen hidden");
- }
-
- // Small delay for UI to hide
- yield return new WaitForSeconds(0.2f);
-
- // Load checkpoint data
- if (checkpointSystem != null)
- {
- Debug.Log("[GameManager] Loading checkpoint...");
- checkpointSystem.LoadCheckpoint();
- }
- else
- {
- Debug.LogWarning("[GameManager] No CheckpointSystem found, performing basic respawn");
- BasicRespawn();
- }
-
- // Apply penalties if enabled
- if (applyGoldPenalty && goldPenaltyPercent > 0)
- {
- ApplyGoldPenalty();
- }
-
- // Reset monsters if enabled
- if (resetMonstersOnRespawn)
- {
- ResetMonsters();
- }
-
- // Small delay before fade in
- yield return new WaitForSeconds(0.3f);
-
- Debug.Log("[GameManager] Starting fade from black...");
-
- // Fade back from black
- yield return StartCoroutine(FadeFromBlack(respawnFadeTime));
-
- Debug.Log("[GameManager] Fade complete, re-enabling player controls");
-
- // Re-enable player input
- if (playerMovement != null)
- playerMovement.enabled = true;
-
- isPlayerDead = false;
-
- Debug.Log("[GameManager] Respawn complete!");
- }
- /// <summary>
- /// Basic respawn without checkpoint system - restores party to full health
- /// </summary>
- private void BasicRespawn()
- {
- if (cohesionManager == null || cohesionManager.groupMembers == null)
- {
- Debug.LogError("[GameManager] Cannot respawn: CohesionManager or group members not found");
- return;
- }
-
- foreach (var character in cohesionManager.groupMembers)
- {
- character.currentHP = character.maxHP;
- character.currentFatigue = character.maxFatigue;
- character.currentMana = character.maxMana;
-
- // Update UI
- UIUpdater.Instance?.UpdateCharacterHP(character);
- UIUpdater.Instance?.UpdateCharacterFatigue(character);
- }
- }
- private void ApplyGoldPenalty()
- {
- // TODO: Implement gold system penalty if you have a gold/currency system
- Debug.Log($"[GameManager] Gold penalty applied: {goldPenaltyPercent}%");
- }
- private void StopAllMonsterAttacks()
- {
- // Find all monster formation groups and stop their attacks
- MonsterFormationGroup[] monsterGroups = FindObjectsByType<MonsterFormationGroup>(FindObjectsSortMode.None);
-
- foreach (var group in monsterGroups)
- {
- // Stop their current behavior immediately
- group.StopAllCoroutines();
- group.isChasing = false;
- group.hasDetectedPlayer = false;
-
- // Stop individual monster animations
- foreach (var monster in group.frontRow)
- {
- if (monster != null)
- monster.StopMove();
- }
- foreach (var monster in group.backRow)
- {
- if (monster != null)
- monster.StopMove();
- }
-
- Debug.Log($"[GameManager] Stopped attacks from monster group: {group.gameObject.name}");
- }
- }
-
- private void ResetMonsters()
- {
- // Find all monster formation groups and reset them
- MonsterFormationGroup[] monsterGroups = FindObjectsByType<MonsterFormationGroup>(FindObjectsSortMode.None);
-
- foreach (var group in monsterGroups)
- {
- // Stop their current behavior
- group.StopAllCoroutines();
- group.isChasing = false;
- group.hasDetectedPlayer = false;
-
- Debug.Log($"[GameManager] Reset monster group: {group.gameObject.name}");
- }
- }
- #region Fade Effects
-
- private void CreateFadeOverlay()
- {
- // Create a full-screen black overlay for fade effects
- GameObject fadeObj = new GameObject("FadeOverlay");
- fadeObj.transform.SetParent(transform);
-
- Canvas canvas = fadeObj.AddComponent<Canvas>();
- canvas.renderMode = RenderMode.ScreenSpaceOverlay;
- canvas.sortingOrder = 100; // Below death screen UI (which should be 200+)
-
- fadeCanvasGroup = fadeObj.AddComponent<CanvasGroup>();
- fadeCanvasGroup.alpha = 0f;
- fadeCanvasGroup.interactable = false;
- fadeCanvasGroup.blocksRaycasts = false;
-
- // Add black panel
- 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
- public bool IsPlayerDead() => isPlayerDead;
- }
|