@vworlds/vecs 1.0.9 → 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.husky/pre-commit +1 -0
- package/README.md +218 -229
- package/dist/command.d.ts +1 -0
- package/dist/command.js +2 -0
- package/dist/command.js.map +1 -0
- package/dist/component.d.ts +51 -59
- package/dist/component.js +31 -25
- package/dist/component.js.map +1 -1
- package/dist/dsl.d.ts +34 -26
- package/dist/dsl.js +46 -20
- package/dist/dsl.js.map +1 -1
- package/dist/entity.d.ts +110 -127
- package/dist/entity.js +323 -164
- package/dist/entity.js.map +1 -1
- package/dist/filter.d.ts +31 -23
- package/dist/filter.js +41 -32
- package/dist/filter.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/package.json +3 -1
- package/dist/phase.d.ts +5 -28
- package/dist/phase.js +11 -10
- package/dist/phase.js.map +1 -1
- package/dist/query.d.ts +128 -94
- package/dist/query.js +254 -145
- package/dist/query.js.map +1 -1
- package/dist/system.d.ts +64 -128
- package/dist/system.js +156 -149
- package/dist/system.js.map +1 -1
- package/dist/util/array_map.d.ts +4 -55
- package/dist/util/array_map.js +35 -37
- package/dist/util/array_map.js.map +1 -1
- package/dist/util/bitset.d.ts +40 -50
- package/dist/util/bitset.js +76 -62
- package/dist/util/bitset.js.map +1 -1
- package/dist/util/events.d.ts +14 -18
- package/dist/util/events.js +24 -3
- package/dist/util/events.js.map +1 -1
- package/dist/util/ordered_set.d.ts +1 -17
- package/dist/util/ordered_set.js +74 -25
- package/dist/util/ordered_set.js.map +1 -1
- package/dist/world.d.ts +222 -201
- package/dist/world.js +394 -323
- package/dist/world.js.map +1 -1
- package/eslint-rules/internal-underscore.js +60 -0
- package/eslint.config.js +5 -0
- package/package.json +3 -1
package/dist/world.js
CHANGED
|
@@ -5,20 +5,26 @@ import { System } from "./system.js";
|
|
|
5
5
|
import { Filter } from "./filter.js";
|
|
6
6
|
import { ArrayMap } from "./util/array_map.js";
|
|
7
7
|
import { Phase } from "./phase.js";
|
|
8
|
+
/**
|
|
9
|
+
* Numeric type ids below this value are reserved for components whose id was
|
|
10
|
+
* pre-registered via {@link World.registerComponentType} (typically server
|
|
11
|
+
* assigned). Auto-assigned ids start here.
|
|
12
|
+
*/
|
|
8
13
|
const LOCAL_COMPONENT_MIN = 256;
|
|
9
14
|
/**
|
|
10
|
-
* The central ECS container.
|
|
15
|
+
* The central ECS container. One world per game session.
|
|
11
16
|
*
|
|
12
|
-
* A `World` owns
|
|
13
|
-
* pipeline.
|
|
17
|
+
* A `World` owns every entity, every registered component class, every
|
|
18
|
+
* registered query / system, and the update pipeline. The typical lifecycle:
|
|
14
19
|
*
|
|
15
|
-
* 1. **Register components** —
|
|
16
|
-
* {@link registerComponentType}) for every component class.
|
|
17
|
-
* 2. **
|
|
18
|
-
*
|
|
20
|
+
* 1. **Register components** — {@link registerComponent} (and optionally
|
|
21
|
+
* {@link registerComponentType}) for every component class you plan to use.
|
|
22
|
+
* 2. **Build the pipeline** — {@link addPhase} for every named phase, then
|
|
23
|
+
* {@link system} / {@link query} for each processor.
|
|
19
24
|
* 3. **Start** — call {@link start} to freeze component registration and
|
|
20
25
|
* distribute systems into their phases.
|
|
21
|
-
* 4. **Run loop** — call {@link runPhase}
|
|
26
|
+
* 4. **Run loop** — call {@link runPhase} per phase or {@link progress} for
|
|
27
|
+
* every phase, once per frame.
|
|
22
28
|
*
|
|
23
29
|
* ```ts
|
|
24
30
|
* const world = new World();
|
|
@@ -28,213 +34,231 @@ const LOCAL_COMPONENT_MIN = 256;
|
|
|
28
34
|
*
|
|
29
35
|
* world.system("Move")
|
|
30
36
|
* .requires(Position, Velocity)
|
|
31
|
-
* .
|
|
37
|
+
* .each([Position, Velocity], (e, [pos, vel]) => {
|
|
38
|
+
* pos.x += vel.vx;
|
|
39
|
+
* });
|
|
32
40
|
*
|
|
33
41
|
* world.start();
|
|
34
42
|
*
|
|
35
43
|
* // game loop:
|
|
36
|
-
* world.
|
|
44
|
+
* world.progress(now, delta);
|
|
37
45
|
* ```
|
|
46
|
+
*
|
|
47
|
+
* ## Deferred mode
|
|
48
|
+
*
|
|
49
|
+
* The world can be in **deferred mode**, in which case entity mutations
|
|
50
|
+
* (`add` / `set` / `remove` / `destroy` / `setParent` / `modified`) are
|
|
51
|
+
* queued instead of applied inline. Systems run inside an automatically
|
|
52
|
+
* deferred scope; user code can wrap arbitrary blocks with
|
|
53
|
+
* {@link beginDefer} / {@link endDefer} or {@link defer}. {@link flush}
|
|
54
|
+
* drains the queue at top level.
|
|
38
55
|
*/
|
|
39
56
|
export class World {
|
|
40
57
|
constructor() {
|
|
41
|
-
|
|
42
|
-
this.
|
|
43
|
-
|
|
44
|
-
this.
|
|
45
|
-
|
|
46
|
-
this.
|
|
47
|
-
|
|
48
|
-
this.
|
|
49
|
-
|
|
50
|
-
this.
|
|
51
|
-
/** @internal */
|
|
58
|
+
/** @internal Entity id → entity. Owns every live entity. */
|
|
59
|
+
this._entities = new Map();
|
|
60
|
+
/** @internal All registered queries, including systems (which extend `Query`). */
|
|
61
|
+
this._queries = [];
|
|
62
|
+
/** @internal Component class → meta record. */
|
|
63
|
+
this._Class2Meta = new Map();
|
|
64
|
+
/** @internal Component type id → meta record. */
|
|
65
|
+
this._Type2Meta = new ArrayMap();
|
|
66
|
+
/** @internal Pre-registered name → type id mappings (server-assigned ids). */
|
|
67
|
+
this._componentNameTypeMap = new Map();
|
|
68
|
+
/** @internal Counter used to auto-assign type ids for "local" components (≥ 256). */
|
|
69
|
+
this._localComponentCounter = LOCAL_COMPONENT_MIN;
|
|
70
|
+
/** @internal `true` once {@link start} (or {@link disableComponentRegistration}) has been called. */
|
|
71
|
+
this._componentRegistrationDisabled = false;
|
|
72
|
+
/** @internal Auto-incrementing entity id counter, seeded by {@link setEntityIdRange}. */
|
|
73
|
+
this._eidCounter = 0;
|
|
74
|
+
/** @internal Single ordered command queue used in deferred mode. */
|
|
75
|
+
this._commandQueue = [];
|
|
76
|
+
/** @internal Nested {@link beginDefer} / {@link endDefer} count. */
|
|
77
|
+
this._deferredDepth = 0;
|
|
78
|
+
/** @internal `true` while {@link _processCommandQueue} is iterating, to avoid re-entrant drains. */
|
|
79
|
+
this._draining = false;
|
|
80
|
+
/** @internal Phase name → phase. Insertion-ordered, matches pipeline execution order. */
|
|
52
81
|
this._pipeline = new Map();
|
|
53
|
-
this.eidCounter = 0;
|
|
54
82
|
}
|
|
55
83
|
/**
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* ```ts
|
|
61
|
-
* const e = world.getOrCreateEntity(snapshot.eid, (e) => {
|
|
62
|
-
* networkEntities.add(e);
|
|
63
|
-
* });
|
|
64
|
-
* e.add(snapshot.type, false);
|
|
65
|
-
* ```
|
|
66
|
-
*
|
|
67
|
-
* @param eid - The entity id to look up or create.
|
|
68
|
-
* @param onCreateCallback - Optional callback invoked only when a **new**
|
|
69
|
-
* entity is created, before it is returned. Use this to initialise
|
|
70
|
-
* bookkeeping (e.g. tracking it in a local set).
|
|
71
|
-
* @returns The existing or newly created entity.
|
|
84
|
+
* @internal Drain the top-level command queue: walk it in arrival order,
|
|
85
|
+
* executing each command. Callbacks may push more commands; they are picked
|
|
86
|
+
* up by index iteration in the same pass.
|
|
72
87
|
*/
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
88
|
+
_processCommandQueue() {
|
|
89
|
+
if (this._draining) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (this._commandQueue.length === 0) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
this._draining = true;
|
|
96
|
+
try {
|
|
97
|
+
for (let i = 0; i < this._commandQueue.length; i++) {
|
|
98
|
+
this._executeCommand(this._commandQueue[i]);
|
|
80
99
|
}
|
|
100
|
+
this._commandQueue.length = 0;
|
|
81
101
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
entity(id) {
|
|
85
|
-
if (id === undefined) {
|
|
86
|
-
const eid = this.eidCounter++;
|
|
87
|
-
const e = new Entity(this, eid);
|
|
88
|
-
this.entities.set(eid, e);
|
|
89
|
-
return e;
|
|
102
|
+
finally {
|
|
103
|
+
this._draining = false;
|
|
90
104
|
}
|
|
91
|
-
return this.entities.get(id);
|
|
92
105
|
}
|
|
93
106
|
/**
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
* Must be called **before** {@link start} (or
|
|
97
|
-
* {@link disableComponentRegistration}). Useful when the world runs alongside
|
|
98
|
-
* a server that owns a different id range — for example, locally-created
|
|
99
|
-
* client entities can start at a high offset to avoid collisions with
|
|
100
|
-
* server-assigned ids.
|
|
101
|
-
*
|
|
102
|
-
* @param min - The first id that will be assigned by {@link entity}.
|
|
103
|
-
* @throws If called after registration has been disabled.
|
|
107
|
+
* @internal Run one command's side effects: data-layer mutation, hook
|
|
108
|
+
* firing, and routing to every registered query / system.
|
|
104
109
|
*/
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
110
|
+
_executeCommand(cmd) {
|
|
111
|
+
switch (cmd.kind) {
|
|
112
|
+
case 0 /* CommandKind.CreateEntity */:
|
|
113
|
+
this._entities.set(cmd.entity.eid, cmd.entity);
|
|
114
|
+
return;
|
|
115
|
+
case 1 /* CommandKind.Set */:
|
|
116
|
+
cmd.entity._set(cmd.type, cmd.props);
|
|
117
|
+
return;
|
|
118
|
+
case 2 /* CommandKind.Modified */:
|
|
119
|
+
cmd.entity._modified(cmd.type);
|
|
120
|
+
return;
|
|
121
|
+
case 3 /* CommandKind.Remove */:
|
|
122
|
+
cmd.entity._remove(cmd.type);
|
|
123
|
+
return;
|
|
124
|
+
case 4 /* CommandKind.Destroy */:
|
|
125
|
+
cmd.entity._destroy();
|
|
126
|
+
return;
|
|
127
|
+
case 5 /* CommandKind.SetParent */:
|
|
128
|
+
cmd.entity._setParent(cmd.parent);
|
|
129
|
+
return;
|
|
108
130
|
}
|
|
109
|
-
this.eidCounter = min;
|
|
110
131
|
}
|
|
111
132
|
/**
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
* @returns The corresponding `ComponentMeta`.
|
|
116
|
-
* @throws If no component with that class or type id has been registered.
|
|
133
|
+
* @internal Distribute every registered system into its phase's `systems`
|
|
134
|
+
* list. Called by {@link start}; idempotent so it can be re-run if the
|
|
135
|
+
* pipeline is rebuilt.
|
|
117
136
|
*/
|
|
118
|
-
|
|
119
|
-
let
|
|
120
|
-
if (
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
else {
|
|
124
|
-
meta = this.Type2Meta.get(typeOrClass);
|
|
125
|
-
}
|
|
126
|
-
if (!meta) {
|
|
127
|
-
throw `unregistered component meta for component type or class '${typeOrClass}'`;
|
|
137
|
+
_reindexSystems() {
|
|
138
|
+
let _defaultPhase = this._pipeline.get("update");
|
|
139
|
+
if (!_defaultPhase) {
|
|
140
|
+
_defaultPhase = new Phase("update", this);
|
|
141
|
+
this._pipeline.set(_defaultPhase.name, _defaultPhase);
|
|
128
142
|
}
|
|
129
|
-
|
|
143
|
+
const defaultPhase = _defaultPhase;
|
|
144
|
+
this._queries.forEach((q) => {
|
|
145
|
+
if (!(q instanceof System)) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
let phase = q._phase;
|
|
149
|
+
if (typeof phase === "string") {
|
|
150
|
+
phase = this._pipeline.get(phase);
|
|
151
|
+
}
|
|
152
|
+
phase = phase || defaultPhase;
|
|
153
|
+
phase.systems.push(q);
|
|
154
|
+
});
|
|
155
|
+
this._pipeline.forEach((phase) => {
|
|
156
|
+
console.log("Phase %s : %s", phase.name, phase.systems.map((s) => s.name).join(" -> "));
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
/** @internal Append a command to the deferred-mode queue. */
|
|
160
|
+
_enqueue(cmd) {
|
|
161
|
+
this._commandQueue.push(cmd);
|
|
162
|
+
}
|
|
163
|
+
/** @internal Register a freshly created {@link Query} (called from its constructor). */
|
|
164
|
+
_addQuery(q) {
|
|
165
|
+
this._queries.push(q);
|
|
130
166
|
}
|
|
131
167
|
/**
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
* @param typeOrClass - A component class constructor or a numeric type id.
|
|
135
|
-
* @returns The numeric type id.
|
|
168
|
+
* @internal Unregister a query and purge its membership from every entity.
|
|
169
|
+
* Called by {@link Query.destroy}.
|
|
136
170
|
*/
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
171
|
+
_removeQuery(q) {
|
|
172
|
+
const idx = this._queries.indexOf(q);
|
|
173
|
+
if (idx !== -1) {
|
|
174
|
+
this._queries.splice(idx, 1);
|
|
140
175
|
}
|
|
141
|
-
|
|
176
|
+
this._entities.forEach((e) => e._purgeQuery(q));
|
|
177
|
+
}
|
|
178
|
+
/** @internal Remove an entity from the world's entity map (called by `Entity._destroy`). */
|
|
179
|
+
_unregisterEntity(entity) {
|
|
180
|
+
this._entities.delete(entity.eid);
|
|
181
|
+
}
|
|
182
|
+
/** Read-only view of the live entities, keyed by entity id. */
|
|
183
|
+
get entities() {
|
|
184
|
+
return this._entities;
|
|
185
|
+
}
|
|
186
|
+
/** Read-only view of every registered query (includes systems). */
|
|
187
|
+
get queries() {
|
|
188
|
+
return this._queries;
|
|
142
189
|
}
|
|
143
190
|
/**
|
|
144
|
-
*
|
|
145
|
-
*
|
|
191
|
+
* `true` while the world is in deferred mode — entity mutations are queued
|
|
192
|
+
* rather than applied inline. Equivalent to "the queue depth is non-zero or
|
|
193
|
+
* the world is currently draining".
|
|
194
|
+
*/
|
|
195
|
+
get deferred() {
|
|
196
|
+
return this._deferredDepth > 0 || this._draining;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Enter deferred mode. Mutations made until the matching {@link endDefer}
|
|
200
|
+
* are queued instead of executing inline.
|
|
146
201
|
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
202
|
+
* Nested `beginDefer` / `endDefer` pairs are allowed; only the outermost
|
|
203
|
+
* `endDefer` triggers a queue drain.
|
|
204
|
+
*/
|
|
205
|
+
beginDefer() {
|
|
206
|
+
this._deferredDepth++;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Leave deferred mode. When the depth returns to zero the world drains the
|
|
210
|
+
* command queue (firing hooks and routing enter / exit / update events).
|
|
211
|
+
*/
|
|
212
|
+
endDefer() {
|
|
213
|
+
this._deferredDepth--;
|
|
214
|
+
this.flush();
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Run `fn` inside a deferred scope. Equivalent to
|
|
218
|
+
* `beginDefer(); try { fn(); } finally { endDefer(); }`.
|
|
149
219
|
*
|
|
150
|
-
* @
|
|
151
|
-
* {@link Entity.remove}.
|
|
220
|
+
* @param fn - Callback executed in deferred mode.
|
|
152
221
|
*/
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
222
|
+
defer(fn) {
|
|
223
|
+
this.beginDefer();
|
|
224
|
+
try {
|
|
225
|
+
fn();
|
|
156
226
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
e.children.forEach((child) => this.archetypeChanged(child));
|
|
160
|
-
}
|
|
161
|
-
/** @internal */
|
|
162
|
-
_notifyComponentAdded(e, c) {
|
|
163
|
-
this.archetypeChanged(e);
|
|
164
|
-
}
|
|
165
|
-
/** @internal */
|
|
166
|
-
_notifyComponentRemoved(e, c) {
|
|
167
|
-
const hook = c.meta._onRemoveHandler;
|
|
168
|
-
if (hook) {
|
|
169
|
-
hook(c);
|
|
227
|
+
finally {
|
|
228
|
+
this.endDefer();
|
|
170
229
|
}
|
|
171
|
-
this.archetypeChanged(e);
|
|
172
230
|
}
|
|
173
|
-
/**
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
this.
|
|
182
|
-
|
|
183
|
-
updateArchetypes() {
|
|
184
|
-
if (this.archChangeQueue.length > 0) {
|
|
185
|
-
this.allQueries.forEach((q) => {
|
|
186
|
-
this.archChangeQueue.forEach((e) => {
|
|
187
|
-
if (q.belongs(e)) {
|
|
188
|
-
e._addQuery(q);
|
|
189
|
-
}
|
|
190
|
-
else {
|
|
191
|
-
e._removeQuery(q);
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
});
|
|
195
|
-
this.archChangeQueue.forEach((e) => {
|
|
196
|
-
e.clearDeletedComponents();
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
if (this.destroyedEntities.length > 0) {
|
|
200
|
-
this.destroyedEntities.forEach((e) => {
|
|
201
|
-
e._destroy();
|
|
202
|
-
});
|
|
203
|
-
this.destroyedEntities.length = 0;
|
|
204
|
-
}
|
|
205
|
-
if (this.updatedComponents.length > 0) {
|
|
206
|
-
this.updatedComponents.forEach((c) => {
|
|
207
|
-
const hook = c.meta._onSetHandler;
|
|
208
|
-
if (hook) {
|
|
209
|
-
hook(c);
|
|
210
|
-
}
|
|
211
|
-
c.entity._notifyModified(c);
|
|
212
|
-
c._dirty = false;
|
|
213
|
-
});
|
|
214
|
-
this.updatedComponents.length = 0;
|
|
215
|
-
}
|
|
216
|
-
if (this.archChangeQueue.length > 0) {
|
|
217
|
-
this.archChangeQueue.forEach((e) => {
|
|
218
|
-
e._updateQueries();
|
|
219
|
-
e._archetypeChanged = false;
|
|
220
|
-
});
|
|
221
|
-
this.archChangeQueue.length = 0;
|
|
231
|
+
/**
|
|
232
|
+
* Drain any commands queued at the top level (depth 0).
|
|
233
|
+
*
|
|
234
|
+
* Call between phases or after batch-loading network snapshots to surface
|
|
235
|
+
* accumulated mutations (firing hooks and routing enter / exit / update)
|
|
236
|
+
* before the next read or system run.
|
|
237
|
+
*/
|
|
238
|
+
flush() {
|
|
239
|
+
if (this._deferredDepth === 0) {
|
|
240
|
+
this._processCommandQueue();
|
|
222
241
|
}
|
|
223
242
|
}
|
|
224
|
-
/**
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
243
|
+
/**
|
|
244
|
+
* Pre-register a `componentName → typeId` mapping without binding a class.
|
|
245
|
+
*
|
|
246
|
+
* Useful when network messages refer to components by type id and the
|
|
247
|
+
* corresponding class may be registered later. Call this **before**
|
|
248
|
+
* {@link registerComponent} so the class picks up the server-assigned id
|
|
249
|
+
* rather than a locally generated one.
|
|
250
|
+
*
|
|
251
|
+
* @param componentName - String name used in network payloads.
|
|
252
|
+
* @param type - Numeric type id assigned by the server.
|
|
253
|
+
*/
|
|
254
|
+
registerComponentType(componentName, type) {
|
|
255
|
+
this._componentNameTypeMap.set(componentName, type);
|
|
231
256
|
}
|
|
232
257
|
registerComponent(ComponentClass, typeOrComponentName, componentName) {
|
|
233
|
-
if (this.
|
|
258
|
+
if (this._componentRegistrationDisabled) {
|
|
234
259
|
throw "World component registartion is disabled";
|
|
235
260
|
}
|
|
236
261
|
let type = undefined;
|
|
237
|
-
// Determine if the second argument is type or componentName based on its type
|
|
238
262
|
if (typeof typeOrComponentName === "number") {
|
|
239
263
|
type = typeOrComponentName;
|
|
240
264
|
}
|
|
@@ -244,82 +268,202 @@ export class World {
|
|
|
244
268
|
componentName = componentName || ComponentClass.name;
|
|
245
269
|
let local = false;
|
|
246
270
|
if (type === undefined) {
|
|
247
|
-
|
|
248
|
-
type = this.componentNameTypeMap.get(componentName);
|
|
271
|
+
type = this._componentNameTypeMap.get(componentName);
|
|
249
272
|
if (type === undefined) {
|
|
250
|
-
type = this.
|
|
273
|
+
type = this._localComponentCounter++;
|
|
251
274
|
local = true;
|
|
252
275
|
}
|
|
253
276
|
}
|
|
254
|
-
let meta = this.
|
|
277
|
+
let meta = this._Class2Meta.get(ComponentClass);
|
|
255
278
|
if (meta) {
|
|
256
279
|
if (local) {
|
|
257
|
-
this.
|
|
280
|
+
this._localComponentCounter--;
|
|
258
281
|
}
|
|
259
282
|
throw `Trying to register ${componentName} with type=${type} which is already registered to ${meta.componentName}`;
|
|
260
283
|
}
|
|
261
284
|
this.registerComponentType(componentName, type);
|
|
262
285
|
meta = new ComponentMeta(ComponentClass, type, componentName);
|
|
263
|
-
this.
|
|
264
|
-
this.
|
|
286
|
+
this._Class2Meta.set(ComponentClass, meta);
|
|
287
|
+
this._Type2Meta.set(type, meta);
|
|
265
288
|
console.log("Registered component %s with type=%d as %s component", componentName, type, local ? "local" : "networked");
|
|
266
289
|
}
|
|
267
290
|
/**
|
|
268
|
-
*
|
|
269
|
-
* class.
|
|
291
|
+
* Look up the {@link ComponentMeta} for a registered component.
|
|
270
292
|
*
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
|
|
293
|
+
* @param typeOrClass - Component class or numeric type id.
|
|
294
|
+
* @returns The corresponding meta record.
|
|
295
|
+
* @throws When no component with that class or type id has been registered.
|
|
296
|
+
*/
|
|
297
|
+
getComponentMeta(typeOrClass) {
|
|
298
|
+
let meta;
|
|
299
|
+
if (typeof typeOrClass === "function") {
|
|
300
|
+
meta = this._Class2Meta.get(typeOrClass);
|
|
301
|
+
}
|
|
302
|
+
else {
|
|
303
|
+
meta = this._Type2Meta.get(typeOrClass);
|
|
304
|
+
}
|
|
305
|
+
if (!meta) {
|
|
306
|
+
throw `unregistered component meta for component type or class '${typeOrClass}'`;
|
|
307
|
+
}
|
|
308
|
+
return meta;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Resolve a component class or type id to its numeric type id.
|
|
275
312
|
*
|
|
276
|
-
* @param
|
|
277
|
-
* @
|
|
313
|
+
* @param typeOrClass - Component class or numeric type id.
|
|
314
|
+
* @returns The numeric type id.
|
|
278
315
|
*/
|
|
279
|
-
|
|
280
|
-
|
|
316
|
+
getComponentType(typeOrClass) {
|
|
317
|
+
if (typeof typeOrClass === "function") {
|
|
318
|
+
return this.getComponentMeta(typeOrClass).type;
|
|
319
|
+
}
|
|
320
|
+
return typeOrClass;
|
|
281
321
|
}
|
|
282
|
-
/**
|
|
283
|
-
|
|
284
|
-
|
|
322
|
+
/**
|
|
323
|
+
* Return the {@link Hook} for a component class.
|
|
324
|
+
*
|
|
325
|
+
* Hooks let you react to component lifecycle events (add / remove / set)
|
|
326
|
+
* without building a full {@link System}. The same hook is returned on every
|
|
327
|
+
* call — handlers stack on the underlying meta record.
|
|
328
|
+
*
|
|
329
|
+
* ```ts
|
|
330
|
+
* world.hook(Sprite)
|
|
331
|
+
* .onAdd(c => c.initialize(scene))
|
|
332
|
+
* .onRemove(c => c.destroy());
|
|
333
|
+
* ```
|
|
334
|
+
*
|
|
335
|
+
* @param C - Component class.
|
|
336
|
+
* @returns The hook bound to that component type.
|
|
337
|
+
*/
|
|
338
|
+
hook(C) {
|
|
339
|
+
return this.getComponentMeta(C);
|
|
285
340
|
}
|
|
286
|
-
/**
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
341
|
+
/**
|
|
342
|
+
* Declare a group of mutually exclusive components.
|
|
343
|
+
*
|
|
344
|
+
* Adding any component in the group to an entity that already has another
|
|
345
|
+
* member of the group automatically removes the previous member. Members
|
|
346
|
+
* not in the group are unaffected.
|
|
347
|
+
*
|
|
348
|
+
* ```ts
|
|
349
|
+
* world.setExclusiveComponents(Walking, Running, Idle);
|
|
350
|
+
* entity.add(Walking);
|
|
351
|
+
* entity.add(Running); // Walking is removed automatically
|
|
352
|
+
* ```
|
|
353
|
+
*
|
|
354
|
+
* Each call defines one independent group. A component may belong to at
|
|
355
|
+
* most one group at a time; calling {@link setExclusiveComponents} with the
|
|
356
|
+
* same class again overwrites its group. Safe to call before or after
|
|
357
|
+
* {@link start}.
|
|
358
|
+
*
|
|
359
|
+
* @param components - Two or more component classes that cannot coexist.
|
|
360
|
+
* @throws When any class has not been registered.
|
|
361
|
+
*/
|
|
362
|
+
setExclusiveComponents(...components) {
|
|
363
|
+
const types = components.map((C) => this.getComponentType(C));
|
|
364
|
+
for (let i = 0; i < components.length; i++) {
|
|
365
|
+
this.getComponentMeta(components[i]).exclusive = types.filter((_, j) => j !== i);
|
|
291
366
|
}
|
|
292
|
-
this.entities.forEach((e) => e._purgeQuery(q));
|
|
293
367
|
}
|
|
294
|
-
/**
|
|
295
|
-
|
|
296
|
-
|
|
368
|
+
/**
|
|
369
|
+
* Set the starting value of the auto-incrementing entity id counter.
|
|
370
|
+
*
|
|
371
|
+
* Must be called **before** {@link start} (or
|
|
372
|
+
* {@link disableComponentRegistration}). Useful when the world runs
|
|
373
|
+
* alongside a server that owns a different id range — locally created
|
|
374
|
+
* client entities can start at a high offset to avoid collisions with
|
|
375
|
+
* server-assigned ids.
|
|
376
|
+
*
|
|
377
|
+
* @param min - First id assigned by {@link entity}.
|
|
378
|
+
* @throws When called after registration has been disabled.
|
|
379
|
+
*/
|
|
380
|
+
setEntityIdRange(min) {
|
|
381
|
+
if (this._componentRegistrationDisabled) {
|
|
382
|
+
throw "setEntityIdRange must be called before component registration is disabled";
|
|
383
|
+
}
|
|
384
|
+
this._eidCounter = min;
|
|
297
385
|
}
|
|
298
386
|
/**
|
|
299
|
-
*
|
|
387
|
+
* Return the entity with id `eid`, creating it if it does not yet exist.
|
|
388
|
+
*
|
|
389
|
+
* Used by networking code to materialise server-assigned entities:
|
|
390
|
+
*
|
|
391
|
+
* ```ts
|
|
392
|
+
* const e = world.getOrCreateEntity(snapshot.eid, (e) => {
|
|
393
|
+
* networkEntities.add(e);
|
|
394
|
+
* });
|
|
395
|
+
* e.add(snapshot.type);
|
|
396
|
+
* ```
|
|
397
|
+
*
|
|
398
|
+
* @param eid - Entity id to look up or create.
|
|
399
|
+
* @param onCreateCallback - Optional callback invoked only when a new
|
|
400
|
+
* entity is created, before it is returned. Use it to initialise
|
|
401
|
+
* bookkeeping (e.g. tracking it in a local set).
|
|
402
|
+
* @returns The existing or newly created entity.
|
|
403
|
+
*/
|
|
404
|
+
getOrCreateEntity(eid, onCreateCallback) {
|
|
405
|
+
let e = this._entities.get(eid);
|
|
406
|
+
if (!e) {
|
|
407
|
+
e = new Entity(this, eid);
|
|
408
|
+
this._entities.set(eid, e);
|
|
409
|
+
if (onCreateCallback) {
|
|
410
|
+
onCreateCallback(e);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return e;
|
|
414
|
+
}
|
|
415
|
+
entity(id) {
|
|
416
|
+
if (id === undefined) {
|
|
417
|
+
const eid = this._eidCounter++;
|
|
418
|
+
const e = new Entity(this, eid);
|
|
419
|
+
if (this.deferred) {
|
|
420
|
+
this._enqueue({ kind: 0 /* CommandKind.CreateEntity */, entity: e });
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
this._entities.set(eid, e);
|
|
424
|
+
}
|
|
425
|
+
return e;
|
|
426
|
+
}
|
|
427
|
+
return this._entities.get(id);
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Destroy every entity currently tracked by the world.
|
|
431
|
+
*
|
|
432
|
+
* Triggers all `onRemove` hooks and `exit` callbacks. Useful when
|
|
433
|
+
* transitioning between game sessions or resetting to a clean state.
|
|
434
|
+
*/
|
|
435
|
+
clearAllEntities() {
|
|
436
|
+
this._entities.forEach((e) => {
|
|
437
|
+
e.destroy();
|
|
438
|
+
});
|
|
439
|
+
this.flush();
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Create, register, and return a new {@link System}, ready for fluent
|
|
443
|
+
* configuration.
|
|
300
444
|
*
|
|
301
445
|
* ```ts
|
|
302
446
|
* world.system("Render")
|
|
303
447
|
* .phase("update")
|
|
304
448
|
* .requires(Position, Sprite)
|
|
305
449
|
* .enter([Sprite], (e, [sprite]) => sprite.initialize(scene))
|
|
306
|
-
* .
|
|
450
|
+
* .each([Position, Sprite], (e, [pos, sprite]) => sprite.draw(pos.x, pos.y));
|
|
307
451
|
* ```
|
|
308
452
|
*
|
|
309
|
-
* @param name -
|
|
310
|
-
* @returns The new
|
|
453
|
+
* @param name - Unique display name for the system.
|
|
454
|
+
* @returns The new system.
|
|
311
455
|
*/
|
|
312
456
|
system(name) {
|
|
313
457
|
return new System(name, this);
|
|
314
458
|
}
|
|
315
459
|
/**
|
|
316
|
-
* Create a standalone {@link Query},
|
|
460
|
+
* Create, register, and return a standalone {@link Query}, ready for fluent
|
|
317
461
|
* configuration.
|
|
318
462
|
*
|
|
319
463
|
* Unlike a {@link System}, a standalone query has no phase and no per-tick
|
|
320
|
-
* callbacks — it is a reactive
|
|
321
|
-
*
|
|
322
|
-
*
|
|
464
|
+
* callbacks — it is a reactive entity set that can be read at any time. It
|
|
465
|
+
* can also be created **after** {@link start}; existing matched entities
|
|
466
|
+
* are backfilled immediately.
|
|
323
467
|
*
|
|
324
468
|
* ```ts
|
|
325
469
|
* const enemies = world.query("Enemies")
|
|
@@ -330,8 +474,8 @@ export class World {
|
|
|
330
474
|
* // enemies.entities is kept up-to-date automatically
|
|
331
475
|
* ```
|
|
332
476
|
*
|
|
333
|
-
* @param name -
|
|
334
|
-
* @returns The new
|
|
477
|
+
* @param name - Unique display name for the query.
|
|
478
|
+
* @returns The new query.
|
|
335
479
|
*/
|
|
336
480
|
query(name) {
|
|
337
481
|
return new Query(name, this);
|
|
@@ -340,75 +484,10 @@ export class World {
|
|
|
340
484
|
return new Filter(this, q);
|
|
341
485
|
}
|
|
342
486
|
/**
|
|
343
|
-
*
|
|
487
|
+
* Add a named phase to the update pipeline.
|
|
344
488
|
*
|
|
345
|
-
*
|
|
346
|
-
*
|
|
347
|
-
*/
|
|
348
|
-
disableComponentRegistration() {
|
|
349
|
-
this.componentRegistrationDisabled = true;
|
|
350
|
-
}
|
|
351
|
-
/**
|
|
352
|
-
* Freeze component registration and prepare the world for running.
|
|
353
|
-
*
|
|
354
|
-
* Distributes all systems registered so far into their pipeline phases
|
|
355
|
-
* (defaulting to `"update"`) and logs the phase → system order to the
|
|
356
|
-
* console. Systems and queries can still be created after this call —
|
|
357
|
-
* standalone queries will immediately backfill existing matched entities.
|
|
358
|
-
*
|
|
359
|
-
* Call this once before the first {@link runPhase} call.
|
|
360
|
-
*/
|
|
361
|
-
start() {
|
|
362
|
-
this.componentRegistrationDisabled = true;
|
|
363
|
-
this.reindexSystems();
|
|
364
|
-
}
|
|
365
|
-
reindexSystems() {
|
|
366
|
-
let _defaultPhase = this._pipeline.get("update");
|
|
367
|
-
if (!_defaultPhase) {
|
|
368
|
-
_defaultPhase = new Phase("update", this);
|
|
369
|
-
this._pipeline.set(_defaultPhase.name, _defaultPhase);
|
|
370
|
-
}
|
|
371
|
-
const defaultPhase = _defaultPhase;
|
|
372
|
-
this.allQueries.forEach((q) => {
|
|
373
|
-
if (!(q instanceof System)) {
|
|
374
|
-
return;
|
|
375
|
-
}
|
|
376
|
-
let phase = q._phase;
|
|
377
|
-
if (typeof phase === "string") {
|
|
378
|
-
phase = this._pipeline.get(phase);
|
|
379
|
-
}
|
|
380
|
-
phase = phase || defaultPhase;
|
|
381
|
-
phase.systems.push(q);
|
|
382
|
-
});
|
|
383
|
-
this._pipeline.forEach((phase) => {
|
|
384
|
-
console.log("Phase %s : %s", phase.name, phase.systems.map((s) => s.name).join(" -> "));
|
|
385
|
-
});
|
|
386
|
-
}
|
|
387
|
-
/**
|
|
388
|
-
* Return the {@link Hook} for a component class.
|
|
389
|
-
*
|
|
390
|
-
* Hooks let you react to component lifecycle events (add / remove / set)
|
|
391
|
-
* without building a full {@link System}. The hook is backed by the
|
|
392
|
-
* component's {@link ComponentMeta} and the same object is returned on every
|
|
393
|
-
* call.
|
|
394
|
-
*
|
|
395
|
-
* ```ts
|
|
396
|
-
* world.hook(Sprite)
|
|
397
|
-
* .onAdd(c => c.initialize(scene))
|
|
398
|
-
* .onRemove(c => c.destroy());
|
|
399
|
-
* ```
|
|
400
|
-
*
|
|
401
|
-
* @param C - The component class.
|
|
402
|
-
* @returns The `Hook` for that component type.
|
|
403
|
-
*/
|
|
404
|
-
hook(C) {
|
|
405
|
-
return this.getComponentMeta(C);
|
|
406
|
-
}
|
|
407
|
-
/**
|
|
408
|
-
* Add a named phase to the update pipeline and return it.
|
|
409
|
-
*
|
|
410
|
-
* Phases are executed in insertion order when you call {@link runPhase} for
|
|
411
|
-
* each one. Systems are assigned to a phase via {@link System.phase}.
|
|
489
|
+
* Phases are executed in insertion order when {@link runPhase} or
|
|
490
|
+
* {@link progress} is called. Systems join a phase via {@link System.phase}.
|
|
412
491
|
*
|
|
413
492
|
* ```ts
|
|
414
493
|
* const preUpdate = world.addPhase("preupdate");
|
|
@@ -417,7 +496,7 @@ export class World {
|
|
|
417
496
|
* ```
|
|
418
497
|
*
|
|
419
498
|
* @param name - Unique phase name. Systems can reference it by this string.
|
|
420
|
-
* @returns The new
|
|
499
|
+
* @returns The new phase.
|
|
421
500
|
*/
|
|
422
501
|
addPhase(name) {
|
|
423
502
|
const phase = new Phase(name, this);
|
|
@@ -425,67 +504,59 @@ export class World {
|
|
|
425
504
|
return phase;
|
|
426
505
|
}
|
|
427
506
|
/**
|
|
428
|
-
*
|
|
507
|
+
* Prevent any further calls to {@link registerComponent}.
|
|
508
|
+
*
|
|
509
|
+
* Called automatically by {@link start}. Call directly if you want to lock
|
|
510
|
+
* registration before the rest of the systems are wired up.
|
|
511
|
+
*/
|
|
512
|
+
disableComponentRegistration() {
|
|
513
|
+
this._componentRegistrationDisabled = true;
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Freeze component registration and prepare the world for running.
|
|
429
517
|
*
|
|
430
|
-
*
|
|
431
|
-
*
|
|
432
|
-
*
|
|
518
|
+
* Distributes every system registered so far into its phase (defaulting to
|
|
519
|
+
* `"update"`) and logs the phase → system order to the console. Systems
|
|
520
|
+
* and queries can still be created after this call — standalone queries
|
|
521
|
+
* backfill existing matched entities immediately.
|
|
522
|
+
*
|
|
523
|
+
* Call once before the first {@link runPhase} / {@link progress}.
|
|
524
|
+
*/
|
|
525
|
+
start() {
|
|
526
|
+
this._componentRegistrationDisabled = true;
|
|
527
|
+
this._reindexSystems();
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Execute every system in `phase` for one tick.
|
|
433
531
|
*
|
|
434
|
-
*
|
|
532
|
+
* Pending top-level mutations are drained before the first system runs so
|
|
533
|
+
* each system observes a consistent world. Each system body executes in a
|
|
534
|
+
* deferred scope; mutations made by callbacks land in the world queue and
|
|
535
|
+
* are processed before the next system runs.
|
|
536
|
+
*
|
|
537
|
+
* @param phase - Phase reference returned from {@link addPhase}.
|
|
435
538
|
* @param now - Absolute timestamp in milliseconds (e.g. `Date.now()`).
|
|
436
539
|
* @param delta - Milliseconds elapsed since the previous tick.
|
|
437
540
|
*/
|
|
438
541
|
runPhase(phase, now, delta) {
|
|
542
|
+
this.flush();
|
|
439
543
|
phase.systems.forEach((s) => {
|
|
440
544
|
s._run(now, delta);
|
|
441
|
-
this.updateArchetypes();
|
|
442
545
|
});
|
|
443
546
|
}
|
|
444
547
|
/**
|
|
445
|
-
* Run every phase in the pipeline in
|
|
446
|
-
*
|
|
447
|
-
* {@link runPhase} for each phase manually.
|
|
548
|
+
* Run every phase in the pipeline in registration order.
|
|
549
|
+
*
|
|
550
|
+
* Equivalent to calling {@link runPhase} for each phase manually.
|
|
448
551
|
*
|
|
449
552
|
* @param now - Absolute timestamp in milliseconds (e.g. `Date.now()`).
|
|
450
553
|
* @param delta - Milliseconds elapsed since the previous tick.
|
|
451
554
|
*/
|
|
452
555
|
progress(now, delta) {
|
|
453
|
-
this.
|
|
556
|
+
this.flush();
|
|
454
557
|
this._pipeline.forEach((phase) => {
|
|
455
558
|
this.runPhase(phase, now, delta);
|
|
456
559
|
});
|
|
457
560
|
}
|
|
458
|
-
/**
|
|
459
|
-
* Declare a group of mutually exclusive components.
|
|
460
|
-
*
|
|
461
|
-
* After this call, adding any component in the group to an entity that
|
|
462
|
-
* already has another component from the same group will remove the other component
|
|
463
|
-
*
|
|
464
|
-
* ```ts
|
|
465
|
-
* world.setExclusiveComponents(Walking, Running, Idle);
|
|
466
|
-
* // entity.add(Running) throws if entity already has Walking or Idle
|
|
467
|
-
* ```
|
|
468
|
-
*
|
|
469
|
-
* @param components - Two or more component classes that cannot coexist.
|
|
470
|
-
* @throws If any class has not been registered.
|
|
471
|
-
*/
|
|
472
|
-
setExclusiveComponents(...components) {
|
|
473
|
-
const types = components.map((C) => this.getComponentType(C));
|
|
474
|
-
for (let i = 0; i < components.length; i++) {
|
|
475
|
-
this.getComponentMeta(components[i]).exclusive = types.filter((_, j) => j !== i);
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
/**
|
|
479
|
-
* Destroy every entity currently tracked by the world.
|
|
480
|
-
*
|
|
481
|
-
* Triggers all `onRemove` hooks and `exit` callbacks. Useful when
|
|
482
|
-
* transitioning between game sessions or resetting to a clean state.
|
|
483
|
-
*/
|
|
484
|
-
clearAllEntities() {
|
|
485
|
-
this.entities.forEach((e) => {
|
|
486
|
-
e.destroy();
|
|
487
|
-
});
|
|
488
|
-
this.entities.clear();
|
|
489
|
-
}
|
|
490
561
|
}
|
|
491
562
|
//# sourceMappingURL=world.js.map
|