-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemManager.ecs.js
More file actions
executable file
·239 lines (191 loc) · 5.9 KB
/
Copy pathSystemManager.ecs.js
File metadata and controls
executable file
·239 lines (191 loc) · 5.9 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
// 系统管理器
export default class SystemManager {
constructor(ecs) {
// ecs
this._ecs = ecs;
// 系统集合
this._systemMap = new Map();
// 当前运行系统
this._currentSystem = null;
}
/*
* 检测系统是否已注册
* @param (function) systemClass 系统类
* @return (boolean) isRegister 是否已注册
*/
_preRegister(systemClass) {
if (this.has(systemClass)) {
console.warn('The system has been registered.');
return true;
}
return false;
}
/*
* 注册系统
* @param (object) system 系统实例
*/
register(system) {
if (this._preRegister(system.constructor)) {
return this;
}
const systems = [...this._systemMap];
systems.push([system.constructor.name, system]);
this._systemMap = new Map(systems);
system.initialize();
}
/*
* 注册系统,并添加到指定系统之前,如果没有指定,则添加到系统列表首部
* @param (object) system 系统实例
* @param (function) beforeSystemClass 系统类
*/
registerBefore(system, beforeSystemClass) {
if (this._preRegister(system.constructor)) {
return this;
}
let index = -1;
const systems = [...this._systemMap];
const systemName = system.constructor.name;
const beforeSystemName = this._ecs.getClassName(beforeSystemClass);
if (this._preRegister(beforeSystemClass)) {
for (let i = 0, l = systems.length; i < l; ++i) {
if (systems[i][0] === beforeSystemClass) {
index = i;
systems.splice(index, 0, [systemName, system]);
break;
}
}
}
if (index < 0) {
systems.unshift([systemName, system]);
}
this._systemMap = new Map(systems);
system.initialize();
}
/*
* 注册系统,并添加到指定系统之前,如果没有指定,则添加到系统列表尾部
* @param (object) system 系统实例
* @param (function) afterSystemClass 系统类
*/
registerAfter(system, afterSystemClass) {
if (this._preRegister(system.constructor)) {
return this;
}
let index = -1;
const systems = [...this._systemMap];
const systemName = system.constructor.name;
const afterSystemName = this._ecs.getClassName(afterSystemClass);
if (this._preRegister(afterSystemClass)) {
for (let i = 0, len = systems.length; i < len; i++) {
if (systems[i][0] === afterSystemName) {
index = i + 1;
systems.splice(index, 0, [systemName, system]);
break;
}
}
}
if (index < 0) {
systems.push([systemName, system]);
}
this._systemMap = new Map(systems);
system.initialize();
}
/*
* 获取系统
* @param (function | string) systemClass 系统类 | 系统名
* @return (object) system 当前系统
*/
get(systemClass) {
const systemName = this._ecs.getClassName(systemClass);
return this._systemMap.get(systemName);
}
/*
* 获取所有系统
* @return (array) systems 所有系统
*/
getAll() {
const systems = [];
for (let system of this._systemMap.values()) {
systems.push(system);
}
return systems;
}
/*
* 检测系统是否存在
* @param (function | string) systemClass 系统类 | 系统名
*/
has(systemClass) {
const systemName = this._ecs.getClassName(systemClass);
return this._systemMap.has(systemName);
}
/*
* 激活系统
* @param (function | string) systemClass 系统类 | 系统名
*/
enable(systemClass) {
const system = this.get(systemClass);
if (system === undefined) {
console.warn('Enable system error.');
return;
}
system.enabled = true;
}
/*
* 禁用系统
* @param (function | string) systemClass 系统类 | 系统名
*/
disable(systemClass) {
const system = this.get(systemClass);
if (system === undefined) {
console.warn('Disable system error.');
return;
}
system.enabled = false;
}
/*
* 移除系统
* @param (function | string) systemClass 系统类 | 系统名
*/
remove(systemClass) {
const system = this.get(systemClass);
if (system === undefined) {
console.warn('Remove system error.');
return;
}
const systemName = this._ecs.getClassName(systemClass);
this._systemMap.delete(systemName);
system.uninitialize();
}
// 清空系统
clear() {
for (const [systemName, system] of this._systemMap) {
if (system) {
system.uninitialize();
}
}
this._systemMap.clear();
}
/*
* 更新系统
* @param (number) dt 帧间隔时间
*/
update(dt) {
for (const [systemName, system] of this._systemMap) {
if (system && system.enabled && !system.started) {
this._currentSystem = system;
system.started = true;
}
}
for (const [systemName, system] of this._systemMap) {
if (system && system.enabled && system.started) {
this._currentSystem = system;
system.update(dt);
}
}
for (const [systemName, system] of this._systemMap) {
if (system && system.enabled && system.started) {
this._currentSystem = system;
system.lateUpdate(dt);
}
}
}
}