DelegateState.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 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. /// <strong>Documentation:</strong>
  8. /// <see href="https://kybernetik.com.au/animancer/docs/manual/fsm/state-types">
  9. /// State Types</see>
  10. /// </remarks>
  11. /// https://kybernetik.com.au/animancer/api/Animancer.FSM/DelegateState
  12. ///
  13. public class DelegateState : IState
  14. {
  15. /************************************************************************************************************************/
  16. /// <summary>Determines whether this state can be entered. Null is treated as returning true.</summary>
  17. public Func<bool> canEnter;
  18. /// <summary>[<see cref="IState"/>] Calls <see cref="canEnter"/> to determine whether this state can be entered.</summary>
  19. public virtual bool CanEnterState => canEnter == null || canEnter();
  20. /************************************************************************************************************************/
  21. /// <summary>Determines whether this state can be exited. Null is treated as returning true.</summary>
  22. public Func<bool> canExit;
  23. /// <summary>[<see cref="IState"/>] Calls <see cref="canExit"/> to determine whether this state can be exited.</summary>
  24. public virtual bool CanExitState => canExit == null || canExit();
  25. /************************************************************************************************************************/
  26. /// <summary>Called when this state is entered.</summary>
  27. public Action onEnter;
  28. /// <summary>[<see cref="IState"/>] Calls <see cref="onEnter"/> when this state is entered.</summary>
  29. public virtual void OnEnterState() => onEnter?.Invoke();
  30. /************************************************************************************************************************/
  31. /// <summary>Called when this state is exited.</summary>
  32. public Action onExit;
  33. /// <summary>[<see cref="IState"/>] Calls <see cref="onExit"/> when this state is exited.</summary>
  34. public virtual void OnExitState() => onExit?.Invoke();
  35. /************************************************************************************************************************/
  36. }
  37. }