DelegateState.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. using System;
  3. namespace Animancer.FSM
  4. {
  5. /// <summary>An <see cref="IState"/> that uses delegates to define its behaviour.</summary>
  6. /// <remarks>
  7. /// Documentation: <see href="https://kybernetik.com.au/animancer/docs/manual/fsm/state-types">State Types</see>
  8. /// </remarks>
  9. /// https://kybernetik.com.au/animancer/api/Animancer.FSM/DelegateState
  10. ///
  11. public class DelegateState : IState
  12. {
  13. /************************************************************************************************************************/
  14. /// <summary>Determines whether this state can be entered. Null is treated as returning true.</summary>
  15. public Func<bool> canEnter;
  16. /// <summary>[<see cref="IState"/>] Calls <see cref="canEnter"/> to determine whether this state can be entered.</summary>
  17. public virtual bool CanEnterState => canEnter == null || canEnter();
  18. /************************************************************************************************************************/
  19. /// <summary>Determines whether this state can be exited. Null is treated as returning true.</summary>
  20. public Func<bool> canExit;
  21. /// <summary>[<see cref="IState"/>] Calls <see cref="canExit"/> to determine whether this state can be exited.</summary>
  22. public virtual bool CanExitState => canExit == null || canExit();
  23. /************************************************************************************************************************/
  24. /// <summary>Called when this state is entered.</summary>
  25. public Action onEnter;
  26. /// <summary>[<see cref="IState"/>] Calls <see cref="onEnter"/> when this state is entered.</summary>
  27. public virtual void OnEnterState() => onEnter?.Invoke();
  28. /************************************************************************************************************************/
  29. /// <summary>Called when this state is exited.</summary>
  30. public Action onExit;
  31. /// <summary>[<see cref="IState"/>] Calls <see cref="onExit"/> when this state is exited.</summary>
  32. public virtual void OnExitState() => onExit?.Invoke();
  33. /************************************************************************************************************************/
  34. }
  35. }