50 lines
1.2 KiB
C#
50 lines
1.2 KiB
C#
using KitsuneCafe.Sys;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
namespace KitsuneCafe.Interaction
|
|
{
|
|
public class Interactable : MonoBehaviour, IInteractable
|
|
{
|
|
[SerializeField]
|
|
private bool oneTimeUse = false;
|
|
|
|
[SerializeField]
|
|
private UnityEvent<IInteractor> onSelected = default;
|
|
|
|
[SerializeField]
|
|
private UnityEvent<IInteractor> onDeselected = default;
|
|
|
|
[SerializeField]
|
|
private UnityEvent<IInteractor> onInteracted = default;
|
|
|
|
public bool IsInteractable => enabled;
|
|
|
|
void IInteractable.Select(IInteractor interactor)
|
|
{
|
|
if (enabled)
|
|
{
|
|
onSelected.Invoke(interactor);
|
|
}
|
|
}
|
|
|
|
void IInteractable.Deselect(IInteractor interactor)
|
|
{
|
|
onDeselected.Invoke(interactor);
|
|
}
|
|
|
|
public IResult<Unit, InteractionError> Interact(IInteractor interactor)
|
|
{
|
|
if (!enabled) { return Result.Err<Unit, InteractionError>(InteractionError.Inactive()); }
|
|
|
|
onInteracted.Invoke(interactor);
|
|
|
|
if (oneTimeUse)
|
|
{
|
|
enabled = false;
|
|
}
|
|
|
|
return Result.Ok<Unit, InteractionError>(default);
|
|
}
|
|
}
|
|
}
|