92 lines
3.2 KiB
C#
92 lines
3.2 KiB
C#
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using Input;
|
|||
|
|
|
|||
|
|
namespace Interactions
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// Base class for all interaction action components
|
|||
|
|
/// These components respond to interaction events and can control the interaction flow
|
|||
|
|
/// </summary>
|
|||
|
|
public abstract class InteractionActionBase : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
[Tooltip("Which interaction events this action should respond to")]
|
|||
|
|
public List<InteractionEventType> respondToEvents = new List<InteractionEventType>();
|
|||
|
|
|
|||
|
|
[Tooltip("Whether the interaction flow should wait for this action to complete")]
|
|||
|
|
public bool pauseInteractionFlow = true;
|
|||
|
|
|
|||
|
|
protected Interactable parentInteractable;
|
|||
|
|
|
|||
|
|
protected virtual void Awake()
|
|||
|
|
{
|
|||
|
|
// Get the parent interactable component
|
|||
|
|
parentInteractable = GetComponentInParent<Interactable>();
|
|||
|
|
|
|||
|
|
if (parentInteractable == null)
|
|||
|
|
{
|
|||
|
|
Debug.LogError($"[{GetType().Name}] Cannot find parent Interactable component!");
|
|||
|
|
enabled = false;
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected virtual void OnEnable()
|
|||
|
|
{
|
|||
|
|
if (parentInteractable != null)
|
|||
|
|
{
|
|||
|
|
parentInteractable.RegisterAction(this);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected virtual void OnDisable()
|
|||
|
|
{
|
|||
|
|
if (parentInteractable != null)
|
|||
|
|
{
|
|||
|
|
parentInteractable.UnregisterAction(this);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Called when an interaction event occurs that this action is registered for
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="eventType">The type of event that occurred</param>
|
|||
|
|
/// <returns>A task that completes when the action is finished, or null if action won't execute</returns>
|
|||
|
|
public Task<bool> OnInteractionEvent(InteractionEventType eventType, PlayerTouchController player, FollowerController follower)
|
|||
|
|
{
|
|||
|
|
if (respondToEvents.Contains(eventType) && ShouldExecute(eventType, player, follower))
|
|||
|
|
{
|
|||
|
|
if (pauseInteractionFlow)
|
|||
|
|
{
|
|||
|
|
return ExecuteAsync(eventType, player, follower);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
// If we don't need to pause the flow, execute in the background
|
|||
|
|
// and return a completed task
|
|||
|
|
_ = ExecuteAsync(eventType, player, follower);
|
|||
|
|
return Task.FromResult(false);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Execute the action for the given event asynchronously
|
|||
|
|
/// </summary>
|
|||
|
|
protected abstract Task<bool> ExecuteAsync(InteractionEventType eventType, PlayerTouchController player, FollowerController follower);
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Called to determine if this action should execute for the given event
|
|||
|
|
/// Override this to add additional conditions for execution
|
|||
|
|
/// </summary>
|
|||
|
|
protected virtual bool ShouldExecute(InteractionEventType eventType, PlayerTouchController player, FollowerController follower)
|
|||
|
|
{
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|