@codehz/ecs 0.3.14 → 0.4.0
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/README.md +65 -44
- package/index.d.mts +2 -783
- package/index.mjs +2 -2137
- package/package.json +6 -2
- package/testing.d.mts +305 -0
- package/testing.mjs +429 -0
- package/testing.mjs.map +1 -0
- package/world.d.mts +766 -0
- package/world.mjs +2081 -0
- package/world.mjs.map +1 -0
- package/index.mjs.map +0 -1
package/world.d.mts
ADDED
|
@@ -0,0 +1,766 @@
|
|
|
1
|
+
//#region src/entity.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Unique symbol brand for associating component type information with EntityId
|
|
4
|
+
*/
|
|
5
|
+
declare const __componentTypeMarker: unique symbol;
|
|
6
|
+
/**
|
|
7
|
+
* Unique symbol brand for tagging the kind of EntityId (e.g., 'component', 'entity-relation')
|
|
8
|
+
*/
|
|
9
|
+
declare const __entityIdTypeTag: unique symbol;
|
|
10
|
+
/**
|
|
11
|
+
* Entity ID type for ECS architecture
|
|
12
|
+
* Based on 52-bit integers within safe integer range
|
|
13
|
+
* - Component IDs: 1-1023
|
|
14
|
+
* - Entity IDs: 1024+
|
|
15
|
+
* - Relation IDs: negative numbers encoding component and entity associations
|
|
16
|
+
*/
|
|
17
|
+
type EntityId<T = void, U$1 = unknown> = number & {
|
|
18
|
+
readonly [__componentTypeMarker]: T;
|
|
19
|
+
readonly [__entityIdTypeTag]: U$1;
|
|
20
|
+
};
|
|
21
|
+
type ComponentId<T = void> = EntityId<T, "component">;
|
|
22
|
+
type EntityRelationId<T = void> = EntityId<T, "entity-relation">;
|
|
23
|
+
type ComponentRelationId<T = void> = EntityId<T, "component-relation">;
|
|
24
|
+
type WildcardRelationId<T = void> = EntityId<T, "wildcard-relation">;
|
|
25
|
+
type RelationId<T = void> = EntityRelationId<T> | ComponentRelationId<T> | WildcardRelationId<T>;
|
|
26
|
+
/**
|
|
27
|
+
* Type for relation ID based on component and target types
|
|
28
|
+
*/
|
|
29
|
+
type RelationIdType<T, R> = R extends ComponentId<infer U> ? U extends void ? ComponentRelationId<T> : ComponentRelationId<T extends void ? U : T> : R extends EntityId<any> ? EntityRelationId<T> : never;
|
|
30
|
+
/**
|
|
31
|
+
* Create a relation ID by associating a component with another ID (entity or component)
|
|
32
|
+
* @param componentId The component ID (0-1023)
|
|
33
|
+
* @param targetId The target ID (entity, component, or '*' for wildcard)
|
|
34
|
+
*/
|
|
35
|
+
declare function relation<T>(componentId: ComponentId<T>, targetId: "*"): WildcardRelationId<T>;
|
|
36
|
+
declare function relation<T, R extends EntityId<any>>(componentId: ComponentId<T>, targetId: R): RelationIdType<T, R>;
|
|
37
|
+
/**
|
|
38
|
+
* Check if an ID is a component ID
|
|
39
|
+
*/
|
|
40
|
+
declare function isComponentId<T>(id: EntityId<T>): id is ComponentId<T>;
|
|
41
|
+
/**
|
|
42
|
+
* Check if an ID is an entity ID
|
|
43
|
+
*/
|
|
44
|
+
declare function isEntityId<T>(id: EntityId<T>): id is EntityId<T>;
|
|
45
|
+
/**
|
|
46
|
+
* Check if an ID is a relation ID
|
|
47
|
+
*/
|
|
48
|
+
declare function isRelationId<T>(id: EntityId<T>): id is RelationId<T>;
|
|
49
|
+
/**
|
|
50
|
+
* Check if an ID is a wildcard relation id
|
|
51
|
+
*/
|
|
52
|
+
declare function isWildcardRelationId<T>(id: EntityId<T>): id is WildcardRelationId<T>;
|
|
53
|
+
/**
|
|
54
|
+
* Decode a relation ID into component and target IDs
|
|
55
|
+
* @param relationId The relation ID (must be negative)
|
|
56
|
+
* @returns Object with componentId, targetId, and relation type
|
|
57
|
+
*/
|
|
58
|
+
declare function decodeRelationId(relationId: RelationId<any>): {
|
|
59
|
+
componentId: ComponentId<any>;
|
|
60
|
+
targetId: EntityId<any>;
|
|
61
|
+
type: "entity" | "component" | "wildcard";
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Component options that define intrinsic properties
|
|
65
|
+
*/
|
|
66
|
+
interface ComponentOptions {
|
|
67
|
+
/**
|
|
68
|
+
* Optional name for the component (for serialization/debugging)
|
|
69
|
+
*/
|
|
70
|
+
name?: string;
|
|
71
|
+
/**
|
|
72
|
+
* If true, an entity can have at most one relation per base component.
|
|
73
|
+
* When adding a new relation with the same base component, any existing relations
|
|
74
|
+
* with that base component are automatically removed.
|
|
75
|
+
* Only applicable to relation components.
|
|
76
|
+
*/
|
|
77
|
+
exclusive?: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* If true, when a relation target entity is deleted, all entities that reference
|
|
80
|
+
* it through this component will also be deleted (cascade delete).
|
|
81
|
+
* Only applicable to entity-relation components.
|
|
82
|
+
*/
|
|
83
|
+
cascadeDelete?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* If true, relations with this component will not cause archetype fragmentation.
|
|
86
|
+
* Entities with different target entities for this relation component will be stored
|
|
87
|
+
* in the same archetype, preventing fragmentation when there are many different targets.
|
|
88
|
+
* Only applicable to relation components.
|
|
89
|
+
* Inspired by Flecs' DontFragment trait.
|
|
90
|
+
*/
|
|
91
|
+
dontFragment?: boolean;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Allocate a new component ID from the global allocator.
|
|
95
|
+
* @param nameOrOptions Optional name for the component (for serialization/debugging) or options object
|
|
96
|
+
* @returns The allocated component ID
|
|
97
|
+
* @example
|
|
98
|
+
* // Just a name
|
|
99
|
+
* const Position = component<Position>("Position");
|
|
100
|
+
*
|
|
101
|
+
* // With options
|
|
102
|
+
* const ChildOf = component({ exclusive: true, cascadeDelete: true });
|
|
103
|
+
*
|
|
104
|
+
* // With name and options
|
|
105
|
+
* const ChildOf = component({ name: "ChildOf", exclusive: true });
|
|
106
|
+
*/
|
|
107
|
+
declare function component<T = void>(nameOrOptions?: string | ComponentOptions): ComponentId<T>;
|
|
108
|
+
/**
|
|
109
|
+
* Get a component ID by its registered name
|
|
110
|
+
* @param name The component name
|
|
111
|
+
* @returns The component ID if found, undefined otherwise
|
|
112
|
+
*/
|
|
113
|
+
declare function getComponentIdByName(name: string): ComponentId<any> | undefined;
|
|
114
|
+
/** Get a component name by its ID
|
|
115
|
+
* @param id The component ID
|
|
116
|
+
* @returns The component name if found, undefined otherwise
|
|
117
|
+
*/
|
|
118
|
+
declare function getComponentNameById(id: ComponentId<any>): string | undefined;
|
|
119
|
+
//#endregion
|
|
120
|
+
//#region src/types.d.ts
|
|
121
|
+
/**
|
|
122
|
+
* Hook types for component lifecycle events
|
|
123
|
+
*/
|
|
124
|
+
interface LifecycleHook<T = unknown> {
|
|
125
|
+
/**
|
|
126
|
+
* Called when a component is added to an entity
|
|
127
|
+
*/
|
|
128
|
+
on_init?: (entityId: EntityId, componentType: EntityId<T>, component: T) => void;
|
|
129
|
+
/**
|
|
130
|
+
* Called when a component is added to an entity
|
|
131
|
+
*/
|
|
132
|
+
on_set?: (entityId: EntityId, componentType: EntityId<T>, component: T) => void;
|
|
133
|
+
/**
|
|
134
|
+
* Called when a component is deleted from an entity
|
|
135
|
+
*/
|
|
136
|
+
on_remove?: (entityId: EntityId, componentType: EntityId<T>, component: T) => void;
|
|
137
|
+
}
|
|
138
|
+
type ComponentType<T> = EntityId<T> | OptionalEntityId<T>;
|
|
139
|
+
type OptionalEntityId<T> = {
|
|
140
|
+
optional: EntityId<T>;
|
|
141
|
+
};
|
|
142
|
+
type ComponentTypeToData<T> = T extends {
|
|
143
|
+
optional: infer U;
|
|
144
|
+
} ? {
|
|
145
|
+
value: ComponentTypeToData<U>;
|
|
146
|
+
} | undefined : T extends WildcardRelationId<infer U> ? [EntityId<unknown>, U][] : T extends EntityId<infer U> ? U : never;
|
|
147
|
+
/**
|
|
148
|
+
* Type helper for component tuples extracted from EntityId array
|
|
149
|
+
*/
|
|
150
|
+
type ComponentTuple<T extends readonly ComponentType<any>[]> = { readonly [K in keyof T]: ComponentTypeToData<T[K]> };
|
|
151
|
+
//#endregion
|
|
152
|
+
//#region src/archetype.d.ts
|
|
153
|
+
/**
|
|
154
|
+
* Archetype class for ECS architecture
|
|
155
|
+
* Represents a group of entities that share the same set of components
|
|
156
|
+
* Optimized for fast iteration and component access
|
|
157
|
+
*/
|
|
158
|
+
declare class Archetype {
|
|
159
|
+
/**
|
|
160
|
+
* The component types that define this archetype
|
|
161
|
+
*/
|
|
162
|
+
readonly componentTypes: EntityId<any>[];
|
|
163
|
+
/**
|
|
164
|
+
* List of entities in this archetype
|
|
165
|
+
*/
|
|
166
|
+
private entities;
|
|
167
|
+
/**
|
|
168
|
+
* Component data storage - maps component type to array of component data
|
|
169
|
+
* Each array index corresponds to the entity index in the entities array
|
|
170
|
+
*/
|
|
171
|
+
private componentData;
|
|
172
|
+
/**
|
|
173
|
+
* Reverse mapping from entity to its index in this archetype
|
|
174
|
+
*/
|
|
175
|
+
private entityToIndex;
|
|
176
|
+
/**
|
|
177
|
+
* Reference to dontFragment relations storage from World
|
|
178
|
+
* This allows entities with different relation targets to share the same archetype
|
|
179
|
+
* Stored in World to avoid migration overhead when entities change archetypes
|
|
180
|
+
*/
|
|
181
|
+
private dontFragmentRelations;
|
|
182
|
+
/**
|
|
183
|
+
* Cache for pre-computed component data sources to avoid repeated calculations
|
|
184
|
+
* For regular components: data array
|
|
185
|
+
* For wildcards: matching relation types array
|
|
186
|
+
*/
|
|
187
|
+
private componentDataSourcesCache;
|
|
188
|
+
/**
|
|
189
|
+
* Create a new archetype with the specified component types
|
|
190
|
+
* @param componentTypes The component types that define this archetype
|
|
191
|
+
* @param dontFragmentRelations Reference to the World's dontFragmentRelations storage
|
|
192
|
+
*/
|
|
193
|
+
constructor(componentTypes: EntityId<any>[], dontFragmentRelations: Map<EntityId, Map<EntityId<any>, any>>);
|
|
194
|
+
/**
|
|
195
|
+
* Get the number of entities in this archetype
|
|
196
|
+
*/
|
|
197
|
+
get size(): number;
|
|
198
|
+
/**
|
|
199
|
+
* Check if this archetype matches the given component types
|
|
200
|
+
* @param componentTypes The component types to check
|
|
201
|
+
*/
|
|
202
|
+
matches(componentTypes: EntityId<any>[]): boolean;
|
|
203
|
+
/**
|
|
204
|
+
* Add an entity to this archetype with initial component data
|
|
205
|
+
* @param entityId The entity to add
|
|
206
|
+
* @param componentData Map of component type to component data (includes both regular and dontFragment components)
|
|
207
|
+
*/
|
|
208
|
+
addEntity(entityId: EntityId, componentData: Map<EntityId<any>, any>): void;
|
|
209
|
+
/**
|
|
210
|
+
* Get all component data for a specific entity
|
|
211
|
+
* @param entityId The entity to get data for
|
|
212
|
+
* @returns Map of component type to component data (includes both regular and dontFragment components)
|
|
213
|
+
*/
|
|
214
|
+
getEntity(entityId: EntityId): Map<EntityId<any>, any> | undefined;
|
|
215
|
+
/**
|
|
216
|
+
* Dump all entities and their component data in this archetype
|
|
217
|
+
* @returns Array of objects with entity and component data (includes both regular and dontFragment components)
|
|
218
|
+
*/
|
|
219
|
+
dump(): Array<{
|
|
220
|
+
entity: EntityId;
|
|
221
|
+
components: Map<EntityId<any>, any>;
|
|
222
|
+
}>;
|
|
223
|
+
/**
|
|
224
|
+
* Remove an entity from this archetype
|
|
225
|
+
* @param entityId The entity to remove
|
|
226
|
+
* @returns The component data of the removed entity (includes both regular and dontFragment components)
|
|
227
|
+
*/
|
|
228
|
+
removeEntity(entityId: EntityId): Map<EntityId<any>, any> | undefined;
|
|
229
|
+
/**
|
|
230
|
+
* Check if an entity is in this archetype
|
|
231
|
+
* @param entityId The entity to check
|
|
232
|
+
*/
|
|
233
|
+
exists(entityId: EntityId): boolean;
|
|
234
|
+
/**
|
|
235
|
+
* Get component data for a specific entity and wildcard relation type
|
|
236
|
+
* Returns an array of all matching relation instances
|
|
237
|
+
* @param entityId The entity
|
|
238
|
+
* @param componentType The wildcard relation type
|
|
239
|
+
*/
|
|
240
|
+
get<T>(entityId: EntityId, componentType: WildcardRelationId<T>): [EntityId<unknown>, any][];
|
|
241
|
+
/**
|
|
242
|
+
* Get component data for a specific entity and component type
|
|
243
|
+
* @param entityId The entity
|
|
244
|
+
* @param componentType The component type
|
|
245
|
+
*/
|
|
246
|
+
get<T>(entityId: EntityId, componentType: EntityId<T>): T;
|
|
247
|
+
/**
|
|
248
|
+
* Set component data for a specific entity and component type
|
|
249
|
+
* @param entityId The entity
|
|
250
|
+
* @param componentType The component type
|
|
251
|
+
* @param data The component data
|
|
252
|
+
*/
|
|
253
|
+
set<T>(entityId: EntityId, componentType: EntityId<T>, data: T): void;
|
|
254
|
+
/**
|
|
255
|
+
* Get all entities in this archetype
|
|
256
|
+
*/
|
|
257
|
+
getEntities(): EntityId[];
|
|
258
|
+
/**
|
|
259
|
+
* Get the mapping of entities to their indices in this archetype
|
|
260
|
+
*/
|
|
261
|
+
getEntityToIndexMap(): Map<EntityId, number>;
|
|
262
|
+
/**
|
|
263
|
+
* Get component data for all entities of a specific component type
|
|
264
|
+
* @param componentType The component type
|
|
265
|
+
*/
|
|
266
|
+
getComponentData<T>(componentType: EntityId<T>): T[];
|
|
267
|
+
/**
|
|
268
|
+
* Get optional component data for all entities of a specific component type
|
|
269
|
+
* @param componentType The component type
|
|
270
|
+
* @returns An array of component data or undefined if not present
|
|
271
|
+
*/
|
|
272
|
+
getOptionalComponentData<T>(componentType: EntityId<T>): T[] | undefined;
|
|
273
|
+
/**
|
|
274
|
+
* Helper: compute or return cached data sources for provided componentTypes
|
|
275
|
+
*/
|
|
276
|
+
private getCachedComponentDataSources;
|
|
277
|
+
/**
|
|
278
|
+
* Build cache key for component types
|
|
279
|
+
*/
|
|
280
|
+
private buildCacheKey;
|
|
281
|
+
/**
|
|
282
|
+
* Get data source for a single component type
|
|
283
|
+
*/
|
|
284
|
+
private getComponentDataSource;
|
|
285
|
+
/**
|
|
286
|
+
* Get data source for wildcard relations
|
|
287
|
+
*/
|
|
288
|
+
private getWildcardRelationDataSource;
|
|
289
|
+
/**
|
|
290
|
+
* Helper: build component tuples for a specific entity index using precomputed data sources
|
|
291
|
+
*/
|
|
292
|
+
private buildComponentsForIndex;
|
|
293
|
+
/**
|
|
294
|
+
* Build a single component value from its data source
|
|
295
|
+
*/
|
|
296
|
+
private buildSingleComponent;
|
|
297
|
+
/**
|
|
298
|
+
* Build wildcard relation value from matching relations
|
|
299
|
+
*/
|
|
300
|
+
private buildWildcardRelationValue;
|
|
301
|
+
/**
|
|
302
|
+
* Build regular component value from data source
|
|
303
|
+
*/
|
|
304
|
+
private buildRegularComponentValue;
|
|
305
|
+
/**
|
|
306
|
+
* Get entities with their component data for specified component types
|
|
307
|
+
* Optimized for bulk component access with pre-computed indices
|
|
308
|
+
* @param componentTypes Array of component types to retrieve
|
|
309
|
+
* @returns Array of objects with entity and component data
|
|
310
|
+
*/
|
|
311
|
+
getEntitiesWithComponents<const T extends readonly ComponentType<any>[]>(componentTypes: T): Array<{
|
|
312
|
+
entity: EntityId;
|
|
313
|
+
components: ComponentTuple<T>;
|
|
314
|
+
}>;
|
|
315
|
+
/**
|
|
316
|
+
* Iterate over entities with their component data for specified component types
|
|
317
|
+
* implemented as a generator returning each entity/components pair lazily
|
|
318
|
+
* @param componentTypes Array of component types to retrieve
|
|
319
|
+
*/
|
|
320
|
+
iterateWithComponents<const T extends readonly ComponentType<any>[]>(componentTypes: T): IterableIterator<[EntityId, ...ComponentTuple<T>]>;
|
|
321
|
+
/**
|
|
322
|
+
* Iterate over entities with their component data for specified component types
|
|
323
|
+
* Optimized for bulk component access
|
|
324
|
+
* @param componentTypes Array of component types to retrieve
|
|
325
|
+
* @param callback Function called for each entity with its components
|
|
326
|
+
*/
|
|
327
|
+
forEachWithComponents<const T extends readonly ComponentType<any>[]>(componentTypes: T, callback: (entity: EntityId, ...components: ComponentTuple<T>) => void): void;
|
|
328
|
+
/**
|
|
329
|
+
* Iterate over all entities with their component data
|
|
330
|
+
* @param callback Function called for each entity with its component data
|
|
331
|
+
*/
|
|
332
|
+
forEach(callback: (entityId: EntityId, components: Map<EntityId<any>, any>) => void): void;
|
|
333
|
+
/**
|
|
334
|
+
* Check if any entity in this archetype has a relation matching the given component ID
|
|
335
|
+
* This includes both regular relations in componentTypes and dontFragment relations
|
|
336
|
+
* @param componentId The component ID to match
|
|
337
|
+
* @returns true if any entity has a matching relation
|
|
338
|
+
*/
|
|
339
|
+
hasRelationWithComponentId(componentId: EntityId<any>): boolean;
|
|
340
|
+
}
|
|
341
|
+
//#endregion
|
|
342
|
+
//#region src/changeset.d.ts
|
|
343
|
+
/**
|
|
344
|
+
* @internal Represents a set of component changes to be applied to an entity
|
|
345
|
+
*/
|
|
346
|
+
declare class ComponentChangeset {
|
|
347
|
+
readonly adds: Map<EntityId<any>, any>;
|
|
348
|
+
readonly removes: Set<EntityId<any>>;
|
|
349
|
+
/**
|
|
350
|
+
* Add a component to the changeset
|
|
351
|
+
*/
|
|
352
|
+
set<T>(componentType: EntityId<T>, component: T): void;
|
|
353
|
+
/**
|
|
354
|
+
* Remove a component from the changeset
|
|
355
|
+
*/
|
|
356
|
+
delete<T>(componentType: EntityId<T>): void;
|
|
357
|
+
/**
|
|
358
|
+
* Check if the changeset has any changes
|
|
359
|
+
*/
|
|
360
|
+
hasChanges(): boolean;
|
|
361
|
+
/**
|
|
362
|
+
* Clear all changes
|
|
363
|
+
*/
|
|
364
|
+
clear(): void;
|
|
365
|
+
/**
|
|
366
|
+
* Merge another changeset into this one
|
|
367
|
+
*/
|
|
368
|
+
merge(other: ComponentChangeset): void;
|
|
369
|
+
/**
|
|
370
|
+
* Apply the changeset to existing components and return the final state
|
|
371
|
+
*/
|
|
372
|
+
applyTo(existingComponents: Map<EntityId<any>, any>): Map<EntityId<any>, any>;
|
|
373
|
+
/**
|
|
374
|
+
* Get the final component types after applying the changeset
|
|
375
|
+
* @param existingComponentTypes - The current component types on the entity
|
|
376
|
+
* @returns The final component types or undefined if no changes
|
|
377
|
+
*/
|
|
378
|
+
getFinalComponentTypes(existingComponentTypes: EntityId<any>[]): EntityId<any>[] | undefined;
|
|
379
|
+
}
|
|
380
|
+
//#endregion
|
|
381
|
+
//#region src/command-buffer.d.ts
|
|
382
|
+
/**
|
|
383
|
+
* Command for deferred execution
|
|
384
|
+
*/
|
|
385
|
+
interface Command {
|
|
386
|
+
type: "set" | "delete" | "destroy";
|
|
387
|
+
entityId: EntityId;
|
|
388
|
+
componentType?: EntityId<any>;
|
|
389
|
+
component?: any;
|
|
390
|
+
}
|
|
391
|
+
//#endregion
|
|
392
|
+
//#region src/query-filter.d.ts
|
|
393
|
+
/**
|
|
394
|
+
* Filter options for queries
|
|
395
|
+
*/
|
|
396
|
+
interface QueryFilter {
|
|
397
|
+
negativeComponentTypes?: EntityId<any>[];
|
|
398
|
+
}
|
|
399
|
+
//#endregion
|
|
400
|
+
//#region src/query.d.ts
|
|
401
|
+
/**
|
|
402
|
+
* Query class for efficient entity queries with cached archetypes
|
|
403
|
+
*/
|
|
404
|
+
declare class Query {
|
|
405
|
+
private world;
|
|
406
|
+
private componentTypes;
|
|
407
|
+
private filter;
|
|
408
|
+
private cachedArchetypes;
|
|
409
|
+
private isDisposed;
|
|
410
|
+
constructor(world: World, componentTypes: EntityId<any>[], filter?: QueryFilter);
|
|
411
|
+
/**
|
|
412
|
+
* Check if query is disposed and throw error if so
|
|
413
|
+
*/
|
|
414
|
+
private ensureNotDisposed;
|
|
415
|
+
/**
|
|
416
|
+
* Get all entities matching the query
|
|
417
|
+
*/
|
|
418
|
+
getEntities(): EntityId[];
|
|
419
|
+
/**
|
|
420
|
+
* Get entities with their component data
|
|
421
|
+
* @param componentTypes Array of component types to retrieve
|
|
422
|
+
* @returns Array of objects with entity and component data
|
|
423
|
+
*/
|
|
424
|
+
getEntitiesWithComponents<const T extends readonly ComponentType<any>[]>(componentTypes: T): Array<{
|
|
425
|
+
entity: EntityId;
|
|
426
|
+
components: ComponentTuple<T>;
|
|
427
|
+
}>;
|
|
428
|
+
/**
|
|
429
|
+
* Iterate over entities with their component data
|
|
430
|
+
* @param componentTypes Array of component types to retrieve
|
|
431
|
+
* @param callback Function called for each entity with its components
|
|
432
|
+
*/
|
|
433
|
+
forEach<const T extends readonly ComponentType<any>[]>(componentTypes: T, callback: (entity: EntityId, ...components: ComponentTuple<T>) => void): void;
|
|
434
|
+
/**
|
|
435
|
+
* Iterate over entities with their component data (generator)
|
|
436
|
+
* @param componentTypes Array of component types to retrieve
|
|
437
|
+
*/
|
|
438
|
+
iterate<const T extends readonly ComponentType<any>[]>(componentTypes: T): IterableIterator<[EntityId, ...ComponentTuple<T>]>;
|
|
439
|
+
/**
|
|
440
|
+
* Get component data arrays for all matching entities
|
|
441
|
+
* @param componentType The component type to retrieve
|
|
442
|
+
* @returns Array of component data for all matching entities
|
|
443
|
+
*/
|
|
444
|
+
getComponentData<T>(componentType: EntityId<T>): T[];
|
|
445
|
+
/**
|
|
446
|
+
* Update the cached archetypes
|
|
447
|
+
* Called when new archetypes are created
|
|
448
|
+
*/
|
|
449
|
+
updateCache(): void;
|
|
450
|
+
/**
|
|
451
|
+
* Check if a new archetype matches this query and add to cache if it does
|
|
452
|
+
*/
|
|
453
|
+
checkNewArchetype(archetype: Archetype): void;
|
|
454
|
+
/**
|
|
455
|
+
* Remove an archetype from the cached archetypes
|
|
456
|
+
*/
|
|
457
|
+
removeArchetype(archetype: Archetype): void;
|
|
458
|
+
/**
|
|
459
|
+
* Dispose the query and disconnect from world
|
|
460
|
+
*/
|
|
461
|
+
/**
|
|
462
|
+
* Request disposal of this query.
|
|
463
|
+
* This will decrement the world's reference count for the query.
|
|
464
|
+
* The query will only be fully disposed when the ref count reaches zero.
|
|
465
|
+
*/
|
|
466
|
+
dispose(): void;
|
|
467
|
+
/**
|
|
468
|
+
* Internal full dispose called by World when refCount reaches zero.
|
|
469
|
+
*/
|
|
470
|
+
_disposeInternal(): void;
|
|
471
|
+
/**
|
|
472
|
+
* Symbol.dispose implementation for automatic resource management
|
|
473
|
+
*/
|
|
474
|
+
[Symbol.dispose](): void;
|
|
475
|
+
/**
|
|
476
|
+
* Check if the query has been disposed
|
|
477
|
+
*/
|
|
478
|
+
get disposed(): boolean;
|
|
479
|
+
}
|
|
480
|
+
//#endregion
|
|
481
|
+
//#region src/world.d.ts
|
|
482
|
+
/**
|
|
483
|
+
* World class for ECS architecture
|
|
484
|
+
* Manages entities and components
|
|
485
|
+
*/
|
|
486
|
+
declare class World {
|
|
487
|
+
/** Manages allocation and deallocation of entity IDs */
|
|
488
|
+
private entityIdManager;
|
|
489
|
+
/** Array of all archetypes in the world */
|
|
490
|
+
private archetypes;
|
|
491
|
+
/** Maps archetype signatures (component type signatures) to archetype instances */
|
|
492
|
+
private archetypeBySignature;
|
|
493
|
+
/** Maps entity IDs to their current archetype */
|
|
494
|
+
private entityToArchetype;
|
|
495
|
+
/** Maps component types to arrays of archetypes that contain them */
|
|
496
|
+
private archetypesByComponent;
|
|
497
|
+
/** Tracks which entities reference each entity as a component type */
|
|
498
|
+
private entityReferences;
|
|
499
|
+
/** Storage for dontFragment relations - maps entity ID to a map of relation type to component data */
|
|
500
|
+
private dontFragmentRelations;
|
|
501
|
+
/** Array of all active queries for archetype change notifications */
|
|
502
|
+
private queries;
|
|
503
|
+
/** Cache for queries keyed by component types and filter signatures */
|
|
504
|
+
private queryCache;
|
|
505
|
+
/** Buffers structural changes for deferred execution */
|
|
506
|
+
private commandBuffer;
|
|
507
|
+
/** Stores lifecycle hooks for component and relation events */
|
|
508
|
+
private hooks;
|
|
509
|
+
/**
|
|
510
|
+
* Create a new World.
|
|
511
|
+
* If an optional snapshot object is provided (previously produced by `world.serialize()`),
|
|
512
|
+
* the world will be restored from that snapshot. The snapshot may contain non-JSON values.
|
|
513
|
+
*/
|
|
514
|
+
constructor(snapshot?: SerializedWorld);
|
|
515
|
+
/**
|
|
516
|
+
* Generate a signature string for component types array
|
|
517
|
+
* @returns A string signature for the component types
|
|
518
|
+
*/
|
|
519
|
+
private createArchetypeSignature;
|
|
520
|
+
/**
|
|
521
|
+
* Create a new entity
|
|
522
|
+
* @returns The ID of the newly created entity
|
|
523
|
+
*/
|
|
524
|
+
new(): EntityId;
|
|
525
|
+
/**
|
|
526
|
+
* Destroy an entity and remove all its components (immediate execution)
|
|
527
|
+
*/
|
|
528
|
+
private destroyEntityImmediate;
|
|
529
|
+
/**
|
|
530
|
+
* Check if an entity exists
|
|
531
|
+
*/
|
|
532
|
+
exists(entityId: EntityId): boolean;
|
|
533
|
+
/**
|
|
534
|
+
* Add a component to an entity (deferred)
|
|
535
|
+
*/
|
|
536
|
+
set(entityId: EntityId, componentType: EntityId<void>): void;
|
|
537
|
+
set<T>(entityId: EntityId, componentType: EntityId<T>, component: NoInfer<T>): void;
|
|
538
|
+
/**
|
|
539
|
+
* Remove a component from an entity (deferred)
|
|
540
|
+
*/
|
|
541
|
+
remove<T>(entityId: EntityId, componentType: EntityId<T>): void;
|
|
542
|
+
/**
|
|
543
|
+
* Destroy an entity and remove all its components (deferred)
|
|
544
|
+
*/
|
|
545
|
+
delete(entityId: EntityId): void;
|
|
546
|
+
/**
|
|
547
|
+
* Check if an entity has a specific component
|
|
548
|
+
*/
|
|
549
|
+
has<T>(entityId: EntityId, componentType: EntityId<T>): boolean;
|
|
550
|
+
/**
|
|
551
|
+
* Get component data for a specific entity and wildcard relation type
|
|
552
|
+
* Returns an array of all matching relation instances
|
|
553
|
+
* @param entityId The entity
|
|
554
|
+
* @param componentType The wildcard relation type
|
|
555
|
+
* @returns Array of [targetEntityId, componentData] pairs for all matching relations
|
|
556
|
+
*/
|
|
557
|
+
get<T>(entityId: EntityId, componentType: WildcardRelationId<T>): [EntityId<unknown>, T][];
|
|
558
|
+
/**
|
|
559
|
+
* Get component data for a specific entity and component type
|
|
560
|
+
* @param entityId The entity
|
|
561
|
+
* @param componentType The component type
|
|
562
|
+
* @returns The component data
|
|
563
|
+
*/
|
|
564
|
+
get<T>(entityId: EntityId, componentType: EntityId<T>): T;
|
|
565
|
+
/**
|
|
566
|
+
* Register a lifecycle hook for component or wildcard relation events
|
|
567
|
+
*/
|
|
568
|
+
hook<T>(componentType: EntityId<T>, hook: LifecycleHook<T>): void;
|
|
569
|
+
/**
|
|
570
|
+
* Unregister a lifecycle hook for component or wildcard relation events
|
|
571
|
+
*/
|
|
572
|
+
unhook<T>(componentType: EntityId<T>, hook: LifecycleHook<T>): void;
|
|
573
|
+
/**
|
|
574
|
+
* Execute all deferred commands immediately
|
|
575
|
+
*/
|
|
576
|
+
sync(): void;
|
|
577
|
+
/**
|
|
578
|
+
* Create a cached query for efficient entity lookups
|
|
579
|
+
* @returns A Query object for the specified component types and filter
|
|
580
|
+
*/
|
|
581
|
+
createQuery(componentTypes: EntityId<any>[], filter?: QueryFilter): Query;
|
|
582
|
+
/**
|
|
583
|
+
* @internal Register a query for archetype update notifications
|
|
584
|
+
*/
|
|
585
|
+
_registerQuery(query: Query): void;
|
|
586
|
+
/**
|
|
587
|
+
* @internal Unregister a query
|
|
588
|
+
*/
|
|
589
|
+
_unregisterQuery(query: Query): void;
|
|
590
|
+
/**
|
|
591
|
+
* Release a query reference obtained from createQuery.
|
|
592
|
+
* Decrements the refCount and fully disposes the query when it reaches zero.
|
|
593
|
+
*/
|
|
594
|
+
releaseQuery(query: Query): void;
|
|
595
|
+
/**
|
|
596
|
+
* @internal Get archetypes that match specific component types (for internal use by queries)
|
|
597
|
+
*/
|
|
598
|
+
getMatchingArchetypes(componentTypes: EntityId<any>[]): Archetype[];
|
|
599
|
+
/**
|
|
600
|
+
* Query entities with specific components
|
|
601
|
+
* @returns Array of entity IDs that have all the specified components
|
|
602
|
+
*/
|
|
603
|
+
query(componentTypes: EntityId<any>[]): EntityId[];
|
|
604
|
+
query<const T extends readonly EntityId<any>[]>(componentTypes: T, includeComponents: true): Array<{
|
|
605
|
+
entity: EntityId;
|
|
606
|
+
components: ComponentTuple<T>;
|
|
607
|
+
}>;
|
|
608
|
+
/**
|
|
609
|
+
* @internal Execute commands for a single entity (for internal use by CommandBuffer)
|
|
610
|
+
* @returns ComponentChangeset describing the changes made
|
|
611
|
+
*/
|
|
612
|
+
executeEntityCommands(entityId: EntityId, commands: Command[]): ComponentChangeset;
|
|
613
|
+
/**
|
|
614
|
+
* Process commands and populate the changeset
|
|
615
|
+
*/
|
|
616
|
+
private processCommands;
|
|
617
|
+
/**
|
|
618
|
+
* Process a set command, handling exclusive relations
|
|
619
|
+
*/
|
|
620
|
+
private processSetCommand;
|
|
621
|
+
/**
|
|
622
|
+
* Remove all relations with the same base component (for exclusive relations)
|
|
623
|
+
*/
|
|
624
|
+
private removeExclusiveRelations;
|
|
625
|
+
private isRelationWithComponent;
|
|
626
|
+
/**
|
|
627
|
+
* Process a delete command, handling wildcard relations
|
|
628
|
+
*/
|
|
629
|
+
private processDeleteCommand;
|
|
630
|
+
/**
|
|
631
|
+
* Remove all relations matching a wildcard component ID
|
|
632
|
+
*/
|
|
633
|
+
private removeWildcardRelations;
|
|
634
|
+
/**
|
|
635
|
+
* Remove a single component from an entity immediately, handling dontFragment relations correctly.
|
|
636
|
+
* Used by destroyEntityImmediate for non-cascade relation cleanup.
|
|
637
|
+
*/
|
|
638
|
+
private removeComponentImmediate;
|
|
639
|
+
/**
|
|
640
|
+
* Apply changeset to entity, moving to new archetype if needed
|
|
641
|
+
* @returns Map of removed components with their data
|
|
642
|
+
*/
|
|
643
|
+
private applyChangeset;
|
|
644
|
+
/**
|
|
645
|
+
* Move entity to a new archetype with updated components
|
|
646
|
+
*/
|
|
647
|
+
private moveEntityToNewArchetype;
|
|
648
|
+
/**
|
|
649
|
+
* Update entity in same archetype (no archetype change needed)
|
|
650
|
+
*/
|
|
651
|
+
private updateEntityInSameArchetype;
|
|
652
|
+
/**
|
|
653
|
+
* Apply dontFragment relation changes directly to World's storage
|
|
654
|
+
* This is much more efficient than the removeEntity + addEntity approach
|
|
655
|
+
*/
|
|
656
|
+
private applyDontFragmentChanges;
|
|
657
|
+
/**
|
|
658
|
+
* Update entity reference tracking based on changeset
|
|
659
|
+
*/
|
|
660
|
+
private updateEntityReferences;
|
|
661
|
+
/**
|
|
662
|
+
* Get or create an archetype for the given component types
|
|
663
|
+
* Filters out dontFragment relations from the archetype signature
|
|
664
|
+
* @returns The archetype for the given component types (excluding dontFragment relations)
|
|
665
|
+
*/
|
|
666
|
+
private ensureArchetype;
|
|
667
|
+
/**
|
|
668
|
+
* Compare two arrays of component types for equality (order-independent)
|
|
669
|
+
*/
|
|
670
|
+
private areComponentTypesEqual;
|
|
671
|
+
/**
|
|
672
|
+
* Filter out dontFragment relations from component types, but keep wildcard markers
|
|
673
|
+
*/
|
|
674
|
+
private filterRegularComponentTypes;
|
|
675
|
+
/**
|
|
676
|
+
* Create a new archetype and register it with all tracking structures
|
|
677
|
+
*/
|
|
678
|
+
private createNewArchetype;
|
|
679
|
+
/**
|
|
680
|
+
* Register archetype in the component-to-archetype index
|
|
681
|
+
*/
|
|
682
|
+
private registerArchetypeInComponentIndex;
|
|
683
|
+
/**
|
|
684
|
+
* Notify all queries to check the new archetype
|
|
685
|
+
*/
|
|
686
|
+
private notifyQueriesOfNewArchetype;
|
|
687
|
+
/**
|
|
688
|
+
* Add a component reference to the reverse index when an entity is used as a component type
|
|
689
|
+
* @param sourceEntityId The entity that has the component
|
|
690
|
+
* @param componentType The component type (which may be an entity ID used as component type)
|
|
691
|
+
* @param targetEntityId The entity being used as component type
|
|
692
|
+
*/
|
|
693
|
+
private trackEntityReference;
|
|
694
|
+
/**
|
|
695
|
+
* Remove a component reference from the reverse index
|
|
696
|
+
* @param sourceEntityId The entity that has the component
|
|
697
|
+
* @param componentType The component type
|
|
698
|
+
* @param targetEntityId The entity being used as component type
|
|
699
|
+
*/
|
|
700
|
+
private untrackEntityReference;
|
|
701
|
+
/**
|
|
702
|
+
* Get all component references where a target entity is used as a component type
|
|
703
|
+
* @param targetEntityId The target entity
|
|
704
|
+
* @returns A MultiMap of sourceEntityId to componentTypes that reference the target entity
|
|
705
|
+
*/
|
|
706
|
+
private getEntityReferences;
|
|
707
|
+
/**
|
|
708
|
+
* Check if an archetype's signature references a specific entity
|
|
709
|
+
* (via entity-relation targeting the entity, or using entity as component type)
|
|
710
|
+
*/
|
|
711
|
+
private archetypeReferencesEntity;
|
|
712
|
+
/**
|
|
713
|
+
* Cleanup empty archetypes that reference a specific deleted entity
|
|
714
|
+
* Only removes archetypes whose component types reference the entity
|
|
715
|
+
*/
|
|
716
|
+
private cleanupArchetypesReferencingEntity;
|
|
717
|
+
/**
|
|
718
|
+
* Remove an empty archetype from all internal data structures
|
|
719
|
+
*/
|
|
720
|
+
private cleanupEmptyArchetype;
|
|
721
|
+
/**
|
|
722
|
+
* Remove archetype from the main archetypes list
|
|
723
|
+
*/
|
|
724
|
+
private removeArchetypeFromList;
|
|
725
|
+
/**
|
|
726
|
+
* Remove archetype from the signature-to-archetype map
|
|
727
|
+
*/
|
|
728
|
+
private removeArchetypeFromSignatureMap;
|
|
729
|
+
/**
|
|
730
|
+
* Remove archetype from the component-to-archetypes index
|
|
731
|
+
*/
|
|
732
|
+
private removeArchetypeFromComponentIndex;
|
|
733
|
+
/**
|
|
734
|
+
* Remove archetype from all queries
|
|
735
|
+
*/
|
|
736
|
+
private removeArchetypeFromQueries;
|
|
737
|
+
/**
|
|
738
|
+
* Execute component lifecycle hooks for added and removed components
|
|
739
|
+
*/
|
|
740
|
+
private triggerLifecycleHooks;
|
|
741
|
+
/**
|
|
742
|
+
* Convert the world into a plain snapshot object.
|
|
743
|
+
* This returns an in-memory structure and does not perform JSON stringification.
|
|
744
|
+
* Component values are stored as-is (they may be non-JSON-serializable).
|
|
745
|
+
*/
|
|
746
|
+
serialize(): SerializedWorld;
|
|
747
|
+
}
|
|
748
|
+
type SerializedWorld = {
|
|
749
|
+
version: number;
|
|
750
|
+
entityManager: any;
|
|
751
|
+
entities: SerializedEntity[];
|
|
752
|
+
};
|
|
753
|
+
type SerializedEntity = {
|
|
754
|
+
id: number;
|
|
755
|
+
components: SerializedComponent[];
|
|
756
|
+
};
|
|
757
|
+
type SerializedComponent = {
|
|
758
|
+
type: number | string | {
|
|
759
|
+
component: string;
|
|
760
|
+
target: number | string;
|
|
761
|
+
};
|
|
762
|
+
value: any;
|
|
763
|
+
};
|
|
764
|
+
//#endregion
|
|
765
|
+
export { relation as S, getComponentNameById as _, ComponentType as a, isRelationId as b, ComponentOptions as c, EntityRelationId as d, RelationId as f, getComponentIdByName as g, decodeRelationId as h, ComponentTuple as i, ComponentRelationId as l, component as m, World as n, LifecycleHook as o, WildcardRelationId as p, Query as r, ComponentId as s, SerializedWorld as t, EntityId as u, isComponentId as v, isWildcardRelationId as x, isEntityId as y };
|
|
766
|
+
//# sourceMappingURL=world.d.mts.map
|