41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.Events;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// A general-purpose interactable component that can be placed on any GameObject in the scene.
|
||
|
|
/// Responds to Player Left-Click interactions via UnityEvents.
|
||
|
|
/// </summary>
|
||
|
|
public class InteractableObject : MonoBehaviour, IInteractable
|
||
|
|
{
|
||
|
|
[Header("Prompt Settings")]
|
||
|
|
[Tooltip("Text displayed when the player looks at this object.")]
|
||
|
|
[SerializeField] private string promptMessage = "Interact";
|
||
|
|
|
||
|
|
[Header("Interaction Events")]
|
||
|
|
[Tooltip("Event invoked when interacted with, passing the player GameObject as an argument.")]
|
||
|
|
[SerializeField] private UnityEvent<GameObject> onInteractWithPlayer;
|
||
|
|
|
||
|
|
[Tooltip("Simple event invoked when interacted with.")]
|
||
|
|
[SerializeField] private UnityEvent onInteract;
|
||
|
|
|
||
|
|
[Header("Behavior Options")]
|
||
|
|
[Tooltip("If true, the object can only be interacted with once.")]
|
||
|
|
[SerializeField] private bool singleUseOnly = false;
|
||
|
|
private bool hasBeenInteracted = false;
|
||
|
|
|
||
|
|
public void Interact(GameObject interactor)
|
||
|
|
{
|
||
|
|
if (singleUseOnly && hasBeenInteracted) return;
|
||
|
|
|
||
|
|
hasBeenInteracted = true;
|
||
|
|
onInteractWithPlayer?.Invoke(interactor);
|
||
|
|
onInteract?.Invoke();
|
||
|
|
}
|
||
|
|
|
||
|
|
public string GetPrompt()
|
||
|
|
{
|
||
|
|
if (singleUseOnly && hasBeenInteracted) return string.Empty;
|
||
|
|
return promptMessage;
|
||
|
|
}
|
||
|
|
}
|