| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using Lattice.ECS.Core; |
| | | 4 | | using Lattice.Math; |
| | | 5 | | |
| | | 6 | | namespace Lattice.ECS.Framework |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// 最小系统调度器。 |
| | | 10 | | /// 当前正式支持的是单线程、平面、按 phase 再按注册顺序执行的系统集合; |
| | | 11 | | /// 不提供 enable / disable、依赖排序或 `SystemGroup` 层级模型。 |
| | | 12 | | /// </summary> |
| | | 13 | | public sealed class SystemScheduler |
| | | 14 | | { |
| | 1 | 15 | | private static readonly SystemSchedulerBoundary FlatPhasedOrderedBoundary = new( |
| | 1 | 16 | | SystemSchedulerKind.FlatPhasedOrdered, |
| | 1 | 17 | | SystemSchedulerCapability.OrderedExecution | |
| | 1 | 18 | | SystemSchedulerCapability.ExplicitLifecycle | |
| | 1 | 19 | | SystemSchedulerCapability.StaticRegistrationBeforeInitialize | |
| | 1 | 20 | | SystemSchedulerCapability.PhasedExecution | |
| | 1 | 21 | | SystemSchedulerCapability.AuthoringContractValidation, |
| | 1 | 22 | | UnsupportedSystemSchedulerCapability.RuntimeMutation | |
| | 1 | 23 | | UnsupportedSystemSchedulerCapability.EnableDisable | |
| | 1 | 24 | | UnsupportedSystemSchedulerCapability.DependencyOrdering | |
| | 1 | 25 | | UnsupportedSystemSchedulerCapability.HierarchicalGrouping | |
| | 1 | 26 | | UnsupportedSystemSchedulerCapability.ThreadedExecution); |
| | | 27 | | |
| | 136 | 28 | | private readonly List<SystemRegistration> _registrations = new(); |
| | 136 | 29 | | private readonly List<SystemRegistration> _executionPlan = new(); |
| | | 30 | | private bool _initialized; |
| | | 31 | | private int _nextRegistrationOrder; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// 当前已注册系统数量。 |
| | | 35 | | /// </summary> |
| | 1 | 36 | | public int Count => _registrations.Count; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// 当前是否已完成初始化。 |
| | | 40 | | /// </summary> |
| | 0 | 41 | | public bool IsInitialized => _initialized; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// 当前系统层正式支持与不支持的调度边界。 |
| | | 45 | | /// </summary> |
| | 1 | 46 | | public SystemSchedulerBoundary Boundary => FlatPhasedOrderedBoundary; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// 注册系统。 |
| | | 50 | | /// 初始化后不允许再动态增删系统;若需要调整系统集合,请先 `Shutdown(frame)`。 |
| | | 51 | | /// </summary> |
| | | 52 | | public void Add(ISystem system) |
| | | 53 | | { |
| | 132 | 54 | | ArgumentNullException.ThrowIfNull(system); |
| | | 55 | | |
| | 131 | 56 | | if (_initialized) |
| | | 57 | | { |
| | 1 | 58 | | throw new InvalidOperationException("Cannot add systems while scheduler is initialized. Call Shutdown(fr |
| | | 59 | | } |
| | | 60 | | |
| | 418 | 61 | | for (int i = 0; i < _registrations.Count; i++) |
| | | 62 | | { |
| | 80 | 63 | | if (ReferenceEquals(_registrations[i].System, system)) |
| | | 64 | | { |
| | 1 | 65 | | throw new InvalidOperationException("System is already registered."); |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | 129 | 69 | | ValidateContract(system); |
| | 128 | 70 | | _registrations.Add(new SystemRegistration(system, _nextRegistrationOrder++)); |
| | 128 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// 移除系统。 |
| | | 75 | | /// 初始化后不允许再动态增删系统;若需要调整系统集合,请先 `Shutdown(frame)`。 |
| | | 76 | | /// </summary> |
| | | 77 | | public bool Remove(ISystem system) |
| | | 78 | | { |
| | 3 | 79 | | ArgumentNullException.ThrowIfNull(system); |
| | | 80 | | |
| | 3 | 81 | | if (_initialized) |
| | | 82 | | { |
| | 1 | 83 | | throw new InvalidOperationException("Cannot remove systems while scheduler is initialized. Call Shutdown |
| | | 84 | | } |
| | | 85 | | |
| | 6 | 86 | | for (int i = 0; i < _registrations.Count; i++) |
| | | 87 | | { |
| | 3 | 88 | | if (ReferenceEquals(_registrations[i].System, system)) |
| | | 89 | | { |
| | 2 | 90 | | _registrations.RemoveAt(i); |
| | 2 | 91 | | return true; |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | return false; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// 清空系统列表。 |
| | | 100 | | /// 必须在未初始化状态下调用;若系统已初始化,请先执行 Shutdown(frame)。 |
| | | 101 | | /// </summary> |
| | | 102 | | public void Clear() |
| | | 103 | | { |
| | 2 | 104 | | if (_initialized) |
| | | 105 | | { |
| | 1 | 106 | | throw new InvalidOperationException("Cannot clear systems while scheduler is initialized. Call Shutdown( |
| | | 107 | | } |
| | | 108 | | |
| | 1 | 109 | | _registrations.Clear(); |
| | 1 | 110 | | _executionPlan.Clear(); |
| | 1 | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// 初始化所有系统。 |
| | | 115 | | /// 初始化后,当前系统集合会被冻结,直到显式执行 `Shutdown(frame)`。 |
| | | 116 | | /// </summary> |
| | | 117 | | public void Initialize(Frame frame) |
| | | 118 | | { |
| | 87 | 119 | | ArgumentNullException.ThrowIfNull(frame); |
| | | 120 | | |
| | 87 | 121 | | if (_initialized) |
| | | 122 | | { |
| | 1 | 123 | | return; |
| | | 124 | | } |
| | | 125 | | |
| | 86 | 126 | | RebuildExecutionPlan(); |
| | | 127 | | |
| | 406 | 128 | | for (int i = 0; i < _executionPlan.Count; i++) |
| | | 129 | | { |
| | 117 | 130 | | _executionPlan[i].System.OnInit(frame); |
| | | 131 | | } |
| | | 132 | | |
| | 86 | 133 | | _initialized = true; |
| | 86 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// 按 phase 再按注册顺序更新所有系统。 |
| | | 138 | | /// </summary> |
| | | 139 | | public void Update(Frame frame, FP deltaTime) |
| | | 140 | | { |
| | 657 | 141 | | ArgumentNullException.ThrowIfNull(frame); |
| | | 142 | | |
| | 657 | 143 | | if (!_initialized) |
| | | 144 | | { |
| | 1 | 145 | | throw new InvalidOperationException("SystemScheduler must be initialized before update."); |
| | | 146 | | } |
| | | 147 | | |
| | 3314 | 148 | | for (int i = 0; i < _executionPlan.Count; i++) |
| | | 149 | | { |
| | 1004 | 150 | | ISystem system = _executionPlan[i].System; |
| | 1004 | 151 | | frame.BeginSystemAuthoringScope(system); |
| | | 152 | | try |
| | | 153 | | { |
| | 1004 | 154 | | system.OnUpdate(frame, deltaTime); |
| | 1001 | 155 | | } |
| | | 156 | | finally |
| | | 157 | | { |
| | 1004 | 158 | | frame.EndSystemAuthoringScope(); |
| | 1004 | 159 | | } |
| | | 160 | | } |
| | 653 | 161 | | } |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// 关闭所有系统。 |
| | | 165 | | /// </summary> |
| | | 166 | | public void Shutdown(Frame frame) |
| | | 167 | | { |
| | 72 | 168 | | ArgumentNullException.ThrowIfNull(frame); |
| | | 169 | | |
| | 72 | 170 | | if (!_initialized) |
| | | 171 | | { |
| | 1 | 172 | | return; |
| | | 173 | | } |
| | | 174 | | |
| | 330 | 175 | | for (int i = 0; i < _executionPlan.Count; i++) |
| | | 176 | | { |
| | 94 | 177 | | _executionPlan[i].System.OnDestroy(frame); |
| | | 178 | | } |
| | | 179 | | |
| | 71 | 180 | | _initialized = false; |
| | 71 | 181 | | } |
| | | 182 | | |
| | | 183 | | private void RebuildExecutionPlan() |
| | | 184 | | { |
| | 86 | 185 | | _executionPlan.Clear(); |
| | | 186 | | |
| | 86 | 187 | | if (_registrations.Count == 0) |
| | | 188 | | { |
| | 10 | 189 | | return; |
| | | 190 | | } |
| | | 191 | | |
| | 76 | 192 | | _executionPlan.AddRange(_registrations); |
| | 76 | 193 | | _executionPlan.Sort(static (left, right) => |
| | 76 | 194 | | { |
| | 48 | 195 | | int phaseComparison = left.Phase.CompareTo(right.Phase); |
| | 48 | 196 | | if (phaseComparison != 0) |
| | 76 | 197 | | { |
| | 28 | 198 | | return phaseComparison; |
| | 76 | 199 | | } |
| | 76 | 200 | | |
| | 20 | 201 | | return left.RegistrationOrder.CompareTo(right.RegistrationOrder); |
| | 76 | 202 | | }); |
| | 76 | 203 | | } |
| | | 204 | | |
| | | 205 | | private static void ValidateContract(ISystem system) |
| | | 206 | | { |
| | 129 | 207 | | system.Contract.ValidateForPhase(system.Phase, system.GetType()); |
| | 128 | 208 | | } |
| | | 209 | | |
| | | 210 | | private readonly struct SystemRegistration |
| | | 211 | | { |
| | | 212 | | public SystemRegistration(ISystem system, int registrationOrder) |
| | | 213 | | { |
| | 128 | 214 | | System = system; |
| | 128 | 215 | | RegistrationOrder = registrationOrder; |
| | 128 | 216 | | } |
| | | 217 | | |
| | 1394 | 218 | | public ISystem System { get; } |
| | | 219 | | |
| | 96 | 220 | | public SystemPhase Phase => System.Phase; |
| | | 221 | | |
| | 40 | 222 | | public int RegistrationOrder { get; } |
| | | 223 | | } |
| | | 224 | | } |
| | | 225 | | } |