StateMachine2.InputBuffer.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. namespace Animancer.FSM
  3. {
  4. public partial class StateMachine<TKey, TState>
  5. {
  6. /// <summary>
  7. /// A simple system that can <see cref="StateMachine{TState}.InputBuffer{TStateMachine}.State"/> a state then
  8. /// try to enter it every time <see cref="StateMachine{TState}.InputBuffer{TStateMachine}.Update(float)"/> is
  9. /// called until the <see cref="StateMachine{TState}.InputBuffer{TStateMachine}.TimeOut"/> expires.
  10. /// </summary>
  11. ///
  12. /// <remarks>
  13. /// Documentation: <see href="https://kybernetik.com.au/animancer/docs/manual/fsm/utilities#input-buffers">Input Buffers</see>
  14. /// </remarks>
  15. ///
  16. /// <example>See <see cref="StateMachine{TState}.InputBuffer{TStateMachine}"/>.</example>
  17. ///
  18. /// https://kybernetik.com.au/animancer/api/Animancer.FSM/InputBuffer
  19. ///
  20. public new class InputBuffer : InputBuffer<StateMachine<TKey, TState>>
  21. {
  22. /************************************************************************************************************************/
  23. /// <summary>The <typeparamref name="TKey"/> of the state this buffer is currently attempting to enter.</summary>
  24. public TKey Key { get; set; }
  25. /************************************************************************************************************************/
  26. /// <summary>Creates a new <see cref="InputBuffer"/>.</summary>
  27. public InputBuffer() { }
  28. /// <summary>Creates a new <see cref="InputBuffer"/> for the specified `stateMachine`.</summary>
  29. public InputBuffer(StateMachine<TKey, TState> stateMachine) : base(stateMachine) { }
  30. /************************************************************************************************************************/
  31. /// <summary>
  32. /// If a state is registered with the `key`, this method calls <see cref="Buffer(TKey, TState, float)"/>
  33. /// and returns true. Otherwise it returns false.
  34. /// </summary>
  35. /// <remarks>Doesn't actually attempt to enter the state until <see cref="Update(float)"/> is called.</remarks>
  36. public bool Buffer(TKey key, float timeOut)
  37. {
  38. if (StateMachine.TryGetValue(key, out var state))
  39. {
  40. Buffer(key, state, timeOut);
  41. return true;
  42. }
  43. else return false;
  44. }
  45. /// <summary>
  46. /// Sets the <see cref="Key"/>, <see cref="StateMachine{TState}.InputBuffer.State"/>, and
  47. /// <see cref="TimeOut"/>.
  48. /// </summary>
  49. /// <remarks>Doesn't actually attempt to enter the state until <see cref="Update(float)"/> is called.</remarks>
  50. public void Buffer(TKey key, TState state, float timeOut)
  51. {
  52. Key = key;
  53. Buffer(state, timeOut);
  54. }
  55. /************************************************************************************************************************/
  56. /// <inheritdoc/>
  57. protected override bool TryEnterState()
  58. => StateMachine.TryResetState(Key, State);
  59. /************************************************************************************************************************/
  60. /// <inheritdoc/>
  61. public override void Clear()
  62. {
  63. base.Clear();
  64. Key = default;
  65. }
  66. /************************************************************************************************************************/
  67. }
  68. }
  69. }