-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystem.cs
More file actions
339 lines (265 loc) · 12.6 KB
/
System.cs
File metadata and controls
339 lines (265 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
namespace PPS {
public interface ISystem : IProcessor {
bool IsReady { get; }
string NewInstanceName { get; }
Transform Transform { get; }
GameObject InstancePrefab { get; }
Processor DeployInstance { get; }
event EventHandler<Type> InstanceDeployed;
event EventHandler<Type> InstanceRemoved;
event EventHandler Ready;
void RemoveInstance(Processor processor);
void AddInstance(Processor processor);
}
internal static class System {
/// <summary>
/// Deploys a system instance.
/// </summary>
public static Processor DeployInstance<TSystem>(Type processorType, ISystem system, GameObject prefab, Transform parent, string instanceName)
where TSystem : ISystem {
GameObject instance = prefab != null ? UnityEngine.Object.Instantiate(prefab, parent) : null;
if (prefab != null) {
instance.name = instanceName;
instance.transform.parent = parent;
}
// Deploy processor.
Type[] processorConstructorTypes = { system.GetType(), typeof(GameObject) };
object[] processorConstructorParams = { system, instance };
Processor<TSystem> processor = processorType.GetConstructor(processorConstructorTypes)?.Invoke(processorConstructorParams) as Processor<TSystem>;
if (processor == null)
throw new Exception($"Could not instantiate the instance's Processor from type {processorType}.\n" +
$"Make sure the class has the correct instance constructor params (TSystem, GameObject).");
return processor;
}
}
[Serializable]
public abstract class System<TProcessor> : MonoBehaviour, ISystem
where TProcessor : Processor {
[SerializeField]
private bool deployOnStartup;
[SerializeField, Tooltip("Whether to automatically mark this System as ready during MonoBehaviour.Awake.\n\n" +
"If false it is required to manually set this System as ready, or its instances will " +
"not have Processor.Start invoked.")]
private bool readyOnStartup = true;
private bool isReady;
[SerializeField]
private GameObject instancePrefab;
protected List<TProcessor> instances = new List<TProcessor>();
private List<ISystem> subsystems = new List<ISystem>();
Processor ISystem.DeployInstance => DeployInstance();
public bool IsReady => this.isReady;
public string NewInstanceName => $"{GetType().Name} instance #{this.instances.Count + 1}";
public Transform Transform => transform; // Thanks, Unity.
public GameObject InstancePrefab => this.instancePrefab;
public event EventHandler<Type> InstanceDeployed;
public event EventHandler<Type> InstanceRemoved;
public event EventHandler Ready;
public virtual void Awake() {
InstanceDeployed += UpdateSerializableInstances;
InstanceRemoved += UpdateSerializableInstances;
DeploySubsystems();
if (this.deployOnStartup && this.instances.Count == 0)
DeployInstance();
if (this.readyOnStartup)
SetReady();
}
protected void SetReady() {
this.isReady = true;
Ready?.Invoke(this, null);
foreach (TProcessor processor in this.instances) {
processor.SetReady();
}
}
/// <summary>
/// Since unity 2019 does not serialise generics, it is needed to provide a wrapper type in the class that inherits from this one.
/// </summary>
protected virtual void UpdateSerializableInstances(object sender, Type instanceType) { }
protected virtual void DeploySubsystems() { }
public TProcessor DeployInstance() {
MethodInfo deployMethod = typeof(System).GetMethod("DeployInstance")?.MakeGenericMethod(GetType());
TProcessor processor = (TProcessor)deployMethod?.Invoke(null, new object[] { typeof(TProcessor), this, this.instancePrefab, transform, NewInstanceName });
if (processor == null)
throw new Exception($"Could not deploy new {GetType()} system instance.");
this.instances.Add(processor);
if (this.isReady)
processor.SetReady();
InstanceDeployed?.Invoke(processor, GetType());
return processor;
}
public TProcessor DeployInstance(Type processorType, GameObject instancePrefab = null) {
MethodInfo deployMethod = typeof(System).GetMethod("DeployInstance")?.MakeGenericMethod(GetType());
TProcessor processor = (TProcessor)deployMethod?.Invoke(null, new object[] { processorType, this, instancePrefab ?? this.instancePrefab, Transform, NewInstanceName });
if (processor == null)
throw new Exception($"Could not deploy new {GetType()} system instance.");
if (this.isReady)
processor.SetReady();
return processor;
}
public void AddInstance(Processor processor) {
this.instances.Add((TProcessor)processor);
InstanceDeployed?.Invoke(processor, GetType());
}
public void RemoveInstance(Processor processor) {
this.instances.Remove((TProcessor)processor);
InstanceRemoved?.Invoke(processor, GetType());
}
protected void DeploySubsystem<TSubsystem, TSProcessor>(ref TSubsystem subsystem)
where TSProcessor : Processor
where TSubsystem : Subsystem<TSProcessor> {
// Only construct subsystem if it has not been serialized.
if (subsystem == null)
subsystem = typeof(TSubsystem).GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TSubsystem;
if (subsystem == null)
throw new Exception("Could not invoke new subsystem from types given");
subsystem.Transform = new GameObject(subsystem.GetType().Name).transform;
subsystem.Transform.parent = transform;
this.subsystems.Add(subsystem);
subsystem.Awake(subsystem.Transform, this);
}
public virtual void Update() {
// Reverse loop due to possible Processor disposal.
for (int i = this.instances.Count; i-- > 0;) {
this.instances[i].Update();
}
foreach (ISystem subsystem in this.subsystems) {
subsystem.Update();
}
}
public virtual void FixedUpdate() {
// Reverse loop due to possible Processor disposal.
for (int i = this.instances.Count; i-- > 0;) {
this.instances[i].FixedUpdate();
}
foreach (ISystem subsystem in this.subsystems) {
subsystem.FixedUpdate();
}
}
public virtual void LateUpdate() {
// Reverse loop due to possible Processor disposal.
for (int i = this.instances.Count; i-- > 0;) {
this.instances[i].LateUpdate();
}
foreach (ISystem subsystem in this.subsystems) {
subsystem.LateUpdate();
}
}
protected List<TSystem> GetSubsystemInstances<TSystem>()
where TSystem : ISystem {
List<TSystem> converted = new List<TSystem>();
foreach (ISystem subsystem in this.subsystems) {
TSystem iteration;
try {
iteration = (TSystem)subsystem;
} catch {
continue;
}
if (iteration == null)
continue;
converted.Add(iteration);
}
return converted;
}
}
[Serializable]
public abstract class Subsystem<TProcessor> : ISystem
where TProcessor : Processor {
[SerializeField]
private bool deployOnStartup;
[SerializeField, Tooltip("Whether to automatically mark this System as ready during MonoBehaviour.Awake.\n\n" +
"If false it is required to manually set this System as ready, or its instances will " +
"not have Processor.Start invoked.")]
private bool readyOnStartup = true;
private bool isReady;
[SerializeField]
private GameObject instancePrefab;
private ISystem parent;
protected readonly List<TProcessor> instances = new List<TProcessor>();
Processor ISystem.DeployInstance => DeployInstance();
public bool IsReady => this.isReady;
public string NewInstanceName => $"{GetType().Name} instance #{this.instances.Count + 1}";
public Transform Transform { get; set; }
public GameObject InstancePrefab => this.instancePrefab;
public List<TProcessor> Instances => this.instances;
public ISystem Parent => this.parent;
public event EventHandler<Type> InstanceDeployed;
public event EventHandler<Type> InstanceRemoved;
public event EventHandler Ready;
protected Subsystem() { }
/// <summary>
/// Since unity 2019 does not serialise generics, it is needed to provide a wrapper type in the class that inherits from this one.
/// </summary>
protected abstract void UpdateSerializableInstances(object sender, Type instanceType);
/// <summary>
/// Subsystems are initialised through the Awake method as they are serialized.
/// </summary>
public virtual void Awake(Transform transform, ISystem parent) {
this.Transform = transform;
this.parent = parent;
InstanceDeployed += UpdateSerializableInstances;
InstanceRemoved += UpdateSerializableInstances;
if (this.deployOnStartup && this.instances.Count == 0)
DeployInstance();
if (this.readyOnStartup)
StartReadyCheck();
}
private void StartReadyCheck() {
if (this.parent.IsReady) {
SetReady();
return;
}
this.parent.Ready += (sender, args) => { SetReady(); };
}
protected void SetReady() {
this.isReady = true;
Ready?.Invoke(this, null);
foreach (TProcessor processor in this.instances) {
processor.SetReady();
}
}
public TProcessor DeployInstance(Type processorType, GameObject instancePrefab = null) {
if (Transform == null)
throw new InvalidOperationException($"Attempted to Deploy an instance of Subsystem {GetType().Name} before it has been initialised.");
MethodInfo deployMethod = typeof(System).GetMethod("DeployInstance")?.MakeGenericMethod(GetType());
TProcessor processor = (TProcessor)deployMethod?.Invoke(null, new object[] { processorType, this, instancePrefab ?? this.instancePrefab, Transform, NewInstanceName });
if (processor == null)
throw new Exception($"Could not deploy new {GetType()} system instance.");
if (this.isReady)
processor.SetReady();
return processor;
}
public TProcessor DeployInstance() {
return DeployInstance(typeof(TProcessor), this.instancePrefab);
}
public void AddInstance(Processor processor) {
this.instances.Add((TProcessor)processor);
InstanceDeployed?.Invoke(processor, GetType());
}
public void RemoveInstance(Processor processor) {
this.instances.Remove((TProcessor)processor);
InstanceRemoved?.Invoke(processor, GetType());
}
public virtual void Update() {
// Reverse loop due to possible Processor disposal.
for (int i = this.instances.Count; i-- > 0;) {
this.instances[i].Update();
}
}
public virtual void FixedUpdate() {
// Reverse loop due to possible Processor disposal.
for (int i = this.instances.Count; i-- > 0;) {
this.instances[i].FixedUpdate();
}
}
public virtual void LateUpdate() {
// Reverse loop due to possible Processor disposal.
for (int i = this.instances.Count; i-- > 0;) {
this.instances[i].LateUpdate();
}
}
}
}