@codehz/ecs 0.0.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/LICENSE +21 -0
- package/README.md +131 -0
- package/archetype.d.ts +107 -0
- package/command-buffer.d.ts +45 -0
- package/entity.d.ts +141 -0
- package/index.d.ts +5 -0
- package/index.js +756 -0
- package/package.json +16 -0
- package/query-filter.d.ts +16 -0
- package/query.d.ts +62 -0
- package/system.d.ts +10 -0
- package/types.d.ts +7 -0
- package/utils.d.ts +19 -0
- package/world.d.ts +106 -0
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@codehz/ecs",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./index.js",
|
|
6
|
+
"types": "./index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./index.d.ts",
|
|
10
|
+
"import": "./index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"typescript": "~5.9.2"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
* Check if an archetype matches the given component types
|
|
11
|
+
*/
|
|
12
|
+
export declare function matchesComponentTypes(archetype: Archetype, componentTypes: EntityId<any>[]): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Check if an archetype matches the filter conditions (only filtering logic)
|
|
15
|
+
*/
|
|
16
|
+
export declare function matchesFilter(archetype: Archetype, filter: QueryFilter): boolean;
|
package/query.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Archetype } from "./archetype";
|
|
2
|
+
import type { EntityId } from "./entity";
|
|
3
|
+
import type { ComponentTuple } from "./types";
|
|
4
|
+
import { type QueryFilter } from "./query-filter";
|
|
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 EntityId<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 EntityId<any>[]>(componentTypes: T, callback: (entity: EntityId, ...components: ComponentTuple<T>) => void): void;
|
|
35
|
+
/**
|
|
36
|
+
* Get component data arrays for all matching entities
|
|
37
|
+
* @param componentType The component type to retrieve
|
|
38
|
+
* @returns Array of component data for all matching entities
|
|
39
|
+
*/
|
|
40
|
+
getComponentData<T>(componentType: EntityId<T>): T[];
|
|
41
|
+
/**
|
|
42
|
+
* Update the cached archetypes
|
|
43
|
+
* Called when new archetypes are created
|
|
44
|
+
*/
|
|
45
|
+
updateCache(): void;
|
|
46
|
+
/**
|
|
47
|
+
* Check if a new archetype matches this query and add to cache if it does
|
|
48
|
+
*/
|
|
49
|
+
checkNewArchetype(archetype: Archetype): void;
|
|
50
|
+
/**
|
|
51
|
+
* Dispose the query and disconnect from world
|
|
52
|
+
*/
|
|
53
|
+
dispose(): void;
|
|
54
|
+
/**
|
|
55
|
+
* Symbol.dispose implementation for automatic resource management
|
|
56
|
+
*/
|
|
57
|
+
[Symbol.dispose](): void;
|
|
58
|
+
/**
|
|
59
|
+
* Check if the query has been disposed
|
|
60
|
+
*/
|
|
61
|
+
get disposed(): boolean;
|
|
62
|
+
}
|
package/system.d.ts
ADDED
package/types.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { EntityId, WildcardRelationId } from "./entity";
|
|
2
|
+
/**
|
|
3
|
+
* Type helper for component tuples extracted from EntityId array
|
|
4
|
+
*/
|
|
5
|
+
export type ComponentTuple<T extends readonly EntityId<any>[]> = {
|
|
6
|
+
readonly [K in keyof T]: T[K] extends WildcardRelationId<infer U> ? [EntityId<unknown>, U][] : T[K] extends EntityId<infer U> ? U : never;
|
|
7
|
+
};
|
package/utils.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Archetype } from "./archetype";
|
|
2
|
+
import { type Command } from "./command-buffer";
|
|
3
|
+
import type { EntityId } from "./entity";
|
|
4
|
+
import { Query } from "./query";
|
|
5
|
+
import type { QueryFilter } from "./query-filter";
|
|
6
|
+
import type { ComponentTuple } from "./types";
|
|
7
|
+
import type { System } from "./system";
|
|
8
|
+
/**
|
|
9
|
+
* World class for ECS architecture
|
|
10
|
+
* Manages entities, components, and systems
|
|
11
|
+
*/
|
|
12
|
+
export declare class World<ExtraParams extends any[] = [deltaTime: number]> {
|
|
13
|
+
private entityIdManager;
|
|
14
|
+
private archetypes;
|
|
15
|
+
private archetypeMap;
|
|
16
|
+
private entityToArchetype;
|
|
17
|
+
private systems;
|
|
18
|
+
private queries;
|
|
19
|
+
private commandBuffer;
|
|
20
|
+
private componentToArchetypes;
|
|
21
|
+
constructor();
|
|
22
|
+
/**
|
|
23
|
+
* Generate a hash key for component types array
|
|
24
|
+
*/
|
|
25
|
+
private getComponentTypesHash;
|
|
26
|
+
/**
|
|
27
|
+
* Create a new entity
|
|
28
|
+
*/
|
|
29
|
+
createEntity(): EntityId;
|
|
30
|
+
/**
|
|
31
|
+
* Destroy an entity and remove all its components (immediate execution)
|
|
32
|
+
*/
|
|
33
|
+
private _destroyEntity;
|
|
34
|
+
/**
|
|
35
|
+
* Check if an entity exists
|
|
36
|
+
*/
|
|
37
|
+
hasEntity(entityId: EntityId): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Add a component to an entity (deferred)
|
|
40
|
+
*/
|
|
41
|
+
addComponent<T>(entityId: EntityId, componentType: EntityId<T>, component: T): void;
|
|
42
|
+
/**
|
|
43
|
+
* Remove a component from an entity (deferred)
|
|
44
|
+
*/
|
|
45
|
+
removeComponent<T>(entityId: EntityId, componentType: EntityId<T>): void;
|
|
46
|
+
/**
|
|
47
|
+
* Destroy an entity and remove all its components (deferred)
|
|
48
|
+
*/
|
|
49
|
+
destroyEntity(entityId: EntityId): void;
|
|
50
|
+
/**
|
|
51
|
+
* Check if an entity has a specific component
|
|
52
|
+
*/
|
|
53
|
+
hasComponent<T>(entityId: EntityId, componentType: EntityId<T>): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Get a component from an entity
|
|
56
|
+
*/
|
|
57
|
+
getComponent<T>(entityId: EntityId, componentType: EntityId<T>): T | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Register a system
|
|
60
|
+
*/
|
|
61
|
+
registerSystem(system: System<ExtraParams>): void;
|
|
62
|
+
/**
|
|
63
|
+
* Unregister a system
|
|
64
|
+
*/
|
|
65
|
+
unregisterSystem(system: System<ExtraParams>): void;
|
|
66
|
+
/**
|
|
67
|
+
* Update the world (run all systems)
|
|
68
|
+
*/
|
|
69
|
+
update(...params: ExtraParams): void;
|
|
70
|
+
/**
|
|
71
|
+
* Execute all deferred commands immediately without running systems
|
|
72
|
+
*/
|
|
73
|
+
flushCommands(): void;
|
|
74
|
+
/**
|
|
75
|
+
* Create a cached query for efficient entity lookups
|
|
76
|
+
*/
|
|
77
|
+
createQuery(componentTypes: EntityId<any>[], filter?: QueryFilter): Query;
|
|
78
|
+
/**
|
|
79
|
+
* @internal Register a query for archetype update notifications
|
|
80
|
+
*/
|
|
81
|
+
registerQuery(query: Query): void;
|
|
82
|
+
/**
|
|
83
|
+
* @internal Unregister a query
|
|
84
|
+
*/
|
|
85
|
+
unregisterQuery(query: Query): void;
|
|
86
|
+
/**
|
|
87
|
+
* @internal Get archetypes that match specific component types (for internal use by queries)
|
|
88
|
+
*/
|
|
89
|
+
getMatchingArchetypes(componentTypes: EntityId<any>[]): Archetype[];
|
|
90
|
+
/**
|
|
91
|
+
* Query entities with specific components
|
|
92
|
+
*/
|
|
93
|
+
queryEntities(componentTypes: EntityId<any>[]): EntityId[];
|
|
94
|
+
queryEntities<const T extends readonly EntityId<any>[]>(componentTypes: T, includeComponents: true): Array<{
|
|
95
|
+
entity: EntityId;
|
|
96
|
+
components: ComponentTuple<T>;
|
|
97
|
+
}>;
|
|
98
|
+
/**
|
|
99
|
+
* @internal Execute commands for a single entity (for internal use by CommandBuffer)
|
|
100
|
+
*/
|
|
101
|
+
executeEntityCommands(entityId: EntityId, commands: Command[]): void;
|
|
102
|
+
/**
|
|
103
|
+
* Get or create an archetype for the given component types
|
|
104
|
+
*/
|
|
105
|
+
private getOrCreateArchetype;
|
|
106
|
+
}
|