@vworlds/vecs 1.0.12 → 1.0.14

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/dist/dsl.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Component, _calculateComponentBitmask, } from "./component.js";
1
+ import { _calculateComponentBitmask, } from "./component.js";
2
2
  /**
3
3
  * Build a predicate that returns `true` when an entity has every component
4
4
  * type in `components` set on its archetype.
@@ -43,11 +43,14 @@ function _PARENT(func) {
43
43
  * @internal Used by `Query`, `System`, and `Filter` to translate user-supplied
44
44
  * DSL expressions into the predicate stored on `Query._belongs`.
45
45
  *
46
- * @param world - World used to resolve component classes to type ids.
46
+ * @param world - World used to resolve registered component classes to type ids.
47
47
  * @param q - Query expression.
48
48
  */
49
49
  export function _buildEntityTest(world, q) {
50
- if (typeof q === "number" || (typeof q === "function" && q.prototype instanceof Component)) {
50
+ if (typeof q === "number") {
51
+ return _HAS(world, q);
52
+ }
53
+ if (typeof q === "function" && world._tryGetComponentMeta(q)) {
51
54
  return _HAS(world, q);
52
55
  }
53
56
  else if (typeof q === "function") {
package/dist/dsl.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"dsl.js","sourceRoot":"","sources":["../src/dsl.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EAGT,0BAA0B,GAC3B,MAAM,gBAAgB,CAAC;AA+FxB;;;;;;;;;GASG;AACH,MAAM,UAAU,IAAI,CAAC,KAAY,EAAE,GAAG,UAA+B;IACnE,MAAM,WAAW,GAAG,0BAA0B,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IAClE,OAAO,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,gBAAgB,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;AAClE,CAAC;AAED;;;GAGG;AACH,SAAS,SAAS,CAAC,KAAY,EAAE,GAAG,UAA+B;IACjE,MAAM,WAAW,GAAG,0BAA0B,CAAC,UAAU,EAAE,KA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;QACd,OAAO,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;KAC7C;IAED,IAAI,QAAQ,IAAI,CAAC,EAAE;QACjB,OAAO,OAAO,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;KACnD;IAED,MAAM,yBAAyB,CAAC;AAClC,CAAC"}
package/dist/entity.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Component } from "./component.js";
1
+ import { type Component, type ComponentClass, type ComponentClassOrType } from "./component.js";
2
2
  import type { World } from "./world.js";
3
3
  import { ReadonlyArrayMap } from "./util/array_map.js";
4
4
  import { Events } from "./util/events.js";
@@ -19,18 +19,20 @@ type EntityEvents = Events<{
19
19
  * e.set(Position, { x: 100 });
20
20
  * ```
21
21
  *
22
- * Entities support a parentchild hierarchy. `parent` and `children` form a
22
+ * Entities support a parent-child hierarchy. `parent` and `children` form a
23
23
  * bidirectional link maintained by {@link setParent}; the children set is
24
24
  * created lazily. Destroying a parent recursively destroys its children.
25
25
  *
26
26
  * ## Deferred semantics
27
27
  *
28
28
  * Inside a system body or `forEach` iteration the world is in **deferred
29
- * mode**: `add` / `set` / `modified` / `remove` / `destroy` / `setParent` only
30
- * enqueue commands. The data layer (the components map and `componentBitmask`)
31
- * is mutated when the world drains its queue. Concretely, while deferred:
29
+ * mode**: `add` / `attach` / `set` / `modified` / `remove` / `destroy` /
30
+ * `setParent` only enqueue commands. The data layer (the components map and
31
+ * `componentBitmask`) is mutated when the world drains its queue. Concretely,
32
+ * while deferred:
32
33
  *
33
34
  * - `entity.get(C)` returns `undefined` after `entity.add(C)` (no instance yet).
35
+ * - `entity.get(C)` returns `undefined` after `entity.attach(instance)` if C was absent.
34
36
  * - `entity.get(C)` returns the previous value after `entity.set(C, props)`.
35
37
  * - `entity.get(C)` still returns the component after `entity.remove(C)`.
36
38
  *
@@ -57,11 +59,19 @@ export declare class Entity {
57
59
  world: World,
58
60
  /** Unique numeric entity id assigned at creation. */
59
61
  eid: number);
62
+ /** Fire every `onAdd` hook registered for `meta`. */
63
+ private _runOnAddHandlers;
64
+ /** Fire every `onSet` hook registered for `meta`. */
65
+ private _runOnSetHandlers;
60
66
  /**
61
67
  * Re-evaluate every world query for this entity, firing `_enter` / `_exit`
62
68
  * routing whenever membership flipped.
63
69
  */
64
70
  private _updateQueries;
71
+ /** Store a component instance and perform the shared add-side bookkeeping. */
72
+ private _storeComponent;
73
+ /** Perform the shared set-side hook and query update routing. */
74
+ private _notifyComponentSet;
65
75
  /**
66
76
  * Construct a fresh component of `type`, apply `props`, store it on this
67
77
  * entity, fire the `onAdd` hook, and route query updates.
@@ -70,64 +80,45 @@ export declare class Entity {
70
80
  * component already on this entity is removed first.
71
81
  */
72
82
  private _new;
73
- /** Parent entity in the scene hierarchy, or `undefined` for a root entity. */
74
- get parent(): Entity | undefined;
75
83
  /**
76
84
  * Read-only view of direct child entities. The backing set is created lazily
77
85
  * on the first child link; before that this getter returns a shared empty set.
78
86
  */
79
87
  get children(): ReadonlySet<Entity>;
80
- /**
81
- * Typed event emitter for entity-level lifecycle events. Currently only the
82
- * `"destroy"` event is emitted, just before the entity is fully torn down.
83
- *
84
- * The emitter is created lazily on first access.
85
- */
86
- get events(): EntityEvents;
87
- /** `true` when no components are currently attached to this entity. */
88
- get empty(): boolean;
89
88
  /**
90
89
  * Read-only view of all components currently attached to this entity, keyed
91
90
  * by numeric component type id.
92
91
  *
93
92
  * The mutating methods (`set`, `delete`, `clear`) are not exposed. Use
94
- * `entity.add`, `entity.set`, and `entity.remove` to change the component
95
- * set.
93
+ * `entity.add`, `entity.attach`, `entity.set`, and `entity.remove` to change
94
+ * the component set.
96
95
  *
97
96
  * ```ts
98
97
  * entity.components.forEach((c) => console.log(c.constructor.name));
99
98
  * ```
100
99
  */
101
100
  get components(): ReadonlyArrayMap<Component>;
101
+ /** `true` when no components are currently attached to this entity. */
102
+ get empty(): boolean;
102
103
  /**
103
- * Reparent this entity. In deferred mode the change is queued; outside
104
- * deferred mode it executes inline.
105
- *
106
- * @param newParent - New parent, or `undefined` to make this a root entity.
107
- */
108
- setParent(newParent: Entity | undefined): void;
109
- /**
110
- * Mark `c` as having changed, queueing the corresponding `onSet` / `update`
111
- * notifications.
112
- *
113
- * Equivalent to `c.modified()` but returns the entity for chaining. Repeated
114
- * calls before the world routes the modified command are coalesced via the
115
- * component's dirty flag.
104
+ * Typed event emitter for entity-level lifecycle events. Currently only the
105
+ * `"destroy"` event is emitted, just before the entity is fully torn down.
116
106
  *
117
- * @param c - Component instance whose data changed.
118
- * @returns This entity, for chaining.
107
+ * The emitter is created lazily on first access.
119
108
  */
120
- modified<C extends typeof Component>(c: InstanceType<C>): Entity;
109
+ get events(): EntityEvents;
110
+ /** Parent entity in the scene hierarchy, or `undefined` for a root entity. */
111
+ get parent(): Entity | undefined;
121
112
  /**
122
113
  * Attach a component to this entity if it is not already present.
123
114
  *
124
- * Idempotent. Does not fire `onSet` use {@link set} when you want to apply
115
+ * Idempotent. Does not fire `onSet` -- use {@link set} when you want to apply
125
116
  * data and notify watchers.
126
117
  *
127
118
  * @param Class - Component class to instantiate.
128
119
  * @returns This entity, for chaining.
129
120
  */
130
- add<C extends typeof Component>(Class: C): Entity;
121
+ add<C extends ComponentClass>(Class: C): Entity;
131
122
  /**
132
123
  * Attach a component by numeric type id.
133
124
  *
@@ -136,26 +127,44 @@ export declare class Entity {
136
127
  */
137
128
  add(type: number): Entity;
138
129
  /**
139
- * Attach a component (creating it if necessary), copy `props` onto the
140
- * instance, and fire the `onSet` hook plus any `update` callbacks for the
141
- * component type.
130
+ * Attach an existing component instance to this entity and store that exact
131
+ * object. If a component of the same registered class already exists, it is
132
+ * replaced rather than assigned into.
142
133
  *
143
- * In deferred mode `props` are not applied until the queued `Set` command is
144
- * routed.
134
+ * `attach` uses the instance constructor to resolve component metadata, so
135
+ * the constructor must already be registered in this world. The operation
136
+ * fires hooks and query updates like a `set` operation.
145
137
  *
146
- * @param Class - Component class to instantiate.
147
- * @param props - Properties to assign onto the component instance.
138
+ * @param component - Existing component instance to store on the entity.
148
139
  * @returns This entity, for chaining.
149
140
  */
150
- set<C extends typeof Component>(Class: C, props: Partial<InstanceType<C>>): Entity;
141
+ attach(component: Component): Entity;
151
142
  /**
152
- * Attach a component by numeric type id and copy `props` onto it.
143
+ * Destroy this entity and recursively destroy its children.
153
144
  *
154
- * @param type - Numeric component type id.
155
- * @param props - Properties to assign onto the component instance.
145
+ * Each component fires its `onRemove` hook, the `"destroy"` event is emitted
146
+ * just before teardown, and the entity is unregistered from the world.
147
+ * After destruction the entity must not be used.
148
+ */
149
+ destroy(): void;
150
+ /**
151
+ * Look up a component on this entity.
152
+ *
153
+ * @param typeOrClass - Component class or numeric type id.
154
+ * @returns The component instance, or `undefined` when it is not attached.
155
+ */
156
+ get<C extends ComponentClass>(typeOrClass: number | C): InstanceType<C> | undefined;
157
+ /**
158
+ * Mark a component type as having changed, queueing the corresponding `onSet` / `update`
159
+ * notifications.
160
+ *
161
+ * Repeated calls before the world routes the modified command are coalesced via
162
+ * the entity's dirty component bitset.
163
+ *
164
+ * @param typeOrClass - Component class or numeric type id whose data changed.
156
165
  * @returns This entity, for chaining.
157
166
  */
158
- set(type: number, props: Partial<Component>): Entity;
167
+ modified(typeOrClass: ComponentClassOrType): Entity;
159
168
  /**
160
169
  * Detach a component from this entity.
161
170
  *
@@ -165,7 +174,7 @@ export declare class Entity {
165
174
  *
166
175
  * @param Class - Component class to detach.
167
176
  */
168
- remove<C extends typeof Component>(Class: C): void;
177
+ remove<C extends ComponentClass>(Class: C): void;
169
178
  /**
170
179
  * Detach a component by numeric type id.
171
180
  *
@@ -173,20 +182,33 @@ export declare class Entity {
173
182
  */
174
183
  remove(type: number): void;
175
184
  /**
176
- * Look up a component on this entity.
185
+ * Reparent this entity. In deferred mode the change is queued; outside
186
+ * deferred mode it executes inline.
177
187
  *
178
- * @param typeOrClass - Component class or numeric type id.
179
- * @returns The component instance, or `undefined` when it is not attached.
188
+ * @param newParent - New parent, or `undefined` to make this a root entity.
180
189
  */
181
- get<C extends typeof Component>(typeOrClass: number | C): InstanceType<C> | undefined;
190
+ setParent(newParent: Entity | undefined): void;
182
191
  /**
183
- * Destroy this entity and recursively destroy its children.
192
+ * Attach a component (creating it if necessary), copy `props` onto the
193
+ * instance, and fire the `onSet` hook plus any `update` callbacks for the
194
+ * component type.
184
195
  *
185
- * Each component fires its `onRemove` hook, the `"destroy"` event is emitted
186
- * just before teardown, and the entity is unregistered from the world.
187
- * After destruction the entity must not be used.
196
+ * In deferred mode `props` are not applied until the queued `Set` command is
197
+ * routed.
198
+ *
199
+ * @param Class - Component class to instantiate.
200
+ * @param props - Properties to assign onto the component instance.
201
+ * @returns This entity, for chaining.
188
202
  */
189
- destroy(): void;
203
+ set<C extends ComponentClass>(Class: C, props: Partial<InstanceType<C>>): Entity;
204
+ /**
205
+ * Attach a component by numeric type id and copy `props` onto it.
206
+ *
207
+ * @param type - Numeric component type id.
208
+ * @param props - Properties to assign onto the component instance.
209
+ * @returns This entity, for chaining.
210
+ */
211
+ set(type: number, props: Partial<Component>): Entity;
190
212
  /** Returns `"EntityN"` where N is the entity id. */
191
213
  toString(): string;
192
214
  }