@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/entity.js
CHANGED
|
@@ -2,153 +2,276 @@ import { ArrayMap } from "./util/array_map.js";
|
|
|
2
2
|
import { Events } from "./util/events.js";
|
|
3
3
|
import { Bitset } from "./util/bitset.js";
|
|
4
4
|
/**
|
|
5
|
-
* A game object
|
|
6
|
-
* {@link Component | components} attached
|
|
5
|
+
* A game object: a unique numeric id with an arbitrary set of
|
|
6
|
+
* {@link Component | components} attached.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* external authority
|
|
8
|
+
* Never instantiate `Entity` directly. Use {@link World.entity} for an
|
|
9
|
+
* auto-assigned id, or {@link World.getOrCreateEntity} when the id comes from
|
|
10
|
+
* an external authority such as a game server:
|
|
11
11
|
*
|
|
12
12
|
* ```ts
|
|
13
13
|
* const e = world.entity();
|
|
14
14
|
* e.set(Position, { x: 100 });
|
|
15
15
|
* ```
|
|
16
16
|
*
|
|
17
|
-
* Entities support a parent–child hierarchy.
|
|
18
|
-
*
|
|
17
|
+
* Entities support a parent–child hierarchy. `parent` and `children` form a
|
|
18
|
+
* bidirectional link maintained by {@link setParent}; the children set is
|
|
19
|
+
* created lazily. Destroying a parent recursively destroys its children.
|
|
20
|
+
*
|
|
21
|
+
* ## Deferred semantics
|
|
22
|
+
*
|
|
23
|
+
* Inside a system body or `forEach` iteration the world is in **deferred
|
|
24
|
+
* mode**: `add` / `set` / `modified` / `remove` / `destroy` / `setParent` only
|
|
25
|
+
* enqueue commands. The data layer (the components map and `componentBitmask`)
|
|
26
|
+
* is mutated when the world drains its queue. Concretely, while deferred:
|
|
27
|
+
*
|
|
28
|
+
* - `entity.get(C)` returns `undefined` after `entity.add(C)` (no instance yet).
|
|
29
|
+
* - `entity.get(C)` returns the previous value after `entity.set(C, props)`.
|
|
30
|
+
* - `entity.get(C)` still returns the component after `entity.remove(C)`.
|
|
31
|
+
*
|
|
32
|
+
* Outside deferred mode the same calls execute inline and mutations are
|
|
33
|
+
* visible immediately.
|
|
19
34
|
*/
|
|
20
35
|
export class Entity {
|
|
21
36
|
constructor(
|
|
22
|
-
/**
|
|
37
|
+
/** World that owns this entity. */
|
|
23
38
|
world,
|
|
24
|
-
/** Unique numeric entity id assigned at creation
|
|
39
|
+
/** Unique numeric entity id assigned at creation. */
|
|
25
40
|
eid) {
|
|
26
41
|
this.world = world;
|
|
27
42
|
this.eid = eid;
|
|
28
|
-
|
|
29
|
-
this.
|
|
43
|
+
/** @internal Maps numeric component type id to component instance. */
|
|
44
|
+
this._components = new ArrayMap();
|
|
45
|
+
/** @internal Set of queries this entity currently belongs to. */
|
|
46
|
+
this._queries = new Set();
|
|
30
47
|
/**
|
|
31
|
-
* Bitmask
|
|
32
|
-
*
|
|
33
|
-
* predicates.
|
|
48
|
+
* Bitmask of component type ids currently attached to this entity. Used by
|
|
49
|
+
* the world for fast archetype matching against query predicates.
|
|
34
50
|
*/
|
|
35
51
|
this.componentBitmask = new Bitset();
|
|
36
|
-
this.queries = new Set();
|
|
37
|
-
this.newQueries = [];
|
|
38
52
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
53
|
+
* Free-form property bag. Modules can use it to associate arbitrary data with
|
|
54
|
+
* an entity without registering a dedicated component.
|
|
41
55
|
*/
|
|
42
56
|
this.properties = new Map();
|
|
43
|
-
this.
|
|
44
|
-
this.
|
|
57
|
+
/** @internal Set to `true` after the world has fully torn down this entity. */
|
|
58
|
+
this._destroyed = false;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Re-evaluate every world query for this entity, firing `_enter` / `_exit`
|
|
62
|
+
* routing whenever membership flipped.
|
|
63
|
+
*/
|
|
64
|
+
_updateQueries() {
|
|
65
|
+
this.world.queries.forEach((q) => {
|
|
66
|
+
const belongs = q.belongs(this);
|
|
67
|
+
const isIn = this._isInQuery(q);
|
|
68
|
+
if (belongs !== isIn) {
|
|
69
|
+
belongs ? q._enter(this) : q._exit(this);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
45
72
|
}
|
|
46
73
|
/**
|
|
47
|
-
*
|
|
74
|
+
* Construct a fresh component of `type`, apply `props`, store it on this
|
|
75
|
+
* entity, fire the `onAdd` hook, and route query updates.
|
|
48
76
|
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* both will keep the parent–child links consistent.
|
|
77
|
+
* If the component type belongs to an exclusivity group, any conflicting
|
|
78
|
+
* component already on this entity is removed first.
|
|
52
79
|
*/
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
80
|
+
_new(type, props) {
|
|
81
|
+
const meta = this.world.getComponentMeta(type);
|
|
82
|
+
if (meta.exclusive) {
|
|
83
|
+
for (const exclusiveType of meta.exclusive) {
|
|
84
|
+
if (this._components.has(exclusiveType)) {
|
|
85
|
+
this._remove(exclusiveType);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
56
88
|
}
|
|
57
|
-
return this._children;
|
|
58
|
-
}
|
|
59
|
-
getComponentInstance(meta) {
|
|
60
89
|
const c = new meta.Class(this, meta);
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
90
|
+
if (props !== undefined) {
|
|
91
|
+
Object.assign(c, props);
|
|
92
|
+
}
|
|
93
|
+
this._components.set(type, c);
|
|
94
|
+
this.componentBitmask.add(type);
|
|
95
|
+
if (meta._onAddHandlers) {
|
|
96
|
+
meta._onAddHandlers.forEach((handler) => handler(c));
|
|
64
97
|
}
|
|
98
|
+
this._updateQueries();
|
|
65
99
|
return c;
|
|
66
100
|
}
|
|
67
101
|
/**
|
|
68
|
-
*
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
102
|
+
* @internal Return `true` when this entity is currently tracked by `q`.
|
|
103
|
+
*/
|
|
104
|
+
_isInQuery(q) {
|
|
105
|
+
return this._queries.has(q);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* @internal Look up a component instance by numeric type id.
|
|
73
109
|
*
|
|
74
|
-
* @
|
|
75
|
-
* @returns This entity, for chaining.
|
|
110
|
+
* Faster than {@link get} because no class → type id resolution is needed.
|
|
76
111
|
*/
|
|
77
|
-
|
|
78
|
-
this.
|
|
79
|
-
return this;
|
|
112
|
+
_get(type) {
|
|
113
|
+
return this._components.get(type);
|
|
80
114
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
115
|
+
/**
|
|
116
|
+
* @internal Reparent this entity in place, maintaining the bidirectional
|
|
117
|
+
* link. Throws if `newParent` is a descendant of this entity.
|
|
118
|
+
*
|
|
119
|
+
* Called by the world either inline (outside deferred mode) or while
|
|
120
|
+
* routing a queued `SetParent` command.
|
|
121
|
+
*/
|
|
122
|
+
_setParent(newParent) {
|
|
123
|
+
if (this._destroyed) {
|
|
124
|
+
return;
|
|
86
125
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if (this
|
|
91
|
-
this.
|
|
126
|
+
if (newParent !== undefined) {
|
|
127
|
+
let ancestor = newParent;
|
|
128
|
+
while (ancestor !== undefined) {
|
|
129
|
+
if (ancestor === this) {
|
|
130
|
+
throw new Error(`Circular parent reference: entity ${this.eid} is already an ancestor of entity ${newParent.eid}`);
|
|
92
131
|
}
|
|
132
|
+
ancestor = ancestor._parent;
|
|
93
133
|
}
|
|
94
134
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
this.
|
|
99
|
-
if (
|
|
100
|
-
|
|
135
|
+
if (this._parent) {
|
|
136
|
+
this._parent._children?.delete(this);
|
|
137
|
+
}
|
|
138
|
+
this._parent = newParent;
|
|
139
|
+
if (newParent) {
|
|
140
|
+
(newParent._children ?? (newParent._children = new Set())).add(this);
|
|
101
141
|
}
|
|
102
|
-
return c;
|
|
103
142
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
143
|
+
/**
|
|
144
|
+
* @internal Apply a `Set` command: create the component if missing, assign
|
|
145
|
+
* `props` if provided, fire `onSet`, and route modified events to every
|
|
146
|
+
* query that watches the component type.
|
|
147
|
+
*/
|
|
148
|
+
_set(type, props) {
|
|
149
|
+
if (this._destroyed) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const existing = this._components.get(type);
|
|
153
|
+
const c = existing ?? this._new(type, props);
|
|
154
|
+
if (props !== undefined) {
|
|
155
|
+
if (existing) {
|
|
156
|
+
Object.assign(c, props);
|
|
157
|
+
}
|
|
158
|
+
const setHandlers = c.meta._onSetHandlers;
|
|
159
|
+
if (setHandlers) {
|
|
160
|
+
setHandlers.forEach((handler) => handler(c));
|
|
161
|
+
}
|
|
162
|
+
c._dirty = false;
|
|
163
|
+
if (existing) {
|
|
164
|
+
this._queries.forEach((q) => q._notifyModified(c));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
107
167
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
168
|
+
/**
|
|
169
|
+
* @internal Apply a `Modified` command: fire `onSet` and route modified
|
|
170
|
+
* events to every query that watches the component type.
|
|
171
|
+
*/
|
|
172
|
+
_modified(type) {
|
|
173
|
+
if (this._destroyed) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const c = this._components.get(type);
|
|
177
|
+
if (!c) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const setHandlers = c.meta._onSetHandlers;
|
|
181
|
+
if (setHandlers) {
|
|
182
|
+
setHandlers.forEach((handler) => handler(c));
|
|
183
|
+
}
|
|
184
|
+
c._dirty = false;
|
|
185
|
+
this._queries.forEach((q) => q._notifyModified(c));
|
|
113
186
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
this.
|
|
127
|
-
|
|
128
|
-
|
|
187
|
+
/**
|
|
188
|
+
* @internal Apply a `Remove` command: clear the type bit, route exits,
|
|
189
|
+
* detach the component, and fire `onRemove`.
|
|
190
|
+
*/
|
|
191
|
+
_remove(type) {
|
|
192
|
+
if (this._destroyed) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const c = this._components.get(type);
|
|
196
|
+
if (!c) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
this.componentBitmask.delete(type);
|
|
200
|
+
this._updateQueries();
|
|
201
|
+
this._components.delete(type);
|
|
202
|
+
const removeHandlers = c.meta._onRemoveHandlers;
|
|
203
|
+
if (removeHandlers) {
|
|
204
|
+
removeHandlers.forEach((handler) => handler(c));
|
|
205
|
+
}
|
|
129
206
|
}
|
|
130
207
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
* @param get_deleted - If `true`, also search components that were removed
|
|
135
|
-
* in the current frame but not yet garbage-collected. Useful inside
|
|
136
|
-
* `exit` callbacks to read final component values.
|
|
137
|
-
* @returns The component instance or `undefined`.
|
|
208
|
+
* @internal Apply a `Destroy` command: fire `_exit` on every query, then
|
|
209
|
+
* `onRemove` on every component, emit the `destroy` event, and unlink from
|
|
210
|
+
* the world and any parent.
|
|
138
211
|
*/
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
212
|
+
_destroy() {
|
|
213
|
+
if (this._destroyed) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
this._destroyed = true;
|
|
217
|
+
const toExit = [];
|
|
218
|
+
this._queries.forEach((q) => {
|
|
219
|
+
if (q.world) {
|
|
220
|
+
toExit.push(q);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
toExit.forEach((q) => q._exit(this));
|
|
224
|
+
this._components.forEach((c) => {
|
|
225
|
+
const removeHandlers = c.meta._onRemoveHandlers;
|
|
226
|
+
if (removeHandlers) {
|
|
227
|
+
removeHandlers.forEach((handler) => handler(c));
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
if (this._events) {
|
|
231
|
+
this._events.emit("destroy");
|
|
232
|
+
this._events.removeAllListeners("destroy");
|
|
233
|
+
}
|
|
234
|
+
this.world._unregisterEntity(this);
|
|
235
|
+
if (this._parent) {
|
|
236
|
+
this._parent._children?.delete(this);
|
|
237
|
+
this._parent = undefined;
|
|
144
238
|
}
|
|
145
|
-
return c;
|
|
146
239
|
}
|
|
147
240
|
/**
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
|
|
151
|
-
|
|
241
|
+
* @internal Forget query `q` without firing exit callbacks. Called by
|
|
242
|
+
* {@link World} when a {@link Query.destroy} sweeps every entity.
|
|
243
|
+
*/
|
|
244
|
+
_purgeQuery(q) {
|
|
245
|
+
this._queries.delete(q);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* @internal Record that this entity is now tracked by `q`. Called by
|
|
249
|
+
* `Query._enter`.
|
|
250
|
+
*/
|
|
251
|
+
_addQueryMembership(q) {
|
|
252
|
+
this._queries.add(q);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* @internal Record that this entity is no longer tracked by `q`. Called by
|
|
256
|
+
* `Query._exit`.
|
|
257
|
+
*/
|
|
258
|
+
_removeQueryMembership(q) {
|
|
259
|
+
this._queries.delete(q);
|
|
260
|
+
}
|
|
261
|
+
/** Parent entity in the scene hierarchy, or `undefined` for a root entity. */
|
|
262
|
+
get parent() {
|
|
263
|
+
return this._parent;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Read-only view of direct child entities. The backing set is created lazily
|
|
267
|
+
* on the first child link; before that this getter returns a shared empty set.
|
|
268
|
+
*/
|
|
269
|
+
get children() {
|
|
270
|
+
return this._children ?? Entity._emptyChildren;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Typed event emitter for entity-level lifecycle events. Currently only the
|
|
274
|
+
* `"destroy"` event is emitted, just before the entity is fully torn down.
|
|
152
275
|
*
|
|
153
276
|
* The emitter is created lazily on first access.
|
|
154
277
|
*/
|
|
@@ -158,92 +281,128 @@ export class Entity {
|
|
|
158
281
|
}
|
|
159
282
|
return this._events;
|
|
160
283
|
}
|
|
161
|
-
/**
|
|
162
|
-
|
|
163
|
-
return this.
|
|
284
|
+
/** `true` when no components are currently attached to this entity. */
|
|
285
|
+
get empty() {
|
|
286
|
+
return this._components.size == 0;
|
|
164
287
|
}
|
|
165
|
-
/**
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
288
|
+
/**
|
|
289
|
+
* Read-only view of all components currently attached to this entity, keyed
|
|
290
|
+
* by numeric component type id.
|
|
291
|
+
*
|
|
292
|
+
* The mutating methods (`set`, `delete`, `clear`) are not exposed. Use
|
|
293
|
+
* `entity.add`, `entity.set`, and `entity.remove` to change the component
|
|
294
|
+
* set.
|
|
295
|
+
*
|
|
296
|
+
* ```ts
|
|
297
|
+
* entity.components.forEach((c) => console.log(c.constructor.name));
|
|
298
|
+
* ```
|
|
299
|
+
*/
|
|
300
|
+
get components() {
|
|
301
|
+
return this._components;
|
|
172
302
|
}
|
|
173
|
-
/**
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
303
|
+
/**
|
|
304
|
+
* Reparent this entity. In deferred mode the change is queued; outside
|
|
305
|
+
* deferred mode it executes inline.
|
|
306
|
+
*
|
|
307
|
+
* @param newParent - New parent, or `undefined` to make this a root entity.
|
|
308
|
+
*/
|
|
309
|
+
setParent(newParent) {
|
|
310
|
+
if (this.world.deferred) {
|
|
311
|
+
this.world._enqueue({ kind: 5 /* CommandKind.SetParent */, entity: this, parent: newParent });
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
this._setParent(newParent);
|
|
178
315
|
}
|
|
179
316
|
}
|
|
180
|
-
/**
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
317
|
+
/**
|
|
318
|
+
* Mark `c` as having changed, queueing the corresponding `onSet` / `update`
|
|
319
|
+
* notifications.
|
|
320
|
+
*
|
|
321
|
+
* Equivalent to `c.modified()` but returns the entity for chaining. Repeated
|
|
322
|
+
* calls before the world routes the modified command are coalesced via the
|
|
323
|
+
* component's dirty flag.
|
|
324
|
+
*
|
|
325
|
+
* @param c - Component instance whose data changed.
|
|
326
|
+
* @returns This entity, for chaining.
|
|
327
|
+
*/
|
|
328
|
+
modified(c) {
|
|
329
|
+
if (c._dirty) {
|
|
330
|
+
return this;
|
|
331
|
+
}
|
|
332
|
+
c._dirty = true;
|
|
333
|
+
if (this.world.deferred) {
|
|
334
|
+
this.world._enqueue({ kind: 2 /* CommandKind.Modified */, entity: this, type: c.type });
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
this._modified(c.type);
|
|
184
338
|
}
|
|
339
|
+
return this;
|
|
185
340
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
this.
|
|
189
|
-
this.
|
|
190
|
-
}
|
|
191
|
-
|
|
341
|
+
add(typeOrClass) {
|
|
342
|
+
const type = this.world.getComponentType(typeOrClass);
|
|
343
|
+
if (this.world.deferred) {
|
|
344
|
+
this.world._enqueue({ kind: 1 /* CommandKind.Set */, entity: this, type, props: undefined });
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
this._set(type, undefined);
|
|
348
|
+
}
|
|
349
|
+
return this;
|
|
192
350
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
351
|
+
set(typeOrClass, props) {
|
|
352
|
+
const type = this.world.getComponentType(typeOrClass);
|
|
353
|
+
if (this.world.deferred) {
|
|
354
|
+
this.world._enqueue({ kind: 1 /* CommandKind.Set */, entity: this, type, props });
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
this._set(type, props);
|
|
358
|
+
}
|
|
359
|
+
return this;
|
|
196
360
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
if (this.
|
|
200
|
-
|
|
361
|
+
remove(typeOrClass) {
|
|
362
|
+
const type = this.world.getComponentType(typeOrClass);
|
|
363
|
+
if (this.world.deferred) {
|
|
364
|
+
this.world._enqueue({ kind: 3 /* CommandKind.Remove */, entity: this, type });
|
|
201
365
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
q._exit(this);
|
|
205
|
-
});
|
|
206
|
-
this.queries.clear();
|
|
207
|
-
if (this._events) {
|
|
208
|
-
this._events.emit("destroy");
|
|
209
|
-
this._events.removeAllListeners("destroy");
|
|
366
|
+
else {
|
|
367
|
+
this._remove(type);
|
|
210
368
|
}
|
|
211
369
|
}
|
|
212
370
|
/**
|
|
213
|
-
*
|
|
371
|
+
* Look up a component on this entity.
|
|
214
372
|
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
* event is emitted. The entity must not be used after calling this method.
|
|
373
|
+
* @param typeOrClass - Component class or numeric type id.
|
|
374
|
+
* @returns The component instance, or `undefined` when it is not attached.
|
|
218
375
|
*/
|
|
219
|
-
|
|
220
|
-
this.world.
|
|
221
|
-
this.
|
|
222
|
-
child.destroy();
|
|
223
|
-
});
|
|
224
|
-
this.children.clear();
|
|
225
|
-
if (this.parent) {
|
|
226
|
-
this.parent.children.delete(this);
|
|
227
|
-
this.world.archetypeChanged(this.parent);
|
|
228
|
-
this.parent = undefined;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
/** @internal */
|
|
232
|
-
clearDeletedComponents() {
|
|
233
|
-
this.deletedComponents.clear();
|
|
376
|
+
get(typeOrClass) {
|
|
377
|
+
const type = this.world.getComponentType(typeOrClass);
|
|
378
|
+
return this._get(type);
|
|
234
379
|
}
|
|
235
380
|
/**
|
|
236
|
-
*
|
|
381
|
+
* Destroy this entity and recursively destroy its children.
|
|
237
382
|
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
383
|
+
* Each component fires its `onRemove` hook, the `"destroy"` event is emitted
|
|
384
|
+
* just before teardown, and the entity is unregistered from the world.
|
|
385
|
+
* After destruction the entity must not be used.
|
|
240
386
|
*/
|
|
241
|
-
|
|
242
|
-
this.
|
|
387
|
+
destroy() {
|
|
388
|
+
if (this.world.deferred) {
|
|
389
|
+
this.world._enqueue({ kind: 4 /* CommandKind.Destroy */, entity: this });
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
this._destroy();
|
|
393
|
+
}
|
|
394
|
+
if (this._children) {
|
|
395
|
+
this._children.forEach((child) => {
|
|
396
|
+
child.destroy();
|
|
397
|
+
});
|
|
398
|
+
this._children.clear();
|
|
399
|
+
}
|
|
243
400
|
}
|
|
244
401
|
/** Returns `"EntityN"` where N is the entity id. */
|
|
245
402
|
toString() {
|
|
246
403
|
return `Entity${this.eid}`;
|
|
247
404
|
}
|
|
248
405
|
}
|
|
406
|
+
/** @internal Empty children set used as the default `children` value. */
|
|
407
|
+
Entity._emptyChildren = new Set();
|
|
249
408
|
//# sourceMappingURL=entity.js.map
|
package/dist/entity.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entity.js","sourceRoot":"","sources":["../src/entity.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"entity.js","sourceRoot":"","sources":["../src/entity.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAoB,MAAM,qBAAqB,CAAC;AAEjE,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAI1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,OAAO,MAAM;IAgCjB;IACE,mCAAmC;IACnB,KAAY;IAC5B,qDAAqD;IACrC,GAAW;QAFX,UAAK,GAAL,KAAK,CAAO;QAEZ,QAAG,GAAH,GAAG,CAAQ;QAnC7B,sEAAsE;QAC9D,gBAAW,GAAG,IAAI,QAAQ,EAAa,CAAC;QAChD,iEAAiE;QAChD,aAAQ,GAAG,IAAI,GAAG,EAAS,CAAC;QAU7C;;;WAGG;QACa,qBAAgB,GAAG,IAAI,MAAM,EAAE,CAAC;QAEhD;;;WAGG;QACI,eAAU,GAAG,IAAI,GAAG,EAAe,CAAC;QAE3C,+EAA+E;QACxE,eAAU,GAAY,KAAK,CA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