MotionScheduler.cs
Runtime/Scheduling/MotionScheduler.cs
1 using System;
2
3 namespace MeatMotion
4 {
5 public interface IMotionScheduler
6 {
7 MotionHandle Schedule<TValue, TOptions, TAdapter>(ref MotionBuilder<TValue, TOptions, TAdapter> builder)
8 where TValue : unmanaged where TOptions : unmanaged, IMotionOptions
9 where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>;
10 }
11
12 public enum MotionTimeKind : byte { Time, UnscaledTime, Realtime }
13 internal enum LoopPhase { Initialization, EarlyUpdate, FixedUpdate, PreUpdate, Update, PreLateUpdate, PostLateUpdate, TimeUpdate }
14
15 internal abstract class WorldScheduler : IMotionScheduler
16 {
17 internal readonly MotionTimeKind Kind;
18 MotionWorld floatWorld;
19 protected WorldScheduler(MotionTimeKind kind = MotionTimeKind.Time) => Kind = kind;
20 internal abstract MotionWorld Resolve(Type motionType);
21
22 // A scheduler keeps its float world until that world is disposed.
23 internal MotionWorld ResolveFloat()
24 {
25 #if UNITY_EDITOR
26 // Edit Mode and Play Mode resolve to different worlds.
27 return Resolve(FloatMotionBuilder.FloatStorage);
28 #else
29 var world = floatWorld;
30 return world != null && !world.IsDisposed ? world : floatWorld = Resolve(FloatMotionBuilder.FloatStorage);
31 #endif
32 }
33
34 public MotionHandle Schedule<TValue, TOptions, TAdapter>(ref MotionBuilder<TValue, TOptions, TAdapter> builder)
35 where TValue : unmanaged where TOptions : unmanaged, IMotionOptions
36 where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
37 => builder.SchedulePending(Resolve(typeof(MotionBuilder<TValue, TOptions, TAdapter>)), Kind);
38 }
39
40 #if UNITY_6000_7_OR_NEWER
41 [Unity.Scripting.LifecycleManagement.NoAutoStaticsCleanup] // The schedulers hold no state. DefaultScheduler is a setting.
42 #endif
43 public static class MotionScheduler
44 {
45 sealed class PlayerScheduler : WorldScheduler
46 {
47 readonly LoopPhase phase;
48 internal PlayerScheduler(LoopPhase phase, MotionTimeKind kind) : base(kind) => this.phase = phase;
49 internal override MotionWorld Resolve(Type motionType) => MotionUpdateDriver.GetWorld(phase, motionType);
50 }
51
52 public static readonly IMotionScheduler Initialization = new PlayerScheduler(LoopPhase.Initialization, MotionTimeKind.Time);
53 public static readonly IMotionScheduler InitializationIgnoreTimeScale = new PlayerScheduler(LoopPhase.Initialization, MotionTimeKind.UnscaledTime);
54 public static readonly IMotionScheduler InitializationRealtime = new PlayerScheduler(LoopPhase.Initialization, MotionTimeKind.Realtime);
55 public static readonly IMotionScheduler EarlyUpdate = new PlayerScheduler(LoopPhase.EarlyUpdate, MotionTimeKind.Time);
56 public static readonly IMotionScheduler EarlyUpdateIgnoreTimeScale = new PlayerScheduler(LoopPhase.EarlyUpdate, MotionTimeKind.UnscaledTime);
57 public static readonly IMotionScheduler EarlyUpdateRealtime = new PlayerScheduler(LoopPhase.EarlyUpdate, MotionTimeKind.Realtime);
58 public static readonly IMotionScheduler FixedUpdate = new PlayerScheduler(LoopPhase.FixedUpdate, MotionTimeKind.Time);
59 public static readonly IMotionScheduler PreUpdate = new PlayerScheduler(LoopPhase.PreUpdate, MotionTimeKind.Time);
60 public static readonly IMotionScheduler PreUpdateIgnoreTimeScale = new PlayerScheduler(LoopPhase.PreUpdate, MotionTimeKind.UnscaledTime);
61 public static readonly IMotionScheduler PreUpdateRealtime = new PlayerScheduler(LoopPhase.PreUpdate, MotionTimeKind.Realtime);
62 public static readonly IMotionScheduler Update = new PlayerScheduler(LoopPhase.Update, MotionTimeKind.Time);
63 public static readonly IMotionScheduler UpdateIgnoreTimeScale = new PlayerScheduler(LoopPhase.Update, MotionTimeKind.UnscaledTime);
64 public static readonly IMotionScheduler UpdateRealtime = new PlayerScheduler(LoopPhase.Update, MotionTimeKind.Realtime);
65 public static readonly IMotionScheduler PreLateUpdate = new PlayerScheduler(LoopPhase.PreLateUpdate, MotionTimeKind.Time);
66 public static readonly IMotionScheduler PreLateUpdateIgnoreTimeScale = new PlayerScheduler(LoopPhase.PreLateUpdate, MotionTimeKind.UnscaledTime);
67 public static readonly IMotionScheduler PreLateUpdateRealtime = new PlayerScheduler(LoopPhase.PreLateUpdate, MotionTimeKind.Realtime);
68 public static readonly IMotionScheduler PostLateUpdate = new PlayerScheduler(LoopPhase.PostLateUpdate, MotionTimeKind.Time);
69 public static readonly IMotionScheduler PostLateUpdateIgnoreTimeScale = new PlayerScheduler(LoopPhase.PostLateUpdate, MotionTimeKind.UnscaledTime);
70 public static readonly IMotionScheduler PostLateUpdateRealtime = new PlayerScheduler(LoopPhase.PostLateUpdate, MotionTimeKind.Realtime);
71 public static readonly IMotionScheduler TimeUpdate = new PlayerScheduler(LoopPhase.TimeUpdate, MotionTimeKind.Time);
72 public static readonly IMotionScheduler TimeUpdateIgnoreTimeScale = new PlayerScheduler(LoopPhase.TimeUpdate, MotionTimeKind.UnscaledTime);
73 public static readonly IMotionScheduler TimeUpdateRealtime = new PlayerScheduler(LoopPhase.TimeUpdate, MotionTimeKind.Realtime);
74 public static IMotionScheduler Manual => ManualMotionDispatcher.Default.Scheduler;
75 public static IMotionScheduler DefaultScheduler { get; set; } = Update;
76 }
77 }