// Animancer // Copyright 2020 Kybernetik // using System; namespace Animancer.FSM { /// /// A state that uses delegates to define its behaviour in the . /// public class DelegateState : IState where TState : class, IState { /************************************************************************************************************************/ /// Determines whether this state can be entered. Null is treated as returning true. public Func canEnter; /// Determines whether this state can be exited. Null is treated as returning true. public Func canExit; /// Called when this state is entered. public Action onEnter; /// Called when this state is exited. public Action onExit; /************************************************************************************************************************/ /// /// Constructs a new with the provided delegates. /// /// Determines whether this state can be entered. Null is treated as returning true. /// Determines whether this state can be exited. Null is treated as returning true. /// Called when this state is entered. /// Called when this state is exited. public DelegateState( Func canEnter = null, Func canExit = null, Action onEnter = null, Action onExit = null) { this.canEnter = canEnter; this.canExit = canExit; this.onEnter = onEnter; this.onExit = onExit; } /************************************************************************************************************************/ /// Calls to determine whether this state can be entered. bool IState.CanEnterState(TState previousState) { return canEnter == null || canEnter(previousState); } /************************************************************************************************************************/ /// Calls to determine whether this state can be exited. bool IState.CanExitState(TState nextState) { return canExit == null || canExit(nextState); } /************************************************************************************************************************/ /// Calls when this state is entered. void IState.OnEnterState() { if (onEnter != null) onEnter(); } /************************************************************************************************************************/ /// Calls when this state is exited. void IState.OnExitState() { if (onExit != null) onExit(); } /************************************************************************************************************************/ } }