95 lines
3.0 KiB
C#
95 lines
3.0 KiB
C#
using Input;
|
|
using UnityEngine;
|
|
|
|
namespace Interactions
|
|
{
|
|
[RequireComponent(typeof(Interactable))]
|
|
public class Pickup : MonoBehaviour
|
|
{
|
|
public PickupItemData itemData;
|
|
public SpriteRenderer iconRenderer;
|
|
protected Interactable Interactable;
|
|
private PlayerTouchController _playerRef;
|
|
protected FollowerController FollowerController;
|
|
|
|
/// <summary>
|
|
/// Unity Awake callback. Sets up icon, interactable, and event handlers.
|
|
/// </summary>
|
|
void Awake()
|
|
{
|
|
if (iconRenderer == null)
|
|
iconRenderer = GetComponent<SpriteRenderer>();
|
|
|
|
Interactable = GetComponent<Interactable>();
|
|
if (Interactable != null)
|
|
{
|
|
Interactable.interactionStarted.AddListener(OnInteractionStarted);
|
|
Interactable.characterArrived.AddListener(OnCharacterArrived);
|
|
}
|
|
|
|
ApplyItemData();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unity OnDestroy callback. Cleans up event handlers.
|
|
/// </summary>
|
|
void OnDestroy()
|
|
{
|
|
if (Interactable != null)
|
|
{
|
|
Interactable.interactionStarted.RemoveListener(OnInteractionStarted);
|
|
Interactable.characterArrived.RemoveListener(OnCharacterArrived);
|
|
}
|
|
}
|
|
|
|
#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
|
|
|
|
/// <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>
|
|
/// Handles the start of an interaction (for feedback/UI only).
|
|
/// </summary>
|
|
private void OnInteractionStarted(PlayerTouchController playerRef, FollowerController followerRef)
|
|
{
|
|
_playerRef = playerRef;
|
|
FollowerController = followerRef;
|
|
}
|
|
|
|
protected virtual void OnCharacterArrived()
|
|
{
|
|
var combinationResult = FollowerController.TryCombineItems(this, out var combinationResultItem);
|
|
if (combinationResultItem != null)
|
|
{
|
|
Interactable.BroadcastInteractionComplete(true);
|
|
return;
|
|
}
|
|
|
|
FollowerController?.TryPickupItem(gameObject, itemData);
|
|
Interactable.BroadcastInteractionComplete(combinationResult == FollowerController.CombinationResult.NotApplicable);
|
|
}
|
|
}
|
|
} |