@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/multi-map.d.ts DELETED
@@ -1,19 +0,0 @@
1
- declare class MultiMap<K, V> {
2
- private map;
3
- private _valueCount;
4
- get valueCount(): number;
5
- get keyCount(): number;
6
- hasKey(key: K): boolean;
7
- has(key: K, value?: V): boolean;
8
- add(key: K, value: V): void;
9
- remove(key: K, value: V): boolean;
10
- deleteKey(key: K): boolean;
11
- get(key: K): Set<V>;
12
- keys(): IterableIterator<K>;
13
- values(): IterableIterator<V>;
14
- [Symbol.iterator](): IterableIterator<[K, V]>;
15
- entries(): IterableIterator<[K, V]>;
16
- clear(): void;
17
- }
18
- export { MultiMap };
19
- export type { MultiMap as MultiMapType };
package/query-filter.d.ts DELETED
@@ -1,21 +0,0 @@
1
- import { Archetype } from "./archetype";
2
- import type { EntityId } from "./entity";
3
- /**
4
- * Filter options for queries
5
- */
6
- export interface QueryFilter {
7
- negativeComponentTypes?: EntityId<any>[];
8
- }
9
- /**
10
- * Serialize a QueryFilter into a deterministic string suitable for cache keys.
11
- * Currently only serializes `negativeComponentTypes`.
12
- */
13
- export declare function serializeQueryFilter(filter?: QueryFilter): string;
14
- /**
15
- * Check if an archetype matches the given component types
16
- */
17
- export declare function matchesComponentTypes(archetype: Archetype, componentTypes: EntityId<any>[]): boolean;
18
- /**
19
- * Check if an archetype matches the filter conditions (only filtering logic)
20
- */
21
- export declare function matchesFilter(archetype: Archetype, filter: QueryFilter): boolean;
package/query.d.ts DELETED
@@ -1,80 +0,0 @@
1
- import { Archetype } from "./archetype";
2
- import type { EntityId } from "./entity";
3
- import { type QueryFilter } from "./query-filter";
4
- import type { ComponentTuple, ComponentType } from "./types";
5
- import type { World } from "./world";
6
- /**
7
- * Query class for efficient entity queries with cached archetypes
8
- */
9
- export declare class Query {
10
- private world;
11
- private componentTypes;
12
- private filter;
13
- private cachedArchetypes;
14
- private isDisposed;
15
- constructor(world: World<any[]>, componentTypes: EntityId<any>[], filter?: QueryFilter);
16
- /**
17
- * Get all entities matching the query
18
- */
19
- getEntities(): EntityId[];
20
- /**
21
- * Get entities with their component data
22
- * @param componentTypes Array of component types to retrieve
23
- * @returns Array of objects with entity and component data
24
- */
25
- getEntitiesWithComponents<const T extends readonly ComponentType<any>[]>(componentTypes: T): Array<{
26
- entity: EntityId;
27
- components: ComponentTuple<T>;
28
- }>;
29
- /**
30
- * Iterate over entities with their component data
31
- * @param componentTypes Array of component types to retrieve
32
- * @param callback Function called for each entity with its components
33
- */
34
- forEach<const T extends readonly ComponentType<any>[]>(componentTypes: T, callback: (entity: EntityId, ...components: ComponentTuple<T>) => void): void;
35
- /**
36
- * Iterate over entities with their component data (generator)
37
- * @param componentTypes Array of component types to retrieve
38
- */
39
- iterate<const T extends readonly ComponentType<any>[]>(componentTypes: T): IterableIterator<[EntityId, ...ComponentTuple<T>]>;
40
- /**
41
- * Get component data arrays for all matching entities
42
- * @param componentType The component type to retrieve
43
- * @returns Array of component data for all matching entities
44
- */
45
- getComponentData<T>(componentType: EntityId<T>): T[];
46
- /**
47
- * Update the cached archetypes
48
- * Called when new archetypes are created
49
- */
50
- updateCache(): void;
51
- /**
52
- * Check if a new archetype matches this query and add to cache if it does
53
- */
54
- checkNewArchetype(archetype: Archetype): void;
55
- /**
56
- * Remove an archetype from the cached archetypes
57
- */
58
- removeArchetype(archetype: Archetype): void;
59
- /**
60
- * Dispose the query and disconnect from world
61
- */
62
- /**
63
- * Request disposal of this query.
64
- * This will decrement the world's reference count for the query.
65
- * The query will only be fully disposed when the ref count reaches zero.
66
- */
67
- dispose(): void;
68
- /**
69
- * Internal full dispose called by World when refCount reaches zero.
70
- */
71
- _disposeInternal(): void;
72
- /**
73
- * Symbol.dispose implementation for automatic resource management
74
- */
75
- [Symbol.dispose](): void;
76
- /**
77
- * Check if the query has been disposed
78
- */
79
- get disposed(): boolean;
80
- }
@@ -1,25 +0,0 @@
1
- import type { System } from "./system";
2
- /**
3
- * System Scheduler for managing system dependencies and execution order
4
- */
5
- export declare class SystemScheduler<UpdateParams extends any[] = []> {
6
- private systems;
7
- private systemDependencies;
8
- private cachedExecutionOrder;
9
- /**
10
- * Add a system with optional dependencies
11
- * @param system The system to add
12
- * @param additionalDeps Additional dependencies for the system
13
- */
14
- addSystem(system: System<UpdateParams>, additionalDeps?: System<UpdateParams>[]): void;
15
- /**
16
- * Get the execution order of systems based on dependencies
17
- * Uses topological sort
18
- */
19
- getExecutionOrder(): System<UpdateParams>[];
20
- update(...params: UpdateParams): Promise<void[]> | void;
21
- /**
22
- * Clear all systems and dependencies
23
- */
24
- clear(): void;
25
- }
package/system.d.ts DELETED
@@ -1,13 +0,0 @@
1
- /**
2
- * Base System interface
3
- */
4
- export interface System<UpdateParams extends any[] = []> {
5
- /**
6
- * Update the system
7
- */
8
- update(...params: UpdateParams): void | Promise<void>;
9
- /**
10
- * Dependencies of this system (systems that must run before this one)
11
- */
12
- readonly dependencies?: readonly System<UpdateParams>[];
13
- }
package/types.d.ts DELETED
@@ -1,34 +0,0 @@
1
- import type { EntityId, WildcardRelationId } from "./entity";
2
- /**
3
- * Hook types for component lifecycle events
4
- */
5
- export interface LifecycleHook<T = unknown> {
6
- /**
7
- * Called when a component is added to an entity
8
- */
9
- on_init?: (entityId: EntityId, componentType: EntityId<T>, component: T) => void;
10
- /**
11
- * Called when a component is added to an entity
12
- */
13
- on_set?: (entityId: EntityId, componentType: EntityId<T>, component: T) => void;
14
- /**
15
- * Called when a component is deleted from an entity
16
- */
17
- on_remove?: (entityId: EntityId, componentType: EntityId<T>, component: T) => void;
18
- }
19
- export type ComponentType<T> = EntityId<T> | OptionalEntityId<T>;
20
- export type OptionalEntityId<T> = {
21
- optional: EntityId<T>;
22
- };
23
- export declare function isOptionalEntityId<T>(type: ComponentType<T>): type is OptionalEntityId<T>;
24
- export type ComponentTypeToData<T> = T extends {
25
- optional: infer U;
26
- } ? {
27
- value: ComponentTypeToData<U>;
28
- } | undefined : T extends WildcardRelationId<infer U> ? [EntityId<unknown>, U][] : T extends EntityId<infer U> ? U : never;
29
- /**
30
- * Type helper for component tuples extracted from EntityId array
31
- */
32
- export type ComponentTuple<T extends readonly ComponentType<any>[]> = {
33
- readonly [K in keyof T]: ComponentTypeToData<T[K]>;
34
- };
package/utils.d.ts DELETED
@@ -1,19 +0,0 @@
1
- /**
2
- * Utility functions for ECS library
3
- */
4
- /**
5
- * Get a value from cache or compute and cache it if not present
6
- * @param cache The cache map
7
- * @param key The cache key
8
- * @param compute Function to compute the value if not cached
9
- * @returns The cached or computed value
10
- */
11
- export declare function getOrComputeCache<K, V>(cache: Map<K, V>, key: K, compute: () => V): V;
12
- /**
13
- * Get a value from cache or create and cache it if not present, allowing side effects during creation
14
- * @param cache The cache map
15
- * @param key The cache key
16
- * @param create Function to create the value if not cached (can have side effects)
17
- * @returns The cached or created value
18
- */
19
- export declare function getOrCreateWithSideEffect<K, V>(cache: Map<K, V>, key: K, create: () => V): V;
package/world.d.ts DELETED
@@ -1,221 +0,0 @@
1
- import { Archetype } from "./archetype";
2
- import { ComponentChangeset } from "./changeset";
3
- import { type Command } from "./command-buffer";
4
- import type { EntityId, WildcardRelationId } from "./entity";
5
- import { Query } from "./query";
6
- import { type QueryFilter } from "./query-filter";
7
- import type { System } from "./system";
8
- import type { ComponentTuple, LifecycleHook } from "./types";
9
- /**
10
- * World class for ECS architecture
11
- * Manages entities, components, and systems
12
- */
13
- export declare class World<UpdateParams extends any[] = []> {
14
- /** Manages allocation and deallocation of entity IDs */
15
- private entityIdManager;
16
- /** Array of all archetypes in the world */
17
- private archetypes;
18
- /** Maps archetype signatures (component type signatures) to archetype instances */
19
- private archetypeBySignature;
20
- /** Maps entity IDs to their current archetype */
21
- private entityToArchetype;
22
- /** Maps component types to arrays of archetypes that contain them */
23
- private archetypesByComponent;
24
- /** Tracks which entities reference each entity as a component type */
25
- private entityReferences;
26
- /** Array of all active queries for archetype change notifications */
27
- private queries;
28
- /** Cache for queries keyed by component types and filter signatures */
29
- private queryCache;
30
- /** Schedules and executes systems in dependency order */
31
- private systemScheduler;
32
- /** Buffers structural changes for deferred execution */
33
- private commandBuffer;
34
- /** Stores lifecycle hooks for component and relation events */
35
- private hooks;
36
- /** Set of component IDs marked as exclusive relations */
37
- private exclusiveComponents;
38
- /** Set of component IDs that will cascade delete when the relation target is deleted */
39
- private cascadeDeleteComponents;
40
- /**
41
- * Create a new World.
42
- * If an optional snapshot object is provided (previously produced by `world.serialize()`),
43
- * the world will be restored from that snapshot. The snapshot may contain non-JSON values.
44
- */
45
- constructor(snapshot?: SerializedWorld);
46
- /**
47
- * Generate a signature string for component types array
48
- * @returns A string signature for the component types
49
- */
50
- private createArchetypeSignature;
51
- /**
52
- * Create a new entity
53
- * @returns The ID of the newly created entity
54
- */
55
- new(): EntityId;
56
- /**
57
- * Destroy an entity and remove all its components (immediate execution)
58
- */
59
- private destroyEntityImmediate;
60
- /**
61
- * Check if an entity exists
62
- */
63
- exists(entityId: EntityId): boolean;
64
- /**
65
- * Add a component to an entity (deferred)
66
- */
67
- set(entityId: EntityId, componentType: EntityId<void>): void;
68
- set<T>(entityId: EntityId, componentType: EntityId<T>, component: NoInfer<T>): void;
69
- /**
70
- * Remove a component from an entity (deferred)
71
- */
72
- remove<T>(entityId: EntityId, componentType: EntityId<T>): void;
73
- /**
74
- * Destroy an entity and remove all its components (deferred)
75
- */
76
- delete(entityId: EntityId): void;
77
- /**
78
- * Check if an entity has a specific component
79
- */
80
- has<T>(entityId: EntityId, componentType: EntityId<T>): boolean;
81
- /**
82
- * Get component data for a specific entity and wildcard relation type
83
- * Returns an array of all matching relation instances
84
- * @param entityId The entity
85
- * @param componentType The wildcard relation type
86
- * @returns Array of [targetEntityId, componentData] pairs for all matching relations
87
- */
88
- get<T>(entityId: EntityId, componentType: WildcardRelationId<T>): [EntityId<unknown>, T][];
89
- /**
90
- * Get component data for a specific entity and component type
91
- * @param entityId The entity
92
- * @param componentType The component type
93
- * @returns The component data
94
- */
95
- get<T>(entityId: EntityId, componentType: EntityId<T>): T;
96
- /**
97
- * Register a system with optional dependencies
98
- */
99
- registerSystem(system: System<UpdateParams>, additionalDeps?: System<UpdateParams>[]): void;
100
- /**
101
- * Register a lifecycle hook for component or wildcard relation events
102
- */
103
- hook<T>(componentType: EntityId<T>, hook: LifecycleHook<T>): void;
104
- /**
105
- * Unregister a lifecycle hook for component or wildcard relation events
106
- */
107
- unhook<T>(componentType: EntityId<T>, hook: LifecycleHook<T>): void;
108
- /**
109
- * Mark a component as exclusive relation
110
- * For exclusive relations, an entity can have at most one relation per base component
111
- */
112
- setExclusive(componentId: EntityId): void;
113
- /**
114
- * Mark a component as cascade-delete relation
115
- * For cascade relations, when the relation target entity is deleted,
116
- * the referencing entity will also be deleted (cascade).
117
- * Only applicable to entity-relation components
118
- */
119
- setCascadeDelete(componentId: EntityId): void;
120
- /**
121
- * Update the world (run all systems in dependency order)
122
- * This function is synchronous when all systems are synchronous,
123
- * and asynchronous (returns a Promise) when any system is asynchronous.
124
- */
125
- update(...params: UpdateParams): Promise<void> | void;
126
- /**
127
- * Execute all deferred commands immediately without running systems
128
- */
129
- sync(): void;
130
- /**
131
- * Create a cached query for efficient entity lookups
132
- * @returns A Query object for the specified component types and filter
133
- */
134
- createQuery(componentTypes: EntityId<any>[], filter?: QueryFilter): Query;
135
- /**
136
- * @internal Register a query for archetype update notifications
137
- */
138
- _registerQuery(query: Query): void;
139
- /**
140
- * @internal Unregister a query
141
- */
142
- _unregisterQuery(query: Query): void;
143
- /**
144
- * Release a query reference obtained from createQuery.
145
- * Decrements the refCount and fully disposes the query when it reaches zero.
146
- */
147
- releaseQuery(query: Query): void;
148
- /**
149
- * @internal Get archetypes that match specific component types (for internal use by queries)
150
- */
151
- getMatchingArchetypes(componentTypes: EntityId<any>[]): Archetype[];
152
- /**
153
- * Query entities with specific components
154
- * @returns Array of entity IDs that have all the specified components
155
- */
156
- query(componentTypes: EntityId<any>[]): EntityId[];
157
- query<const T extends readonly EntityId<any>[]>(componentTypes: T, includeComponents: true): Array<{
158
- entity: EntityId;
159
- components: ComponentTuple<T>;
160
- }>;
161
- /**
162
- * @internal Execute commands for a single entity (for internal use by CommandBuffer)
163
- * @returns ComponentChangeset describing the changes made
164
- */
165
- executeEntityCommands(entityId: EntityId, commands: Command[]): ComponentChangeset;
166
- /**
167
- * Get or create an archetype for the given component types
168
- * @returns The archetype for the given component types
169
- */
170
- private ensureArchetype;
171
- /**
172
- * Add a component reference to the reverse index when an entity is used as a component type
173
- * @param sourceEntityId The entity that has the component
174
- * @param componentType The component type (which may be an entity ID used as component type)
175
- * @param targetEntityId The entity being used as component type
176
- */
177
- private trackEntityReference;
178
- /**
179
- * Remove a component reference from the reverse index
180
- * @param sourceEntityId The entity that has the component
181
- * @param componentType The component type
182
- * @param targetEntityId The entity being used as component type
183
- */
184
- private untrackEntityReference;
185
- /**
186
- * Get all component references where a target entity is used as a component type
187
- * @param targetEntityId The target entity
188
- * @returns A MultiMap of sourceEntityId to componentTypes that reference the target entity
189
- */
190
- private getEntityReferences;
191
- /**
192
- * Remove an empty archetype from all internal data structures
193
- */
194
- private cleanupEmptyArchetype;
195
- /**
196
- * Execute component lifecycle hooks for added and removed components
197
- */
198
- private triggerLifecycleHooks;
199
- /**
200
- * Convert the world into a plain snapshot object.
201
- * This returns an in-memory structure and does not perform JSON stringification.
202
- * Component values are stored as-is (they may be non-JSON-serializable).
203
- */
204
- serialize(): SerializedWorld;
205
- }
206
- export type SerializedWorld = {
207
- version: number;
208
- entityManager: any;
209
- entities: SerializedEntity[];
210
- };
211
- export type SerializedEntity = {
212
- id: number;
213
- components: SerializedComponent[];
214
- };
215
- export type SerializedComponent = {
216
- type: number | string | {
217
- component: string;
218
- target: number | string;
219
- };
220
- value: any;
221
- };