ManualMotionDispatcher.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using LitMotion.Collections;
  3. namespace LitMotion
  4. {
  5. /// <summary>
  6. /// Manually updatable MotionDispatcher
  7. /// </summary>
  8. public static class ManualMotionDispatcher
  9. {
  10. static class Cache<TValue, TOptions, TAdapter>
  11. where TValue : unmanaged
  12. where TOptions : unmanaged, IMotionOptions
  13. where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
  14. {
  15. public static MotionStorage<TValue, TOptions, TAdapter> updateStorage;
  16. public static UpdateRunner<TValue, TOptions, TAdapter> updateRunner;
  17. public static MotionStorage<TValue, TOptions, TAdapter> GetOrCreate()
  18. {
  19. if (updateStorage == null)
  20. {
  21. var storage = new MotionStorage<TValue, TOptions, TAdapter>(MotionStorageManager.CurrentStorageId);
  22. MotionStorageManager.AddStorage(storage);
  23. updateStorage = storage;
  24. }
  25. return updateStorage;
  26. }
  27. }
  28. static FastListCore<IUpdateRunner> updateRunners;
  29. /// <summary>
  30. /// ManualMotionDispatcher time. It increases every time Update is called.
  31. /// </summary>
  32. public static double Time { get; set; }
  33. /// <summary>
  34. /// Ensures the storage capacity until it reaches at least `capacity`.
  35. /// </summary>
  36. /// <param name="capacity">The minimum capacity to ensure.</param>
  37. public static void EnsureStorageCapacity<TValue, TOptions, TAdapter>(int capacity)
  38. where TValue : unmanaged
  39. where TOptions : unmanaged, IMotionOptions
  40. where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
  41. {
  42. Cache<TValue, TOptions, TAdapter>.GetOrCreate().EnsureCapacity(capacity);
  43. }
  44. /// <summary>
  45. /// Update all scheduled motions with MotionScheduler.Manual
  46. /// </summary>
  47. /// <param name="deltaTime">Delta time</param>
  48. public static void Update(double deltaTime)
  49. {
  50. if (deltaTime < 0f) throw new ArgumentException("deltaTime must be 0 or higher.");
  51. Time += deltaTime;
  52. Update();
  53. }
  54. /// <summary>
  55. /// Update all scheduled motions with MotionScheduler.Manual
  56. /// </summary>
  57. public static void Update()
  58. {
  59. var span = updateRunners.AsSpan();
  60. for (int i = 0; i < span.Length; i++)
  61. {
  62. span[i].Update(Time, Time, Time);
  63. }
  64. }
  65. /// <summary>
  66. /// Cancel all motions and reset data.
  67. /// </summary>
  68. public static void Reset()
  69. {
  70. var span = updateRunners.AsSpan();
  71. for (int i = 0; i < span.Length; i++)
  72. {
  73. span[i].Reset();
  74. }
  75. }
  76. internal static MotionHandle Schedule<TValue, TOptions, TAdapter>(in MotionData<TValue, TOptions> data, in MotionCallbackData callbackData)
  77. where TValue : unmanaged
  78. where TOptions : unmanaged, IMotionOptions
  79. where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
  80. {
  81. MotionStorage<TValue, TOptions, TAdapter> storage = Cache<TValue, TOptions, TAdapter>.GetOrCreate();
  82. if (Cache<TValue, TOptions, TAdapter>.updateRunner == null)
  83. {
  84. var runner = new UpdateRunner<TValue, TOptions, TAdapter>(storage, Time, Time, Time);
  85. updateRunners.Add(runner);
  86. Cache<TValue, TOptions, TAdapter>.updateRunner = runner;
  87. }
  88. var (EntryIndex, Version) = storage.Append(data, callbackData);
  89. return new MotionHandle()
  90. {
  91. StorageId = storage.StorageId,
  92. Index = EntryIndex,
  93. Version = Version
  94. };
  95. }
  96. }
  97. }