WTF are you doing anna lyse?
This commit is contained in:
@@ -5,12 +5,26 @@ using UnityEngine.SceneManagement;
|
||||
using Utils;
|
||||
using AppleHills.Core.Settings;
|
||||
using Core;
|
||||
using Core.SaveLoad;
|
||||
using Bootstrap;
|
||||
using UnityEngine.Events;
|
||||
|
||||
/// <summary>
|
||||
/// Saveable data for FollowerController state
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class FollowerSaveData
|
||||
{
|
||||
public Vector3 worldPosition;
|
||||
public Quaternion worldRotation;
|
||||
public string heldItemSaveId; // Save ID of held pickup (if any)
|
||||
public string heldItemDataAssetPath; // Asset path to PickupItemData
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Controls the follower character, including following the player, handling pickups, and managing held items.
|
||||
/// </summary>
|
||||
public class FollowerController: MonoBehaviour
|
||||
public class FollowerController : MonoBehaviour, ISaveParticipant
|
||||
{
|
||||
private static readonly int CombineTrigger = Animator.StringToHash("Combine");
|
||||
|
||||
@@ -83,6 +97,9 @@ public class FollowerController: MonoBehaviour
|
||||
public UnityEvent PulverIsCombining;
|
||||
|
||||
private Input.PlayerTouchController _playerTouchController;
|
||||
|
||||
// Save system tracking
|
||||
private bool hasBeenRestored;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
@@ -103,6 +120,23 @@ public class FollowerController: MonoBehaviour
|
||||
// Initialize settings references
|
||||
_settings = GameManager.GetSettingsObject<IPlayerFollowerSettings>();
|
||||
_interactionSettings = GameManager.GetSettingsObject<IInteractionSettings>();
|
||||
|
||||
// Register for post-boot initialization
|
||||
BootCompletionService.RegisterInitAction(InitializePostBoot);
|
||||
}
|
||||
|
||||
private void InitializePostBoot()
|
||||
{
|
||||
// Register with save system after boot
|
||||
if (SaveLoadManager.Instance != null)
|
||||
{
|
||||
SaveLoadManager.Instance.RegisterParticipant(this);
|
||||
Logging.Debug("[FollowerController] Registered with SaveLoadManager");
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Warning("[FollowerController] SaveLoadManager not available for registration");
|
||||
}
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
@@ -114,6 +148,12 @@ public class FollowerController: MonoBehaviour
|
||||
void OnDisable()
|
||||
{
|
||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
|
||||
// Unregister from save system
|
||||
if (SaveLoadManager.Instance != null)
|
||||
{
|
||||
SaveLoadManager.Instance.UnregisterParticipant(GetSaveId());
|
||||
}
|
||||
}
|
||||
|
||||
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||
@@ -690,6 +730,123 @@ public class FollowerController: MonoBehaviour
|
||||
|
||||
#endregion ItemInteractions
|
||||
|
||||
#region ISaveParticipant Implementation
|
||||
|
||||
public bool HasBeenRestored => hasBeenRestored;
|
||||
|
||||
public string GetSaveId()
|
||||
{
|
||||
return "FollowerController";
|
||||
}
|
||||
|
||||
public string SerializeState()
|
||||
{
|
||||
var saveData = new FollowerSaveData
|
||||
{
|
||||
worldPosition = transform.position,
|
||||
worldRotation = transform.rotation
|
||||
};
|
||||
|
||||
// Save held item if any
|
||||
if (_cachedPickupObject != null)
|
||||
{
|
||||
var pickup = _cachedPickupObject.GetComponent<Pickup>();
|
||||
if (pickup is SaveableInteractable saveable)
|
||||
{
|
||||
saveData.heldItemSaveId = saveable.GetSaveId();
|
||||
}
|
||||
|
||||
if (_currentlyHeldItemData != null)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
saveData.heldItemDataAssetPath = UnityEditor.AssetDatabase.GetAssetPath(_currentlyHeldItemData);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
return JsonUtility.ToJson(saveData);
|
||||
}
|
||||
|
||||
public void RestoreState(string serializedData)
|
||||
{
|
||||
if (string.IsNullOrEmpty(serializedData))
|
||||
{
|
||||
Logging.Debug("[FollowerController] No saved state to restore");
|
||||
hasBeenRestored = true;
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var saveData = JsonUtility.FromJson<FollowerSaveData>(serializedData);
|
||||
if (saveData != null)
|
||||
{
|
||||
// Restore position and rotation
|
||||
transform.position = saveData.worldPosition;
|
||||
transform.rotation = saveData.worldRotation;
|
||||
|
||||
// Restore held item if any
|
||||
if (!string.IsNullOrEmpty(saveData.heldItemSaveId))
|
||||
{
|
||||
RestoreHeldItem(saveData.heldItemSaveId, saveData.heldItemDataAssetPath);
|
||||
}
|
||||
|
||||
hasBeenRestored = true;
|
||||
Logging.Debug($"[FollowerController] Restored position: {saveData.worldPosition}");
|
||||
}
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Logging.Warning($"[FollowerController] Failed to restore state: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void RestoreHeldItem(string heldItemSaveId, string heldItemDataAssetPath)
|
||||
{
|
||||
// Try to find the item by its save ID using ItemManager
|
||||
GameObject heldObject = ItemManager.Instance?.FindPickupBySaveId(heldItemSaveId);
|
||||
|
||||
if (heldObject == null)
|
||||
{
|
||||
Logging.Warning($"[FollowerController] Could not find held item with save ID: {heldItemSaveId}");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the item data
|
||||
PickupItemData heldData = null;
|
||||
#if UNITY_EDITOR
|
||||
if (!string.IsNullOrEmpty(heldItemDataAssetPath))
|
||||
{
|
||||
heldData = UnityEditor.AssetDatabase.LoadAssetAtPath<PickupItemData>(heldItemDataAssetPath);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (heldData == null)
|
||||
{
|
||||
var pickup = heldObject.GetComponent<Pickup>();
|
||||
if (pickup != null)
|
||||
{
|
||||
heldData = pickup.itemData;
|
||||
}
|
||||
}
|
||||
|
||||
// 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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion ISaveParticipant Implementation
|
||||
|
||||
#if UNITY_EDITOR
|
||||
void OnDrawGizmos()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user