| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- using UnityEngine;
- using System.Collections.Generic;
- /// <summary>
- /// Manages checkpoint saving and loading for respawn system
- /// </summary>
- public class CheckpointSystem : MonoBehaviour
- {
- public static CheckpointSystem Instance { get; private set; }
- [Header("References")]
- public TeamCohesionManager cohesionManager;
- public Transform playerTransform;
- public GridMovement playerMovement;
-
- [Header("Checkpoint Settings")]
- public bool autoSaveOnCheckpoint = true;
- public float checkpointRadius = 3f;
-
- // Saved checkpoint data
- private Vector3 savedPlayerPosition;
- private Quaternion savedPlayerRotation;
- private int savedFloorLevel;
- private List<SavedCharacterData> savedCharacters = new List<SavedCharacterData>();
- private bool hasCheckpoint = false;
- [System.Serializable]
- private class SavedCharacterData
- {
- public CharacterInGroup character;
- public int hp;
- public int fatigue;
- public int mana;
- public int gridX;
- public int gridY;
-
- public SavedCharacterData(CharacterInGroup chara)
- {
- character = chara;
- hp = chara.currentHP;
- fatigue = chara.currentFatigue;
- mana = chara.currentMana;
- gridX = chara.gridX;
- gridY = chara.gridY;
- }
- }
- private void Awake()
- {
- if (Instance != null && Instance != this)
- {
- Destroy(gameObject);
- return;
- }
- Instance = this;
- }
- private void Start()
- {
- if (cohesionManager == null)
- cohesionManager = FindFirstObjectByType<TeamCohesionManager>();
-
- if (playerTransform == null)
- {
- GameObject player = GameObject.FindGameObjectWithTag("Player");
- if (player != null)
- playerTransform = player.transform;
- }
-
- if (playerMovement == null)
- playerMovement = FindFirstObjectByType<GridMovement>();
- }
- /// <summary>
- /// Save current game state as a checkpoint
- /// </summary>
- public void SaveCheckpoint()
- {
- if (playerTransform == null)
- {
- Debug.LogWarning("[CheckpointSystem] Cannot save checkpoint: playerTransform is null");
- return;
- }
-
- if (cohesionManager == null)
- {
- Debug.LogWarning("[CheckpointSystem] Cannot save checkpoint: cohesionManager is null");
- return;
- }
- // Save player position and rotation
- savedPlayerPosition = playerTransform.position;
- savedPlayerRotation = playerTransform.rotation;
-
- // Save floor level if available
- if (playerMovement != null)
- savedFloorLevel = playerMovement.currentFloorLevel;
-
- // Save all character data
- savedCharacters.Clear();
- foreach (var character in cohesionManager.groupMembers)
- {
- savedCharacters.Add(new SavedCharacterData(character));
- }
-
- hasCheckpoint = true;
-
- Debug.Log($"[CheckpointSystem] ✓ Checkpoint saved at {savedPlayerPosition} with {savedCharacters.Count} characters");
- }
- /// <summary>
- /// Save checkpoint at specific position
- /// </summary>
- public void SaveCheckpointAt(Vector3 position, Quaternion rotation)
- {
- savedPlayerPosition = position;
- savedPlayerRotation = rotation;
-
- if (playerMovement != null)
- savedFloorLevel = playerMovement.currentFloorLevel;
-
- // Save all character data
- savedCharacters.Clear();
- if (cohesionManager != null)
- {
- foreach (var character in cohesionManager.groupMembers)
- {
- savedCharacters.Add(new SavedCharacterData(character));
- }
- }
-
- hasCheckpoint = true;
-
- Debug.Log($"[CheckpointSystem] Manual checkpoint saved at {savedPlayerPosition}");
- }
- /// <summary>
- /// Load the last checkpoint and restore party
- /// </summary>
- public void LoadCheckpoint()
- {
- if (!hasCheckpoint)
- {
- Debug.LogWarning("[CheckpointSystem] No checkpoint to load, using default spawn");
- LoadDefaultSpawn();
- return;
- }
- Debug.Log("[CheckpointSystem] Loading checkpoint...");
- // Restore player position
- if (playerTransform != null)
- {
- playerTransform.position = savedPlayerPosition;
- playerTransform.rotation = savedPlayerRotation;
- }
-
- // Restore floor level
- if (playerMovement != null)
- playerMovement.currentFloorLevel = savedFloorLevel;
- // Restore all characters
- RestoreCharacters();
-
- Debug.Log($"[CheckpointSystem] Checkpoint loaded: {savedCharacters.Count} characters restored");
- }
- private void RestoreCharacters()
- {
- if (cohesionManager == null)
- {
- Debug.LogError("[CheckpointSystem] CohesionManager not found!");
- return;
- }
- // Clear any dead characters from the current party
- cohesionManager.groupMembers.Clear();
- // Restore all saved characters
- foreach (var saved in savedCharacters)
- {
- CharacterInGroup character = saved.character;
-
- // Restore stats to saved values (full health at checkpoint)
- character.currentHP = character.maxHP; // Always restore to full HP
- character.currentFatigue = character.maxFatigue; // Full stamina
- character.currentMana = character.maxMana; // Full mana
-
- // Restore grid position
- character.gridX = saved.gridX;
- character.gridY = saved.gridY;
-
- // Add back to party
- cohesionManager.groupMembers.Add(character);
-
- Debug.Log($"[CheckpointSystem] Restored character: {character.characterName} with {character.currentHP}/{character.maxHP} HP");
- }
- // Recreate UI for all characters
- RecreatePartyUI();
-
- Debug.Log($"[CheckpointSystem] Finished restoring {savedCharacters.Count} characters");
- }
- /// <summary>
- /// Recreate the party UI after respawn
- /// </summary>
- private void RecreatePartyUI()
- {
- Debug.Log("[CheckpointSystem] Recreating party UI...");
-
- PartyUIManager partyUI = FindFirstObjectByType<PartyUIManager>();
- if (partyUI != null)
- {
- // Rebuild UI for all characters using existing method
- partyUI.DisplayPartyUI();
- Debug.Log("[CheckpointSystem] Party UI recreated successfully");
- }
- else
- {
- Debug.LogWarning("[CheckpointSystem] PartyUIManager not found, cannot recreate UI");
- }
- }
- /// <summary>
- /// Load default spawn point if no checkpoint exists
- /// </summary>
- private void LoadDefaultSpawn()
- {
- // Use current position as default or find a spawn point
- GameObject spawnPoint = GameObject.FindGameObjectWithTag("SpawnPoint");
-
- if (spawnPoint != null && playerTransform != null)
- {
- playerTransform.position = spawnPoint.transform.position;
- playerTransform.rotation = spawnPoint.transform.rotation;
- Debug.Log("[CheckpointSystem] Loaded default spawn point");
- }
- else if (playerTransform != null)
- {
- // Just restore characters at current position
- Debug.Log("[CheckpointSystem] No spawn point, restoring at current position");
- }
- // Restore all party members to full health
- if (cohesionManager != null)
- {
- 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);
- }
- }
- }
- /// <summary>
- /// Check if player is near a checkpoint trigger
- /// </summary>
- private void OnTriggerEnter(Collider other)
- {
- if (other.CompareTag("Checkpoint") && autoSaveOnCheckpoint)
- {
- SaveCheckpoint();
-
- // Optional: Show checkpoint saved message
- Debug.Log("[CheckpointSystem] Checkpoint reached!");
- }
- }
- public bool HasCheckpoint() => hasCheckpoint;
-
- public Vector3 GetCheckpointPosition() => savedPlayerPosition;
- }
|