StateMachine2.InputBuffer.cs 3.8 KB

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