SaveLoad using managed lifecycle
This commit is contained in:
committed by
Michal Pikulski
parent
3e835ed3b8
commit
b932be2232
@@ -2,6 +2,7 @@
|
||||
using Interactions;
|
||||
using UnityEngine;
|
||||
using Core;
|
||||
using Core.Lifecycle;
|
||||
|
||||
namespace PuzzleS
|
||||
{
|
||||
@@ -9,7 +10,7 @@ namespace PuzzleS
|
||||
/// Manages the state and interactions for a single puzzle step, including unlock/lock logic and event handling.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(InteractableBase))]
|
||||
public class ObjectiveStepBehaviour : MonoBehaviour, IPuzzlePrompt
|
||||
public class ObjectiveStepBehaviour : ManagedBehaviour, IPuzzlePrompt
|
||||
{
|
||||
/// <summary>
|
||||
/// The data object representing this puzzle step.
|
||||
@@ -31,8 +32,10 @@ namespace PuzzleS
|
||||
// Enum for tracking proximity state (simplified to just Close and Far)
|
||||
public enum ProximityState { Close, Far }
|
||||
|
||||
void Awake()
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
|
||||
_interactable = GetComponent<InteractableBase>();
|
||||
|
||||
// Initialize the indicator if it exists, but ensure it's hidden initially
|
||||
@@ -57,6 +60,21 @@ namespace PuzzleS
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnManagedAwake()
|
||||
{
|
||||
base.OnManagedAwake();
|
||||
|
||||
// Register with PuzzleManager - safe to access .Instance here
|
||||
if (stepData != null && PuzzleManager.Instance != null)
|
||||
{
|
||||
PuzzleManager.Instance.RegisterStepBehaviour(this);
|
||||
}
|
||||
else if (stepData == null)
|
||||
{
|
||||
Logging.Warning($"[Puzzles] Cannot register step on {gameObject.name}: stepData is null");
|
||||
}
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
if (_interactable == null)
|
||||
@@ -68,28 +86,19 @@ namespace PuzzleS
|
||||
_interactable.interactionComplete.AddListener(OnInteractionComplete);
|
||||
}
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
// Simply register with the PuzzleManager
|
||||
// The manager will handle state updates appropriately based on whether data is loaded
|
||||
if (stepData != null && PuzzleManager.Instance != null)
|
||||
{
|
||||
PuzzleManager.Instance.RegisterStepBehaviour(this);
|
||||
}
|
||||
else if (stepData == null)
|
||||
{
|
||||
Logging.Warning($"[Puzzles] Cannot register step on {gameObject.name}: stepData is null");
|
||||
}
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
if (_interactable != null)
|
||||
{
|
||||
_interactable.interactionStarted.RemoveListener(OnInteractionStarted);
|
||||
_interactable.interactionComplete.RemoveListener(OnInteractionComplete);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDestroy()
|
||||
{
|
||||
base.OnDestroy();
|
||||
|
||||
if (PuzzleManager.Instance != null && stepData != null)
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user