SaveLoad using managed lifecycle
This commit is contained in:
committed by
Michal Pikulski
parent
3e835ed3b8
commit
b932be2232
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using AppleHills.Data.CardSystem;
|
||||
@@ -11,14 +11,19 @@ namespace Data.CardSystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages the player's card collection, booster packs, and related operations.
|
||||
/// Uses a singleton pattern for global access.
|
||||
/// Implements ISaveParticipant to integrate with the save/load system.
|
||||
/// Manages the card collection system for the game.
|
||||
/// Handles unlocking cards, tracking collections, and integrating with the save/load system.
|
||||
/// </summary>
|
||||
public class CardSystemManager : ManagedBehaviour, ISaveParticipant
|
||||
public class CardSystemManager : ManagedBehaviour
|
||||
{
|
||||
private static CardSystemManager _instance;
|
||||
public static CardSystemManager Instance => _instance;
|
||||
|
||||
// Save system configuration
|
||||
public override bool AutoRegisterForSave => true;
|
||||
public override string SaveId => "CardSystemManager";
|
||||
|
||||
|
||||
[Header("Card Collection")]
|
||||
[SerializeField] private List<CardDefinition> availableCards = new List<CardDefinition>();
|
||||
|
||||
@@ -90,17 +95,6 @@ namespace Data.CardSystem
|
||||
BuildDefinitionLookup();
|
||||
|
||||
Logging.Debug($"[CardSystemManager] Loaded {availableCards.Count} card definitions from Addressables");
|
||||
|
||||
// NOW register with save/load system (definitions are ready for state restoration)
|
||||
if (SaveLoadManager.Instance != null)
|
||||
{
|
||||
SaveLoadManager.Instance.RegisterParticipant(this);
|
||||
Logging.Debug("[CardSystemManager] Registered with SaveLoadManager after definitions loaded");
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Warning("[CardSystemManager] SaveLoadManager not available for registration");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -108,14 +102,6 @@ namespace Data.CardSystem
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
// Unregister from save/load system
|
||||
if (SaveLoadManager.Instance != null)
|
||||
{
|
||||
SaveLoadManager.Instance.UnregisterParticipant(GetSaveId());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds a lookup dictionary for quick access to card definitions by ID
|
||||
@@ -703,42 +689,19 @@ namespace Data.CardSystem
|
||||
}
|
||||
}
|
||||
|
||||
#region ISaveParticipant Implementation
|
||||
#region Save/Load Lifecycle Hooks
|
||||
|
||||
private bool hasBeenRestored;
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if this participant has already had its state restored.
|
||||
/// </summary>
|
||||
public bool HasBeenRestored => hasBeenRestored;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the unique save ID for the CardSystemManager.
|
||||
/// Since this is a singleton global system, the ID is constant.
|
||||
/// </summary>
|
||||
public string GetSaveId()
|
||||
{
|
||||
return "CardSystemManager";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the current card collection state to JSON.
|
||||
/// </summary>
|
||||
public string SerializeState()
|
||||
protected override string OnGlobalSaveRequested()
|
||||
{
|
||||
var state = ExportCardCollectionState();
|
||||
return JsonUtility.ToJson(state);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Restores the card collection state from serialized JSON data.
|
||||
/// </summary>
|
||||
public void RestoreState(string serializedData)
|
||||
protected override void OnGlobalRestoreRequested(string serializedData)
|
||||
{
|
||||
if (string.IsNullOrEmpty(serializedData))
|
||||
{
|
||||
Logging.Debug("[CardSystemManager] No saved state to restore, using defaults");
|
||||
hasBeenRestored = true;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -748,7 +711,6 @@ namespace Data.CardSystem
|
||||
if (state != null)
|
||||
{
|
||||
ApplyCardCollectionState(state);
|
||||
hasBeenRestored = true;
|
||||
Logging.Debug("[CardSystemManager] Successfully restored card collection state");
|
||||
}
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user