-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessor.cs
127 lines (100 loc) · 3.79 KB
/
Processor.cs
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
using System;
using System.Collections.Generic;
using UnityEngine;
using Object = System.Object;
namespace PPS {
// TODO: Add all MonoBehaviour messages: https://docs.unity3d.com/ScriptReference/MonoBehaviour.html
public interface IProcessor {
void Update();
void FixedUpdate();
void LateUpdate();
}
/// <summary>
/// An independent Processor which does not handle any data owned by itself.
/// </summary>
public abstract class Processor : IProcessor {
private bool isProcessing;
private readonly List<Processor> subProcessors = new List<Processor>();
public event EventHandler OnProcessingStart;
public event EventHandler OnProcessingEnd;
protected virtual bool ProcessOnFixedUpdate => false;
protected virtual bool ShouldProcess => true;
public List<Processor> SubProcessors => this.subProcessors;
protected virtual void Process() { }
/// <summary>
/// Processor.Start is called when its system has been fully initialised
/// in respect to subsystems and dependencies and is ready to operate.
/// </summary>
public virtual void SetReady() { }
public virtual void Update() {
// Reverse loop due to possible Processor disposal.
for (int i = this.subProcessors.Count; i-- > 0;) {
this.subProcessors[i].Update();
}
if (!ProcessOnFixedUpdate)
TryProcess();
}
public virtual void FixedUpdate() {
// Reverse loop due to possible Processor disposal.
for (int i = this.subProcessors.Count; i-- > 0;) {
this.subProcessors[i].FixedUpdate();
}
if (ProcessOnFixedUpdate)
TryProcess();
}
public virtual void LateUpdate() {
// Reverse loop due to possible Processor disposal.
for (int i = this.subProcessors.Count; i-- > 0;) {
this.subProcessors[i].LateUpdate();
}
}
private void TryProcess() {
if (!ShouldProcess) {
if (!this.isProcessing)
return;
this.isProcessing = false;
OnProcessingEnd?.Invoke(this, null);
return;
}
if (!this.isProcessing) {
this.isProcessing = true;
OnProcessingStart?.Invoke(this, null);
}
Process();
}
}
/// <summary>
/// A Processor which is linked to its Profile and instantiating System.
/// </summary>
public abstract class Processor<TSystem> : Processor, IDisposable
where TSystem : ISystem {
private bool isDisposed;
private readonly TSystem system;
private readonly GameObject gameObject;
public TSystem System => this.system;
public GameObject GameObject => this.gameObject;
public Transform Transform => this.gameObject.transform;
protected Processor(TSystem system, GameObject instance) {
this.gameObject = instance;
this.system = system;
system.AddInstance(this);
}
~Processor() {
Dispose(false);
}
public virtual void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool isDisposing) {
if (this.isDisposed || !isDisposing)
return;
if (this.gameObject != null)
UnityEngine.Object.DestroyImmediate(this.gameObject);
foreach (IDisposable subProcessor in SubProcessors)
subProcessor.Dispose();
this.system.RemoveInstance(this);
this.isDisposed = true;
}
}
}