Fome further save load work
This commit is contained in:
@@ -100,6 +100,8 @@ public class FollowerController : MonoBehaviour, ISaveParticipant
|
||||
|
||||
// Save system tracking
|
||||
private bool hasBeenRestored;
|
||||
private bool _hasRestoredHeldItem; // Track if held item restoration completed
|
||||
private string _expectedHeldItemSaveId; // Expected saveId during restoration
|
||||
|
||||
void Awake()
|
||||
{
|
||||
@@ -785,10 +787,11 @@ public class FollowerController : MonoBehaviour, ISaveParticipant
|
||||
transform.position = saveData.worldPosition;
|
||||
transform.rotation = saveData.worldRotation;
|
||||
|
||||
// Restore held item if any
|
||||
// Try bilateral restoration of held item
|
||||
if (!string.IsNullOrEmpty(saveData.heldItemSaveId))
|
||||
{
|
||||
RestoreHeldItem(saveData.heldItemSaveId, saveData.heldItemDataAssetPath);
|
||||
_expectedHeldItemSaveId = saveData.heldItemSaveId;
|
||||
TryRestoreHeldItem(saveData.heldItemSaveId, saveData.heldItemDataAssetPath);
|
||||
}
|
||||
|
||||
hasBeenRestored = true;
|
||||
@@ -801,48 +804,110 @@ public class FollowerController : MonoBehaviour, ISaveParticipant
|
||||
}
|
||||
}
|
||||
|
||||
private void RestoreHeldItem(string heldItemSaveId, string heldItemDataAssetPath)
|
||||
/// <summary>
|
||||
/// Bilateral restoration: Follower tries to find and claim the held item.
|
||||
/// If pickup doesn't exist yet, it will try to claim us when it restores.
|
||||
/// </summary>
|
||||
private void TryRestoreHeldItem(string heldItemSaveId, string heldItemDataAssetPath)
|
||||
{
|
||||
// Try to find the item by its save ID using ItemManager
|
||||
GameObject heldObject = ItemManager.Instance?.FindPickupBySaveId(heldItemSaveId);
|
||||
|
||||
if (heldObject == null)
|
||||
if (_hasRestoredHeldItem)
|
||||
{
|
||||
Logging.Warning($"[FollowerController] Could not find held item with save ID: {heldItemSaveId}");
|
||||
Logging.Debug("[FollowerController] Held item already restored");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the item data
|
||||
PickupItemData heldData = null;
|
||||
#if UNITY_EDITOR
|
||||
if (!string.IsNullOrEmpty(heldItemDataAssetPath))
|
||||
// Try to find the pickup immediately
|
||||
GameObject heldObject = ItemManager.Instance?.FindPickupBySaveId(heldItemSaveId);
|
||||
|
||||
if (heldObject == null)
|
||||
{
|
||||
heldData = UnityEditor.AssetDatabase.LoadAssetAtPath<PickupItemData>(heldItemDataAssetPath);
|
||||
Logging.Debug($"[FollowerController] Held item not found yet: {heldItemSaveId}, waiting for pickup to restore");
|
||||
return; // Pickup will find us when it restores
|
||||
}
|
||||
|
||||
var pickup = heldObject.GetComponent<Pickup>();
|
||||
if (pickup == null)
|
||||
{
|
||||
Logging.Warning($"[FollowerController] Found object but no Pickup component: {heldItemSaveId}");
|
||||
return;
|
||||
}
|
||||
|
||||
// Claim the pickup
|
||||
TakeOwnership(pickup, heldItemDataAssetPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bilateral restoration entry point: Pickup calls this to offer itself to the Follower.
|
||||
/// Returns true if claim was successful, false if Follower already has an item or wrong pickup.
|
||||
/// </summary>
|
||||
public bool TryClaimHeldItem(Pickup pickup)
|
||||
{
|
||||
if (pickup == null)
|
||||
return false;
|
||||
|
||||
if (_hasRestoredHeldItem)
|
||||
{
|
||||
Logging.Debug("[FollowerController] Already restored held item, rejecting claim");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Verify this is the expected pickup
|
||||
if (pickup is SaveableInteractable saveable)
|
||||
{
|
||||
if (saveable.GetSaveId() != _expectedHeldItemSaveId)
|
||||
{
|
||||
Logging.Warning($"[FollowerController] Pickup tried to claim but saveId mismatch: {saveable.GetSaveId()} != {_expectedHeldItemSaveId}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Claim the pickup
|
||||
TakeOwnership(pickup, null);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes ownership of a pickup during restoration. Called by both restoration paths.
|
||||
/// </summary>
|
||||
private void TakeOwnership(Pickup pickup, string itemDataAssetPath)
|
||||
{
|
||||
if (_hasRestoredHeldItem)
|
||||
return; // Already claimed
|
||||
|
||||
// Get the item data
|
||||
PickupItemData heldData = pickup.itemData;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// Try loading from asset path if available and pickup doesn't have data
|
||||
if (heldData == null && !string.IsNullOrEmpty(itemDataAssetPath))
|
||||
{
|
||||
heldData = UnityEditor.AssetDatabase.LoadAssetAtPath<PickupItemData>(itemDataAssetPath);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (heldData == null)
|
||||
{
|
||||
var pickup = heldObject.GetComponent<Pickup>();
|
||||
if (pickup != null)
|
||||
{
|
||||
heldData = pickup.itemData;
|
||||
}
|
||||
Logging.Warning($"[FollowerController] Could not get item data for pickup: {pickup.gameObject.name}");
|
||||
return;
|
||||
}
|
||||
|
||||
// Restore the held item state
|
||||
if (heldData != null)
|
||||
{
|
||||
var pickup = heldObject.GetComponent<Pickup>();
|
||||
if (pickup != null)
|
||||
{
|
||||
_cachedPickupObject = heldObject;
|
||||
_cachedPickupObject.SetActive(false); // Held items should be hidden
|
||||
SetHeldItem(heldData, pickup.iconRenderer);
|
||||
_animator.SetBool("IsCarrying", true);
|
||||
Logging.Debug($"[FollowerController] Restored held item: {heldData.itemName}");
|
||||
}
|
||||
}
|
||||
// Setup the held item
|
||||
_cachedPickupObject = pickup.gameObject;
|
||||
_cachedPickupObject.SetActive(false); // Held items should be hidden
|
||||
SetHeldItem(heldData, pickup.iconRenderer);
|
||||
_animator.SetBool("IsCarrying", true);
|
||||
_hasRestoredHeldItem = true;
|
||||
|
||||
Logging.Debug($"[FollowerController] Successfully restored held item: {heldData.itemName}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Static method to find the FollowerController instance in the scene.
|
||||
/// Used by Pickup during bilateral restoration.
|
||||
/// </summary>
|
||||
public static FollowerController FindInstance()
|
||||
{
|
||||
return FindObjectOfType<FollowerController>();
|
||||
}
|
||||
|
||||
#endregion ISaveParticipant Implementation
|
||||
|
||||
Reference in New Issue
Block a user