66 lines
1.9 KiB
C#
66 lines
1.9 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
/// <summary>
|
|
/// An interactable item pickup script. When Left-Clicked, it invokes pickup events
|
|
/// and destroys or deactivates the world object.
|
|
/// </summary>
|
|
public class PickupItem : MonoBehaviour, IInteractable
|
|
{
|
|
[Header("Item Details")]
|
|
[Tooltip("Item name or identifier.")]
|
|
[SerializeField] private string itemName = "Key";
|
|
|
|
[Tooltip("Action text shown in prompt (e.g. 'Pick up Key').")]
|
|
[SerializeField] private string promptAction = "Pick up";
|
|
|
|
[Header("Audio / FX")]
|
|
[SerializeField] private AudioClip pickupSound;
|
|
[SerializeField] private GameObject pickupEffectPrefab;
|
|
|
|
[Header("Events")]
|
|
[Tooltip("Event invoked with the item name when picked up.")]
|
|
[SerializeField] private UnityEvent<string> onPickedUpWithName;
|
|
|
|
[Tooltip("Event invoked passing the player GameObject.")]
|
|
[SerializeField] private UnityEvent<GameObject> onPickedUpByPlayer;
|
|
|
|
[Header("Options")]
|
|
[Tooltip("If true, destroys the GameObject upon pickup. If false, simply deactivates it.")]
|
|
[SerializeField] private bool destroyOnPickup = true;
|
|
|
|
public void Interact(GameObject interactor)
|
|
{
|
|
// Play sound at position if available
|
|
if (pickupSound != null)
|
|
{
|
|
AudioSource.PlayClipAtPoint(pickupSound, transform.position);
|
|
}
|
|
|
|
// Spawn visual effect if assigned
|
|
if (pickupEffectPrefab != null)
|
|
{
|
|
Instantiate(pickupEffectPrefab, transform.position, Quaternion.identity);
|
|
}
|
|
|
|
onPickedUpWithName?.Invoke(itemName);
|
|
onPickedUpByPlayer?.Invoke(interactor);
|
|
|
|
Debug.Log($"[PickupItem] Picked up: {itemName}");
|
|
|
|
if (destroyOnPickup)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
else
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
public string GetPrompt()
|
|
{
|
|
return $"{promptAction} {itemName}";
|
|
}
|
|
}
|