SaveLoad using managed lifecycle

This commit is contained in:
Michal Pikulski
2025-11-04 20:01:27 +01:00
parent 379a033d6b
commit bb68d1fd31
23 changed files with 1083 additions and 648 deletions

View File

@@ -7,7 +7,6 @@ using UnityEngine.SceneManagement;
using AppleHills.Core.Settings;
using Core;
using Core.Lifecycle;
using Core.SaveLoad;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using Utils;
@@ -28,7 +27,7 @@ namespace PuzzleS
/// <summary>
/// Manages puzzle step registration, dependency management, and step completion for the puzzle system.
/// </summary>
public class PuzzleManager : ManagedBehaviour, ISaveParticipant
public class PuzzleManager : ManagedBehaviour
{
private static PuzzleManager _instance;
@@ -49,6 +48,10 @@ namespace PuzzleS
// Store registered behaviors that are waiting for data to be loaded
private List<ObjectiveStepBehaviour> _registeredBehaviours = new List<ObjectiveStepBehaviour>();
// Save system configuration
public override bool AutoRegisterForSave => true;
public override string SaveId => $"{SceneManager.GetActiveScene().name}/PuzzleManager";
/// <summary>
/// Singleton instance of the PuzzleManager.
/// </summary>
@@ -66,7 +69,6 @@ namespace PuzzleS
// Save/Load restoration tracking
private bool _isDataRestored = false;
private bool _hasBeenRestored = false;
private List<ObjectiveStepBehaviour> _pendingRegistrations = new List<ObjectiveStepBehaviour>();
// Registration for ObjectiveStepBehaviour
@@ -74,12 +76,6 @@ namespace PuzzleS
// Track pending unlocks for steps that were unlocked before their behavior registered
private HashSet<string> _pendingUnlocks = new HashSet<string>();
/// <summary>
/// Returns true if this participant has already had its state restored.
/// Used by SaveLoadManager to prevent double-restoration.
/// </summary>
public bool HasBeenRestored => _hasBeenRestored;
public override int ManagedAwakePriority => 80; // Puzzle systems
@@ -108,10 +104,6 @@ namespace PuzzleS
LoadPuzzleDataForCurrentScene();
}
// Register with save/load system
SaveLoadManager.Instance.RegisterParticipant(this);
Logging.Debug("[PuzzleManager] Registered with SaveLoadManager");
// Subscribe to scene load events from SceneManagerService
// This is necessary because PuzzleManager is in DontDestroyOnLoad and won't receive OnSceneReady() callbacks
if (SceneManagerService.Instance != null)
@@ -558,21 +550,9 @@ namespace PuzzleS
return _isDataLoaded;
}
#region ISaveParticipant Implementation
#region Save/Load Lifecycle Hooks
/// <summary>
/// Get unique save ID for this puzzle manager instance
/// </summary>
public string GetSaveId()
{
string sceneName = SceneManager.GetActiveScene().name;
return $"{sceneName}/PuzzleManager";
}
/// <summary>
/// Serialize current puzzle state to JSON
/// </summary>
public string SerializeState()
protected override string OnSceneSaveRequested()
{
if (_currentLevelData == null)
{
@@ -592,16 +572,12 @@ namespace PuzzleS
return json;
}
/// <summary>
/// Restore puzzle state from serialized JSON data
/// </summary>
public void RestoreState(string data)
protected override void OnSceneRestoreRequested(string data)
{
if (string.IsNullOrEmpty(data) || data == "{}")
{
Logging.Debug("[PuzzleManager] No puzzle save data to restore");
_isDataRestored = true;
_hasBeenRestored = true;
return;
}
@@ -612,7 +588,6 @@ namespace PuzzleS
{
Logging.Warning("[PuzzleManager] Failed to deserialize puzzle save data");
_isDataRestored = true;
_hasBeenRestored = true;
return;
}
@@ -621,7 +596,6 @@ namespace PuzzleS
_unlockedSteps = new HashSet<string>(saveData.unlockedStepIds ?? new List<string>());
_isDataRestored = true;
_hasBeenRestored = true;
Logging.Debug($"[PuzzleManager] Restored puzzle state: {_completedSteps.Count} completed, {_unlockedSteps.Count} unlocked steps");
@@ -636,7 +610,6 @@ namespace PuzzleS
{
Debug.LogError($"[PuzzleManager] Error restoring puzzle state: {e.Message}");
_isDataRestored = true;
_hasBeenRestored = true;
}
}