SaveLoad using managed lifecycle

This commit is contained in:
Michal Pikulski
2025-11-04 20:01:27 +01:00
committed by Michal Pikulski
parent 3e835ed3b8
commit b932be2232
19 changed files with 1042 additions and 627 deletions

View File

@@ -13,6 +13,8 @@ namespace Interactions
{
public bool isPickedUp;
public bool wasHeldByFollower;
public bool wasInSlot; // NEW: Was this pickup in a slot?
public string slotSaveId; // NEW: Which slot held this pickup?
public Vector3 worldPosition;
public Quaternion worldRotation;
public bool isActive;
@@ -24,9 +26,12 @@ namespace Interactions
public SpriteRenderer iconRenderer;
public bool IsPickedUp { get; internal set; }
// Track which slot owns this pickup (for bilateral restoration)
internal ItemSlot OwningSlot { get; set; }
public event Action<PickupItemData> OnItemPickedUp;
public event Action<PickupItemData, PickupItemData, PickupItemData> OnItemsCombined;
protected override void Awake()
{
base.Awake(); // Register with save system
@@ -36,19 +41,17 @@ namespace Interactions
ApplyItemData();
}
protected override void Start()
{
base.Start(); // Register with save system
// Always register with ItemManager, even if picked up
// Always register with ItemManager, even if picked up
// This allows the save/load system to find held items when restoring state
protected override void OnManagedAwake()
{
ItemManager.Instance?.RegisterPickup(this);
}
protected override void OnDestroy()
{
base.OnDestroy(); // Unregister from save system
base.OnDestroy();
// Unregister from ItemManager
ItemManager.Instance?.UnregisterPickup(this);
@@ -139,10 +142,16 @@ namespace Interactions
// Check if this pickup is currently held by the follower
bool isHeldByFollower = IsPickedUp && !gameObject.activeSelf && transform.parent != null;
// Check if this pickup is in a slot
bool isInSlot = OwningSlot != null;
string slotId = isInSlot && OwningSlot is SaveableInteractable saveableSlot ? saveableSlot.SaveId : "";
return new PickupSaveData
{
isPickedUp = this.IsPickedUp,
wasHeldByFollower = isHeldByFollower,
wasInSlot = isInSlot,
slotSaveId = slotId,
worldPosition = transform.position,
worldRotation = transform.rotation,
isActive = gameObject.activeSelf
@@ -177,6 +186,20 @@ namespace Interactions
follower.TryClaimHeldItem(this);
}
}
// If this was in a slot, try bilateral restoration with the slot
else if (data.wasInSlot && !string.IsNullOrEmpty(data.slotSaveId))
{
// Try to give this pickup to the slot
var slot = FindSlotBySaveId(data.slotSaveId);
if (slot != null)
{
slot.TryClaimSlottedItem(this);
}
else
{
Debug.LogWarning($"[Pickup] Could not find slot with SaveId: {data.slotSaveId}");
}
}
}
else
{
@@ -190,6 +213,28 @@ namespace Interactions
// This prevents duplicate logic execution
}
/// <summary>
/// Find an ItemSlot by its SaveId (for bilateral restoration).
/// </summary>
private ItemSlot FindSlotBySaveId(string slotSaveId)
{
if (string.IsNullOrEmpty(slotSaveId)) return null;
// Get all ItemSlots from ItemManager
var allSlots = ItemManager.Instance?.GetAllItemSlots();
if (allSlots == null) return null;
foreach (var slot in allSlots)
{
if (slot is SaveableInteractable saveable && saveable.SaveId == slotSaveId)
{
return slot;
}
}
return null;
}
/// <summary>
/// Resets the pickup state when the item is dropped back into the world.
/// Called by FollowerController when swapping items.