API reference · 日本語API索引

ManualMotionDispatcher.cs

Runtime/Scheduling/ManualMotionDispatcher.cs

1 using System;
2 using System.Collections.Generic;
3 
4 namespace MeatMotion
5 {
6 #if UNITY_6000_7_OR_NEWER
7     [Unity.Scripting.LifecycleManagement.NoAutoStaticsCleanup] // MotionUpdateDriver.EndSession resets Default.
8 #endif
9     public sealed class ManualMotionDispatcher
10     {
11         sealed class Lane
12         {
13             internal double Time;
14             internal readonly MotionWorld World = new MotionWorld();
15         }
16 
17         sealed class ManualScheduler : WorldScheduler
18         {
19             readonly ManualMotionDispatcher owner;
20             internal ManualScheduler(ManualMotionDispatcher owner) => this.owner = owner;
21             internal override MotionWorld Resolve(Type motionType) => owner.GetLane(motionType).World;
22         }
23 
24         public static readonly ManualMotionDispatcher Default = new ManualMotionDispatcher();
25         public IMotionScheduler Scheduler { get; }
26         readonly Dictionary<Type, Lane> byType = new Dictionary<Type, Lane>();
27         readonly List<Lane> lanes = new List<Lane>();
28         Lane floatLane;
29         double time;
30         bool updating;
31 
32         public ManualMotionDispatcher() => Scheduler = new ManualScheduler(this);
33         public double Time { get => time; set => Update(value - time); }
34 
35         Lane GetLane(Type type)
36         {
37             if (type == FloatMotionBuilder.FloatStorage && floatLane != null) return floatLane;
38             if (!byType.TryGetValue(type, out var lane))
39             {
40                 lane = new Lane();
41                 byType.Add(type, lane);
42                 lanes.Add(lane);
43                 if (type == FloatMotionBuilder.FloatStorage) floatLane = lane;
44             }
45             return lane;
46         }
47 
48         public void Update(double deltaTime)
49         {
50             if (updating) throw new InvalidOperationException("Recursive manual update.");
51             double next = time + deltaTime;
52             if (double.IsNaN(next) || double.IsInfinity(next)) throw new ArgumentOutOfRangeException(nameof(deltaTime));
53             updating = true;
54             time = next;
55             try
56             {
57                 int count = lanes.Count;
58                 for (int i = 0; i < count; i++)
59                 {
60                     var lane = lanes[i];
61                     double delta = time - lane.Time;
62                     lane.Time = time;
63                     lane.World.TickClocks(delta, delta, delta);
64                 }
65             }
66             finally { updating = false; }
67         }
68 
69         public void Reset()
70         {
71             time = 0;
72             foreach (var lane in lanes)
73             {
74                 lane.World.ResetWithoutCallbacks();
75                 lane.Time = 0;
76             }
77         }
78 
79         public void EnsureStorageCapacity<TValue, TOptions, TAdapter>(int capacity)
80             where TValue : unmanaged where TOptions : unmanaged, IMotionOptions
81             where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
82             => GetLane(typeof(MotionBuilder<TValue, TOptions, TAdapter>)).World.EnsureCapacity(capacity);
83     }
84 }