@vworlds/vecs 1.0.9 → 1.0.10

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 CHANGED
@@ -1 +1,2 @@
1
+ npx tsc --noEmit
1
2
  npx lint-staged
@@ -0,0 +1,46 @@
1
+ import type { Component } from "./component.js";
2
+ import type { Entity } from "./entity.js";
3
+ export declare const enum CommandKind {
4
+ CreateEntity = 0,
5
+ Set = 1,
6
+ Modified = 2,
7
+ Remove = 3,
8
+ Destroy = 4,
9
+ SetParent = 5
10
+ }
11
+ /**
12
+ * Command kinds emitted by {@link Entity} and routed by {@link World}.
13
+ *
14
+ * Commands are produced by `entity.add` / `entity.set` / `entity.remove` /
15
+ * `entity.destroy` (and `Component.modified`). In deferred mode they are
16
+ * pushed onto the world's command queue and processed at well-defined
17
+ * boundaries (after each system run, on `flush()`, on the next `runPhase`,
18
+ * etc.). Outside deferred mode they execute inline.
19
+ *
20
+ * @internal
21
+ */
22
+ export type Command = {
23
+ kind: CommandKind.CreateEntity;
24
+ entity: Entity;
25
+ } | {
26
+ kind: CommandKind.Set;
27
+ entity: Entity;
28
+ type: number;
29
+ /** Properties to assign. `undefined` for `entity.add(C)` (ensure-exists, no data). */
30
+ props: Partial<Component> | undefined;
31
+ } | {
32
+ kind: CommandKind.Modified;
33
+ entity: Entity;
34
+ type: number;
35
+ } | {
36
+ kind: CommandKind.Remove;
37
+ entity: Entity;
38
+ type: number;
39
+ } | {
40
+ kind: CommandKind.Destroy;
41
+ entity: Entity;
42
+ } | {
43
+ kind: CommandKind.SetParent;
44
+ entity: Entity;
45
+ parent: Entity | undefined;
46
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=command.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command.js","sourceRoot":"","sources":["../src/command.ts"],"names":[],"mappings":""}
package/dist/entity.d.ts CHANGED
@@ -21,6 +21,20 @@ type EntityEvents = Events<{
21
21
  *
22
22
  * Entities support a parent–child hierarchy. When a parent is destroyed its
23
23
  * children are destroyed recursively. The `children` set is created lazily.
24
+ *
25
+ * ## Deferred semantics
26
+ *
27
+ * Inside a system body or `forEach` iteration the world is in **deferred
28
+ * mode**: `add` / `set` / `remove` / `destroy` only enqueue commands; the
29
+ * data layer (`components` map, `componentBitmask`) is not mutated until the
30
+ * world processes the queue. Concretely, inside deferred mode:
31
+ *
32
+ * - `entity.get(C)` returns `undefined` after `entity.add(C)` (no instance yet).
33
+ * - `entity.get(C)` returns the previous value after `entity.set(C, props)`.
34
+ * - `entity.get(C)` still returns the component after `entity.remove(C)`.
35
+ *
36
+ * Outside deferred mode (top-level user code) the same calls execute inline —
37
+ * mutations are visible immediately.
24
38
  */
25
39
  export declare class Entity {
26
40
  /** The {@link World} that owns this entity. */
@@ -28,7 +42,6 @@ export declare class Entity {
28
42
  /** Unique numeric entity id assigned at creation time. */
29
43
  readonly eid: number;
30
44
  private components;
31
- private deletedComponents;
32
45
  /**
33
46
  * Bitmask representing the set of component types currently attached to this
34
47
  * entity. Used by the world to efficiently match entities against query
@@ -36,33 +49,37 @@ export declare class Entity {
36
49
  */
37
50
  readonly componentBitmask: Bitset;
38
51
  private readonly queries;
39
- private readonly newQueries;
40
52
  /**
41
53
  * A free-form property bag that modules can use to associate arbitrary data
42
54
  * with an entity without registering a component.
43
55
  */
44
56
  properties: Map<string, any>;
45
57
  _events: EntityEvents;
46
- /** Parent entity in the scene hierarchy, or `undefined` if root. */
47
- parent: Entity | undefined;
48
- /** @internal */
49
- _children: Set<Entity> | undefined;
50
- _archetypeChanged: boolean;
51
- private destroyed;
58
+ private _parent;
59
+ private _children;
60
+ /** @internal True once the world has fully processed this entity's destruction. */
61
+ _destroyed: boolean;
62
+ private static readonly _emptyChildren;
52
63
  constructor(
53
64
  /** The {@link World} that owns this entity. */
54
65
  world: World,
55
66
  /** Unique numeric entity id assigned at creation time. */
56
67
  eid: number);
68
+ /** Parent entity in the scene hierarchy, or `undefined` if this is a root entity. */
69
+ get parent(): Entity | undefined;
70
+ /** Read-only view of direct child entities. */
71
+ get children(): ReadonlySet<Entity>;
57
72
  /**
58
- * The set of direct child entities in the scene hierarchy.
59
- *
60
- * The set is created lazily on first access. Mutate it only through
61
- * {@link Entity.destroy} or by setting {@link Entity.parent} on a child —
62
- * both will keep the parent–child links consistent.
73
+ * Immediately reparent this entity. Maintains the bidirectional link,
74
+ * removes it from the old parent's children, and adds it to the new
75
+ * parent's children. Throws if the operation would create a cycle.
63
76
  */
64
- get children(): Set<Entity>;
65
- private getComponentInstance;
77
+ _setParent(newParent: Entity | undefined): void;
78
+ /**
79
+ * Reparent this entity. In deferred mode the change is queued; outside
80
+ * deferred mode it executes immediately.
81
+ */
82
+ setParent(newParent: Entity | undefined): void;
66
83
  /**
67
84
  * Queue an `onSet` / `update` notification for the given component and
68
85
  * return the entity for chaining.
@@ -75,54 +92,30 @@ export declare class Entity {
75
92
  */
76
93
  modified<C extends typeof Component>(c: InstanceType<C>): Entity;
77
94
  /**
78
- * Add a component of type `Class` to this entity and return the instance.
95
+ * Add a component of type `Class` to this entity if it isn't already
96
+ * present, and return the entity for chaining.
79
97
  *
80
- * If the component is already present the existing instance is returned and
81
- * no callback is fired. Pass `markAsModified = false` to suppress the
82
- * initial `onSet` / `update` notification (useful when bulk-loading
83
- * network snapshots before systems are running).
98
+ * Does nothing if the component is already attached. `add` does not fire
99
+ * `onSet` use {@link set} when you want to apply data and notify.
84
100
  *
85
101
  * @param Class - The component class to instantiate.
86
- * @param markAsModified - Whether to immediately queue an `update`
87
- * notification. Defaults to `true`.
88
- * @returns The new (or existing) component instance, typed as
89
- * `InstanceType<Class>`.
90
- */
91
- _add<C extends typeof Component>(Class: C, markAsModified?: boolean): InstanceType<C>;
92
- /**
93
- * Add a component by its numeric type id.
94
- *
95
- * @param type - Numeric component type id (as returned by
96
- * {@link World.getComponentType}).
97
- * @param markAsModified - Whether to queue an update notification.
98
- */
99
- _add(type: number, markAsModified?: boolean): Component;
100
- /**
101
- * Add a component of type `Class` to this entity and return the entity.
102
- *
103
- * If the component is already present the existing instance is returned and
104
- * no callback is fired. Pass `markAsModified = false` to suppress the
105
- * initial `onSet` / `update` notification (useful when bulk-loading
106
- * network snapshots before systems are running).
107
- *
108
- * @param Class - The component class to instantiate.
109
- * @param markAsModified - Whether to immediately queue an `update`
110
- * notification. Defaults to `true`.
111
102
  * @returns This entity, for chaining.
112
103
  */
113
- add<C extends typeof Component>(Class: C, markAsModified?: boolean): Entity;
104
+ add<C extends typeof Component>(Class: C): Entity;
114
105
  /**
115
106
  * Add a component by its numeric type id.
116
107
  *
117
108
  * @param type - Numeric component type id (as returned by
118
109
  * {@link World.getComponentType}).
119
- * @param markAsModified - Whether to queue an update notification.
120
110
  */
121
- add(type: number, markAsModified?: boolean): Entity;
111
+ add(type: number): Entity;
122
112
  /**
123
113
  * Add a component of type `Class` (if not already present), assign the
124
114
  * provided properties onto the instance, and return the entity for chaining.
125
115
  *
116
+ * In deferred mode the props are not applied until the world processes the
117
+ * resulting `Set` command.
118
+ *
126
119
  * @param Class - The component class to instantiate.
127
120
  * @param props - Properties to assign onto the component instance.
128
121
  * @returns This entity, for chaining.
@@ -139,9 +132,9 @@ export declare class Entity {
139
132
  /**
140
133
  * Remove the component of the given class from this entity.
141
134
  *
142
- * The `onRemove` hook and any `exit` callbacks on matching systems are
143
- * called when archetype changes are flushed at the end of the next system
144
- * run. Does nothing if the component is not present.
135
+ * In deferred mode the removal is queued — `get(Class)` continues to return
136
+ * the component until the queue is processed. Once processed, queries fire
137
+ * exit callbacks first, then the `onRemove` hook fires.
145
138
  *
146
139
  * @param Class - The component class to remove.
147
140
  */
@@ -152,18 +145,15 @@ export declare class Entity {
152
145
  * @param type - Numeric component type id.
153
146
  */
154
147
  remove(type: number): void;
155
- /** @internal Called by queries to deliver update notifications. */
156
- _notifyModified(component: Component): void;
148
+ /** @internal Look up a component instance by type id. */
149
+ _get(type: number): Component | undefined;
157
150
  /**
158
151
  * Retrieve the component of type `Class`, or `undefined` if not present.
159
152
  *
160
153
  * @param typeOrClass - Component class or numeric type id.
161
- * @param get_deleted - If `true`, also search components that were removed
162
- * in the current frame but not yet garbage-collected. Useful inside
163
- * `exit` callbacks to read final component values.
164
154
  * @returns The component instance or `undefined`.
165
155
  */
166
- get<C extends typeof Component>(typeOrClass: number | C, get_deleted?: boolean): InstanceType<C> | undefined;
156
+ get<C extends typeof Component>(typeOrClass: number | C): InstanceType<C> | undefined;
167
157
  /**
168
158
  * Typed event emitter for entity-level lifecycle events.
169
159
  *
@@ -173,30 +163,33 @@ export declare class Entity {
173
163
  * The emitter is created lazily on first access.
174
164
  */
175
165
  get events(): EntityEvents;
176
- /** @internal */
177
- _hasQuery(q: Query): boolean;
166
+ isInQuery(q: Query): boolean;
178
167
  /** @internal Removes a query from this entity's tracking sets without firing any callbacks. */
179
168
  _purgeQuery(q: Query): void;
180
- /** @internal */
181
- _addQuery(q: Query): void;
182
- /** @internal */
183
- _removeQuery(q: Query): void;
184
- /** @internal */
185
- _updateQueries(): void;
169
+ /** @internal Add a query to the entity's tracked-query set. Called by Query._enter. */
170
+ _addQueryMembership(q: Query): void;
171
+ /** @internal Remove a query from the entity's tracked-query set. Called by Query._exit. */
172
+ _removeQueryMembership(q: Query): void;
173
+ private _updateQueries;
174
+ private _new;
175
+ /** @internal Execute a Set command — create if absent, apply props, fire hooks, route events. */
176
+ _set(type: number, props: Partial<Component> | undefined): void;
177
+ /** @internal Execute a Modified command — fire onSet and route update events. */
178
+ _modified(type: number): void;
179
+ /** @internal Execute a Remove command — clear bitmask, route exits, remove component, fire onRemove. */
180
+ _remove(type: number): void;
181
+ /** @internal Execute a Destroy command — exit queries, fire onRemove, emit event, unregister. */
182
+ _destroy(): void;
186
183
  /** `true` when the entity has no components attached. */
187
184
  get empty(): boolean;
188
- /** @internal */
189
- _destroy(): void;
190
185
  /**
191
186
  * Destroy this entity and recursively destroy all of its children.
192
187
  *
193
- * All components are removed (triggering `onRemove` hooks and `exit`
194
- * callbacks), the entity is unregistered from the world, and the `"destroy"`
195
- * event is emitted. The entity must not be used after calling this method.
188
+ * All components have their `onRemove` hooks fired, the entity is
189
+ * unregistered from the world, and the `"destroy"` event is emitted. The
190
+ * entity must not be used after the destroy completes.
196
191
  */
197
192
  destroy(): void;
198
- /** @internal */
199
- clearDeletedComponents(): void;
200
193
  /**
201
194
  * Iterate over every component currently attached to this entity.
202
195
  *