@codehz/ecs 0.3.7 → 0.3.8

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