[Input][Interaction] Add interactable items
This commit is contained in:
55
Assets/Scripts/Pickup.cs
Normal file
55
Assets/Scripts/Pickup.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Pickup : MonoBehaviour
|
||||
{
|
||||
public PickupItemData itemData;
|
||||
public SpriteRenderer iconRenderer;
|
||||
private Interactable interactable;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (iconRenderer == null)
|
||||
iconRenderer = GetComponent<SpriteRenderer>();
|
||||
interactable = GetComponent<Interactable>();
|
||||
if (interactable != null)
|
||||
{
|
||||
interactable.Interacted += OnInteracted;
|
||||
}
|
||||
ApplyItemData();
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if (interactable != null)
|
||||
{
|
||||
interactable.Interacted -= OnInteracted;
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
void OnValidate()
|
||||
{
|
||||
if (iconRenderer == null)
|
||||
iconRenderer = GetComponent<SpriteRenderer>();
|
||||
ApplyItemData();
|
||||
}
|
||||
#endif
|
||||
|
||||
public void ApplyItemData()
|
||||
{
|
||||
if (itemData != null)
|
||||
{
|
||||
if (iconRenderer != null)
|
||||
iconRenderer.sprite = itemData.mapSprite;
|
||||
gameObject.name = itemData.itemName;
|
||||
// Optionally update other fields, e.g. description
|
||||
}
|
||||
}
|
||||
|
||||
private void OnInteracted()
|
||||
{
|
||||
Debug.Log($"Pickup.OnInteracted: Picked up {itemData?.itemName}");
|
||||
// TODO: Add item to inventory manager here
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user