Rework of base interactables and managed behaviors

This commit is contained in:
Michal Pikulski
2025-11-04 11:11:27 +01:00
parent 0dc3f3e803
commit c57e3aa7e0
62 changed files with 11193 additions and 1376 deletions

View File

@@ -19,32 +19,39 @@ namespace Interactions
/// <summary>
/// Saveable data for ItemSlot state
/// </summary>
[System.Serializable]
[Serializable]
public class ItemSlotSaveData
{
public PickupSaveData pickupData; // Base pickup state
public ItemSlotState slotState; // Current slot validation state
public string slottedItemSaveId; // Save ID of slotted item (if any)
public string slottedItemDataAssetPath; // Asset path to PickupItemData
public ItemSlotState slotState;
public string slottedItemSaveId;
}
// TODO: Remove this ridiculous inheritance from Pickup if possible
/// <summary>
/// Interaction requirement that allows slotting, swapping, or picking up items in a slot.
/// Interaction that allows slotting, swapping, or picking up items in a slot.
/// ItemSlot is a CONTAINER, not a Pickup itself.
/// </summary>
public class ItemSlot : Pickup
public class ItemSlot : SaveableInteractable
{
// Slot visual data (for the slot itself, not the item in it)
public PickupItemData itemData;
public SpriteRenderer iconRenderer;
// Slotted item tracking
private PickupItemData currentlySlottedItemData;
public SpriteRenderer slottedItemRenderer;
private GameObject currentlySlottedItemObject;
// Tracks the current state of the slotted item
private ItemSlotState _currentState = ItemSlotState.None;
private ItemSlotState currentState = ItemSlotState.None;
// Settings reference
private IInteractionSettings _interactionSettings;
private IPlayerFollowerSettings _playerFollowerSettings;
private IInteractionSettings interactionSettings;
private IPlayerFollowerSettings playerFollowerSettings;
/// <summary>
/// Read-only access to the current slotted item state.
/// </summary>
public ItemSlotState CurrentSlottedState => _currentState;
public ItemSlotState CurrentSlottedState => currentState;
public UnityEvent onItemSlotted;
public UnityEvent onItemSlotRemoved;
@@ -62,118 +69,189 @@ namespace Interactions
public UnityEvent onForbiddenItemSlotted;
// Native C# event alternative for code-only subscribers
public event Action<PickupItemData, PickupItemData> OnForbiddenItemSlotted;
private PickupItemData _currentlySlottedItemData;
public SpriteRenderer slottedItemRenderer;
private GameObject _currentlySlottedItemObject;
public GameObject GetSlottedObject()
{
return _currentlySlottedItemObject;
return currentlySlottedItemObject;
}
public void SetSlottedObject(GameObject obj)
{
_currentlySlottedItemObject = obj;
if (_currentlySlottedItemObject != null)
currentlySlottedItemObject = obj;
if (currentlySlottedItemObject != null)
{
_currentlySlottedItemObject.SetActive(false);
currentlySlottedItemObject.SetActive(false);
}
}
protected override void Awake()
{
base.Awake();
base.Awake(); // SaveableInteractable registration
// Setup visuals
if (iconRenderer == null)
iconRenderer = GetComponent<SpriteRenderer>();
ApplyItemData();
// Initialize settings references
_interactionSettings = GameManager.GetSettingsObject<IInteractionSettings>();
_playerFollowerSettings = GameManager.GetSettingsObject<IPlayerFollowerSettings>();
interactionSettings = GameManager.GetSettingsObject<IInteractionSettings>();
playerFollowerSettings = GameManager.GetSettingsObject<IPlayerFollowerSettings>();
}
protected override void OnCharacterArrived()
#if UNITY_EDITOR
/// <summary>
/// Unity OnValidate callback. Ensures icon and data are up to date in editor.
/// </summary>
void OnValidate()
{
Logging.Debug("[ItemSlot] OnCharacterArrived");
var heldItemData = _followerController.CurrentlyHeldItemData;
var heldItemObj = _followerController.GetHeldPickupObject();
var config = _interactionSettings?.GetSlotItemConfig(itemData);
var forbidden = config?.forbiddenItems ?? new List<PickupItemData>();
// Held item, slot empty -> try to slot item
if (heldItemData != null && _currentlySlottedItemObject == null)
{
// First check for forbidden items at the very start so we don't continue unnecessarily
if (PickupItemData.ListContainsEquivalent(forbidden, heldItemData))
{
DebugUIMessage.Show("Can't place that here.", Color.red);
onForbiddenItemSlotted?.Invoke();
OnForbiddenItemSlotted?.Invoke(itemData, heldItemData);
_currentState = ItemSlotState.Forbidden;
CompleteInteraction(false);
return;
}
if (iconRenderer == null)
iconRenderer = GetComponent<SpriteRenderer>();
ApplyItemData();
}
#endif
SlotItem(heldItemObj, heldItemData, true);
return;
/// <summary>
/// Applies the item data to the slot (icon, name, etc).
/// </summary>
public void ApplyItemData()
{
if (itemData != null)
{
if (iconRenderer != null && itemData.mapSprite != null)
{
iconRenderer.sprite = itemData.mapSprite;
}
gameObject.name = itemData.itemName + "_Slot";
}
}
#region Interaction Logic
/// <summary>
/// Validation: Check if interaction can proceed based on held item and slot state.
/// </summary>
protected override (bool canProceed, string errorMessage) CanProceedWithInteraction()
{
var heldItem = FollowerController?.CurrentlyHeldItemData;
// Scenario: Nothing held + Empty slot = Error
if (heldItem == null && currentlySlottedItemObject == null)
return (false, "This requires an item.");
// Check forbidden items if trying to slot into empty slot
if (heldItem != null && currentlySlottedItemObject == null)
{
var config = interactionSettings?.GetSlotItemConfig(itemData);
var forbidden = config?.forbiddenItems ?? new List<PickupItemData>();
if (PickupItemData.ListContainsEquivalent(forbidden, heldItem))
return (false, "Can't place that here.");
}
// Either pickup or swap items
if ((heldItemData == null && _currentlySlottedItemObject != null)
|| (heldItemData != null && _currentlySlottedItemObject != null))
return (true, null);
}
/// <summary>
/// Main interaction logic: Slot, pickup, swap, or combine items.
/// Returns true only if correct item was slotted.
/// </summary>
protected override bool DoInteraction()
{
Logging.Debug("[ItemSlot] DoInteraction");
var heldItemData = FollowerController.CurrentlyHeldItemData;
var heldItemObj = FollowerController.GetHeldPickupObject();
// Scenario 1: Held item + Empty slot = Slot it
if (heldItemData != null && currentlySlottedItemObject == null)
{
// If both held and slotted items exist, attempt combination via follower (reuse existing logic from Pickup)
if (heldItemData != null && _currentlySlottedItemData != null)
SlotItem(heldItemObj, heldItemData);
FollowerController.ClearHeldItem(); // Clear follower's hand after slotting
return IsSlottedItemCorrect();
}
// Scenario 2 & 3: Slot is full
if (currentlySlottedItemObject != null)
{
// Try combination if both items present
if (heldItemData != null)
{
var slottedPickup = _currentlySlottedItemObject?.GetComponent<Pickup>();
var slottedPickup = currentlySlottedItemObject.GetComponent<Pickup>();
if (slottedPickup != null)
{
var comboResult = _followerController.TryCombineItems(slottedPickup, out var combinationResultItem);
if (combinationResultItem != null && comboResult == FollowerController.CombinationResult.Successful)
var comboResult = FollowerController.TryCombineItems(slottedPickup, out var combinationResultItem);
if (comboResult == FollowerController.CombinationResult.Successful)
{
// Combination succeeded: fire slot-removed events and clear internals (don't call SlotItem to avoid duplicate events)
onItemSlotRemoved?.Invoke();
OnItemSlotRemoved?.Invoke(_currentlySlottedItemData);
_currentState = ItemSlotState.None;
// Clear internal references and visuals
_currentlySlottedItemObject = null;
_currentlySlottedItemData = null;
UpdateSlottedSprite();
CompleteInteraction(false);
return;
// Combination succeeded - clear slot and return false (not a "slot success")
ClearSlot();
return false;
}
}
}
// No combination (or not applicable) -> perform normal swap/pickup behavior
_followerController.TryPickupItem(_currentlySlottedItemObject, _currentlySlottedItemData, false);
onItemSlotRemoved?.Invoke();
OnItemSlotRemoved?.Invoke(_currentlySlottedItemData);
_currentState = ItemSlotState.None;
SlotItem(heldItemObj, heldItemData, _currentlySlottedItemObject == null);
return;
// No combination or unsuccessful - perform swap
// Step 1: Pickup from slot (follower now holds the old slotted item)
FollowerController.TryPickupItem(currentlySlottedItemObject, currentlySlottedItemData, dropItem: false);
ClearSlot();
// Step 2: If we had a held item, slot it (follower already holding picked up item, don't clear!)
if (heldItemData != null)
{
SlotItem(heldItemObj, heldItemData);
// Don't clear follower - they're holding the item they picked up from the slot
return IsSlottedItemCorrect();
}
// Just picked up from slot - not a success
return false;
}
// No held item, slot empty -> show warning
if (heldItemData == null && _currentlySlottedItemObject == null)
{
DebugUIMessage.Show("This requires an item.", Color.red);
return;
}
// Shouldn't reach here (validation prevents empty + no held)
return false;
}
/// <summary>
/// Helper: Check if the currently slotted item is correct.
/// </summary>
private bool IsSlottedItemCorrect()
{
return currentState == ItemSlotState.Correct;
}
/// <summary>
/// Helper: Clear the slot and fire removal events.
/// </summary>
private void ClearSlot()
{
var previousData = currentlySlottedItemData;
currentlySlottedItemObject = null;
currentlySlottedItemData = null;
currentState = ItemSlotState.None;
UpdateSlottedSprite();
// Fire removal events
onItemSlotRemoved?.Invoke();
OnItemSlotRemoved?.Invoke(previousData);
}
#endregion
#region Visual Updates
/// <summary>
/// Updates the sprite and scale for the currently slotted item.
/// </summary>
private void UpdateSlottedSprite()
{
if (slottedItemRenderer != null && _currentlySlottedItemData != null && _currentlySlottedItemData.mapSprite != null)
if (slottedItemRenderer != null && currentlySlottedItemData != null && currentlySlottedItemData.mapSprite != null)
{
slottedItemRenderer.sprite = _currentlySlottedItemData.mapSprite;
slottedItemRenderer.sprite = currentlySlottedItemData.mapSprite;
// Scale sprite to desired height, preserve aspect ratio, compensate for parent scale
float desiredHeight = _playerFollowerSettings?.HeldIconDisplayHeight ?? 2.0f;
var sprite = _currentlySlottedItemData.mapSprite;
float desiredHeight = playerFollowerSettings?.HeldIconDisplayHeight ?? 2.0f;
var sprite = currentlySlottedItemData.mapSprite;
float spriteHeight = sprite.bounds.size.y;
Vector3 parentScale = slottedItemRenderer.transform.parent != null
? slottedItemRenderer.transform.parent.localScale
@@ -191,18 +269,20 @@ namespace Interactions
}
}
#endregion
// Register with ItemManager when enabled
protected override void Start()
{
base.Start(); // This calls Pickup.Start() which registers with save system
base.Start(); // SaveableInteractable registration
// Additionally register as ItemSlot
// Register as ItemSlot
ItemManager.Instance?.RegisterItemSlot(this);
}
protected override void OnDestroy()
{
base.OnDestroy(); // Unregister from save system and pickup manager
base.OnDestroy(); // SaveableInteractable cleanup
// Unregister from slot manager
ItemManager.Instance?.UnregisterItemSlot(this);
@@ -212,35 +292,22 @@ namespace Interactions
protected override object GetSerializableState()
{
// Get base pickup state
PickupSaveData baseData = base.GetSerializableState() as PickupSaveData;
// Get slotted item save ID if there's a slotted item
string slottedSaveId = "";
string slottedAssetPath = "";
if (_currentlySlottedItemObject != null)
if (currentlySlottedItemObject != null)
{
var slottedPickup = _currentlySlottedItemObject.GetComponent<Pickup>();
var slottedPickup = currentlySlottedItemObject.GetComponent<Pickup>();
if (slottedPickup is SaveableInteractable saveablePickup)
{
slottedSaveId = saveablePickup.GetSaveId();
}
if (_currentlySlottedItemData != null)
{
#if UNITY_EDITOR
slottedAssetPath = UnityEditor.AssetDatabase.GetAssetPath(_currentlySlottedItemData);
#endif
}
}
return new ItemSlotSaveData
{
pickupData = baseData,
slotState = _currentState,
slottedItemSaveId = slottedSaveId,
slottedItemDataAssetPath = slottedAssetPath
slotState = currentState,
slottedItemSaveId = slottedSaveId
};
}
@@ -253,20 +320,13 @@ namespace Interactions
return;
}
// First restore base pickup state
if (data.pickupData != null)
{
string pickupJson = JsonUtility.ToJson(data.pickupData);
base.ApplySerializableState(pickupJson);
}
// Restore slot state
_currentState = data.slotState;
currentState = data.slotState;
// Restore slotted item if there was one
if (!string.IsNullOrEmpty(data.slottedItemSaveId))
{
RestoreSlottedItem(data.slottedItemSaveId, data.slottedItemDataAssetPath);
RestoreSlottedItem(data.slottedItemSaveId);
}
}
@@ -274,7 +334,7 @@ namespace Interactions
/// Restore a slotted item from save data.
/// This is called during load restoration and should NOT trigger events.
/// </summary>
private void RestoreSlottedItem(string slottedItemSaveId, string slottedItemDataAssetPath)
private void RestoreSlottedItem(string slottedItemSaveId)
{
// Try to find the item in the scene by its save ID via ItemManager
GameObject slottedObject = ItemManager.Instance?.FindPickupBySaveId(slottedItemSaveId);
@@ -285,106 +345,92 @@ namespace Interactions
return;
}
// Get the item data
// Get the item data from the pickup component
PickupItemData slottedData = null;
#if UNITY_EDITOR
if (!string.IsNullOrEmpty(slottedItemDataAssetPath))
var pickup = slottedObject.GetComponent<Pickup>();
if (pickup != null)
{
slottedData = UnityEditor.AssetDatabase.LoadAssetAtPath<PickupItemData>(slottedItemDataAssetPath);
}
#endif
if (slottedData == null)
{
var pickup = slottedObject.GetComponent<Pickup>();
if (pickup != null)
{
slottedData = pickup.itemData;
}
slottedData = pickup.itemData;
}
// Silently slot the item (no events, no interaction completion)
// Follower state is managed separately during save/load restoration
ApplySlottedItemState(slottedObject, slottedData, triggerEvents: false);
}
/// <summary>
/// Core logic for slotting an item. Can be used both for normal slotting and silent restoration.
/// NOTE: Does NOT call CompleteInteraction - the template method handles that via DoInteraction return value.
/// NOTE: Does NOT manage follower state - caller is responsible for clearing follower's hand if needed.
/// </summary>
/// <param name="itemToSlot">The item GameObject to slot (or null to clear)</param>
/// <param name="itemToSlotData">The PickupItemData for the item</param>
/// <param name="triggerEvents">Whether to fire events and complete interaction</param>
/// <param name="clearFollowerHeldItem">Whether to clear the follower's held item</param>
private void ApplySlottedItemState(GameObject itemToSlot, PickupItemData itemToSlotData, bool triggerEvents, bool clearFollowerHeldItem = true)
/// <param name="triggerEvents">Whether to fire events</param>
private void ApplySlottedItemState(GameObject itemToSlot, PickupItemData itemToSlotData, bool triggerEvents)
{
// Cache the previous item data before clearing, needed for OnItemSlotRemoved event
var previousItemData = _currentlySlottedItemData;
bool wasSlotCleared = _currentlySlottedItemObject != null && itemToSlot == null;
if (itemToSlot == null)
{
_currentlySlottedItemObject = null;
_currentlySlottedItemData = null;
_currentState = ItemSlotState.None;
// Clear slot
var previousData = currentlySlottedItemData;
currentlySlottedItemObject = null;
currentlySlottedItemData = null;
currentState = ItemSlotState.None;
// Fire native event for slot clearing (only if triggering events)
if (wasSlotCleared && triggerEvents)
if (previousData != null && triggerEvents)
{
OnItemSlotRemoved?.Invoke(previousItemData);
onItemSlotRemoved?.Invoke();
OnItemSlotRemoved?.Invoke(previousData);
}
}
else
{
// Slot the item
itemToSlot.SetActive(false);
itemToSlot.transform.SetParent(null);
SetSlottedObject(itemToSlot);
_currentlySlottedItemData = itemToSlotData;
}
if (clearFollowerHeldItem && _followerController != null)
{
_followerController.ClearHeldItem();
}
UpdateSlottedSprite();
// Only validate and trigger events if requested
if (triggerEvents)
{
// Once an item is slotted, we know it is not forbidden, so we can skip that check, but now check if it was
// the correct item we're looking for
var config = _interactionSettings?.GetSlotItemConfig(itemData);
currentlySlottedItemData = itemToSlotData;
// Determine if correct
var config = interactionSettings?.GetSlotItemConfig(itemData);
var allowed = config?.allowedItems ?? new List<PickupItemData>();
if (itemToSlotData != null && PickupItemData.ListContainsEquivalent(allowed, itemToSlotData))
{
if (itemToSlot != null)
currentState = ItemSlotState.Correct;
// Fire events if requested
if (triggerEvents)
{
DebugUIMessage.Show("You correctly slotted " + itemToSlotData.itemName + " into: " + itemData.itemName, Color.green);
DebugUIMessage.Show($"You correctly slotted {itemToSlotData.itemName} into: {itemData.itemName}", Color.green);
onCorrectItemSlotted?.Invoke();
OnCorrectItemSlotted?.Invoke(itemData, _currentlySlottedItemData);
_currentState = ItemSlotState.Correct;
OnCorrectItemSlotted?.Invoke(itemData, currentlySlottedItemData);
}
CompleteInteraction(true);
}
else
{
if (itemToSlot != null)
currentState = ItemSlotState.Incorrect;
// Fire events if requested
if (triggerEvents)
{
DebugUIMessage.Show("I'm not sure this works.", Color.yellow);
onIncorrectItemSlotted?.Invoke();
OnIncorrectItemSlotted?.Invoke(itemData, _currentlySlottedItemData);
_currentState = ItemSlotState.Incorrect;
OnIncorrectItemSlotted?.Invoke(itemData, currentlySlottedItemData);
}
CompleteInteraction(false);
}
}
UpdateSlottedSprite();
}
/// <summary>
/// Public API for slotting items during gameplay.
/// Caller is responsible for managing follower's held item state.
/// </summary>
public void SlotItem(GameObject itemToSlot, PickupItemData itemToSlotData, bool clearFollowerHeldItem = true)
public void SlotItem(GameObject itemToSlot, PickupItemData itemToSlotData)
{
ApplySlottedItemState(itemToSlot, itemToSlotData, triggerEvents: true, clearFollowerHeldItem);
ApplySlottedItemState(itemToSlot, itemToSlotData, triggerEvents: true);
}
#endregion