Simple interactable rework

This commit is contained in:
Michal Pikulski
2025-10-31 13:50:08 +01:00
parent 095f21908b
commit 917230e10a
15 changed files with 1897 additions and 103 deletions

View File

@@ -5,14 +5,10 @@ using Core; // register with ItemManager
namespace Interactions
{
[RequireComponent(typeof(Interactable))]
public class Pickup : MonoBehaviour
public class Pickup : InteractableBase
{
public PickupItemData itemData;
public SpriteRenderer iconRenderer;
protected Interactable Interactable;
private PlayerTouchController _playerRef;
protected FollowerController FollowerController;
// Track if the item has been picked up
public bool isPickedUp { get; private set; }
@@ -24,19 +20,12 @@ namespace Interactions
public event Action<PickupItemData, PickupItemData, PickupItemData> OnItemsCombined;
/// <summary>
/// Unity Awake callback. Sets up icon, interactable, and event handlers.
/// Unity Awake callback. Sets up icon and applies item data.
/// </summary>
public virtual void Awake()
protected virtual void Awake()
{
if (iconRenderer == null)
iconRenderer = GetComponent<SpriteRenderer>();
Interactable = GetComponent<Interactable>();
if (Interactable != null)
{
Interactable.interactionStarted.AddListener(OnInteractionStarted);
Interactable.characterArrived.AddListener(OnCharacterArrived);
}
ApplyItemData();
}
@@ -50,16 +39,10 @@ namespace Interactions
}
/// <summary>
/// Unity OnDestroy callback. Cleans up event handlers.
/// Unity OnDestroy callback. Unregisters from ItemManager.
/// </summary>
void OnDestroy()
{
if (Interactable != null)
{
Interactable.interactionStarted.RemoveListener(OnInteractionStarted);
Interactable.characterArrived.RemoveListener(OnCharacterArrived);
}
// Unregister from ItemManager
ItemManager.Instance?.UnregisterPickup(this);
}
@@ -76,6 +59,7 @@ namespace Interactions
}
#endif
/// <summary>
/// Applies the item data to the pickup (icon, name, etc).
/// </summary>
@@ -93,22 +77,17 @@ namespace Interactions
}
/// <summary>
/// Handles the start of an interaction (for feedback/UI only).
/// Override: Called when character arrives at the interaction point.
/// Handles item pickup and combination logic.
/// </summary>
private void OnInteractionStarted(PlayerTouchController playerRef, FollowerController followerRef)
{
_playerRef = playerRef;
FollowerController = followerRef;
}
protected virtual void OnCharacterArrived()
protected override void OnCharacterArrived()
{
Logging.Debug("[Pickup] OnCharacterArrived");
var combinationResult = FollowerController.TryCombineItems(this, out var combinationResultItem);
var combinationResult = _followerController.TryCombineItems(this, out var combinationResultItem);
if (combinationResultItem != null)
{
Interactable.BroadcastInteractionComplete(true);
CompleteInteraction(true);
// Fire the combination event when items are successfully combined
if (combinationResult == FollowerController.CombinationResult.Successful)
@@ -118,7 +97,7 @@ namespace Interactions
{
// Get the combined item data
var resultItemData = resultPickup.itemData;
var heldItem = FollowerController.GetHeldPickupObject();
var heldItem = _followerController.GetHeldPickupObject();
if (heldItem != null)
{
@@ -135,18 +114,18 @@ namespace Interactions
return;
}
FollowerController?.TryPickupItem(gameObject, itemData);
_followerController?.TryPickupItem(gameObject, itemData);
var step = GetComponent<PuzzleS.ObjectiveStepBehaviour>();
if (step != null && !step.IsStepUnlocked())
{
Interactable.BroadcastInteractionComplete(false);
CompleteInteraction(false);
return;
}
bool wasPickedUp = (combinationResult == FollowerController.CombinationResult.NotApplicable
|| combinationResult == FollowerController.CombinationResult.Unsuccessful);
Interactable.BroadcastInteractionComplete(wasPickedUp);
CompleteInteraction(wasPickedUp);
// Update pickup state and invoke event when the item was picked up successfully
if (wasPickedUp)