forked from ecsyjs/ecsy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorld.js
95 lines (75 loc) · 2.09 KB
/
World.js
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
import { SystemManager } from "./SystemManager.js";
import { EntityManager } from "./EntityManager.js";
import { ComponentManager } from "./ComponentManager.js";
import { Version } from "./Version.js";
import { hasWindow, now } from "./Utils.js";
import { Entity } from "./Entity.js";
const DEFAULT_OPTIONS = {
entityPoolSize: 0,
entityClass: Entity,
};
export class World {
constructor(options = {}) {
this.options = Object.assign({}, DEFAULT_OPTIONS, options);
this.componentsManager = new ComponentManager(this);
this.entityManager = new EntityManager(this);
this.systemManager = new SystemManager(this);
this.enabled = true;
this.eventQueues = {};
if (hasWindow && typeof CustomEvent !== "undefined") {
var event = new CustomEvent("ecsy-world-created", {
detail: { world: this, version: Version },
});
window.dispatchEvent(event);
}
this.lastTime = now() / 1000;
}
registerComponent(Component, objectPool) {
this.componentsManager.registerComponent(Component, objectPool);
return this;
}
registerSystem(System, attributes) {
this.systemManager.registerSystem(System, attributes);
return this;
}
hasRegisteredComponent(Component) {
return this.componentsManager.hasComponent(Component);
}
unregisterSystem(System) {
this.systemManager.unregisterSystem(System);
return this;
}
getSystem(SystemClass) {
return this.systemManager.getSystem(SystemClass);
}
getSystems() {
return this.systemManager.getSystems();
}
execute(delta, time) {
if (!delta) {
time = now() / 1000;
delta = time - this.lastTime;
this.lastTime = time;
}
if (this.enabled) {
this.systemManager.execute(delta, time);
this.entityManager.processDeferredRemoval();
}
}
stop() {
this.enabled = false;
}
play() {
this.enabled = true;
}
createEntity(name) {
return this.entityManager.createEntity(name);
}
stats() {
var stats = {
entities: this.entityManager.stats(),
system: this.systemManager.stats(),
};
return stats;
}
}