Files
AppleHillsProduction/Assets/Scripts/Interactions/Pickup.cs

138 lines
4.7 KiB
C#
Raw Normal View History

using Input;
using UnityEngine;
using System; // added for Action<T>
using Core; // register with ItemManager
namespace Interactions
{
2025-10-31 13:50:08 +01:00
public class Pickup : InteractableBase
{
public PickupItemData itemData;
public SpriteRenderer iconRenderer;
// Track if the item has been picked up
public bool isPickedUp { get; private set; }
// Event: invoked when the item was picked up successfully
public event Action<PickupItemData> OnItemPickedUp;
// Event: invoked when this item is successfully combined with another
public event Action<PickupItemData, PickupItemData, PickupItemData> OnItemsCombined;
/// <summary>
2025-10-31 13:50:08 +01:00
/// Unity Awake callback. Sets up icon and applies item data.
/// </summary>
2025-10-31 13:50:08 +01:00
protected virtual void Awake()
{
if (iconRenderer == null)
iconRenderer = GetComponent<SpriteRenderer>();
ApplyItemData();
}
/// <summary>
/// Register with ItemManager on Start
/// </summary>
void Start()
{
ItemManager.Instance?.RegisterPickup(this);
}
/// <summary>
2025-10-31 13:50:08 +01:00
/// Unity OnDestroy callback. Unregisters from ItemManager.
/// </summary>
void OnDestroy()
{
// Unregister from ItemManager
ItemManager.Instance?.UnregisterPickup(this);
}
#if UNITY_EDITOR
/// <summary>
/// Unity OnValidate callback. Ensures icon and data are up to date in editor.
/// </summary>
void OnValidate()
{
if (iconRenderer == null)
iconRenderer = GetComponent<SpriteRenderer>();
ApplyItemData();
}
#endif
2025-10-31 13:50:08 +01:00
/// <summary>
/// Applies the item data to the pickup (icon, name, etc).
/// </summary>
public void ApplyItemData()
{
if (itemData != null)
{
if (iconRenderer != null && itemData.mapSprite != null)
{
iconRenderer.sprite = itemData.mapSprite;
}
gameObject.name = itemData.itemName;
}
}
/// <summary>
2025-10-31 13:50:08 +01:00
/// Override: Called when character arrives at the interaction point.
/// Handles item pickup and combination logic.
/// </summary>
2025-10-31 13:50:08 +01:00
protected override void OnCharacterArrived()
{
Logging.Debug("[Pickup] OnCharacterArrived");
2025-10-31 13:50:08 +01:00
var combinationResult = _followerController.TryCombineItems(this, out var combinationResultItem);
if (combinationResultItem != null)
{
2025-10-31 13:50:08 +01:00
CompleteInteraction(true);
// Fire the combination event when items are successfully combined
if (combinationResult == FollowerController.CombinationResult.Successful)
{
var resultPickup = combinationResultItem.GetComponent<Pickup>();
if (resultPickup != null && resultPickup.itemData != null)
{
// Get the combined item data
var resultItemData = resultPickup.itemData;
2025-10-31 13:50:08 +01:00
var heldItem = _followerController.GetHeldPickupObject();
if (heldItem != null)
{
var heldPickup = heldItem.GetComponent<Pickup>();
if (heldPickup != null && heldPickup.itemData != null)
{
// Trigger the combination event
OnItemsCombined?.Invoke(itemData, heldPickup.itemData, resultItemData);
}
}
}
}
return;
}
2025-10-31 13:50:08 +01:00
_followerController?.TryPickupItem(gameObject, itemData);
2025-10-02 07:16:39 +02:00
var step = GetComponent<PuzzleS.ObjectiveStepBehaviour>();
if (step != null && !step.IsStepUnlocked())
{
2025-10-31 13:50:08 +01:00
CompleteInteraction(false);
2025-10-02 07:16:39 +02:00
return;
}
bool wasPickedUp = (combinationResult == FollowerController.CombinationResult.NotApplicable
|| combinationResult == FollowerController.CombinationResult.Unsuccessful);
2025-10-31 13:50:08 +01:00
CompleteInteraction(wasPickedUp);
// Update pickup state and invoke event when the item was picked up successfully
if (wasPickedUp)
{
isPickedUp = true;
OnItemPickedUp?.Invoke(itemData);
}
}
}
}