@ste_tisci/ecs 0.1.0 → 0.1.2
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/.prettierrc +12 -0
- package/README.md +84 -36
- package/package.json +1 -1
- package/src/ComponentStore.ts +64 -0
- package/src/Ecs.ts +94 -0
- package/src/EntityManager.ts +58 -0
- package/src/index.ts +1 -0
- package/src/types/IComponentRegistry.d.ts +29 -0
- package/src/types/IComponentStore.d.ts +53 -0
- package/{dist/index.d.cts → src/types/IEcs.d.ts} +60 -100
- package/src/types/IEntityManager.d.ts +64 -0
- package/src/types/ISparseSet.d.ts +49 -0
- package/src/types/index.d.ts +42 -0
- package/src/utils/ComponentRegistry.ts +32 -0
- package/src/utils/SparseSet.ts +54 -0
- package/tsconfig.json +17 -0
- package/tsup.config.ts +10 -0
- package/dist/index.cjs +0 -247
- package/dist/index.d.ts +0 -103
- package/dist/index.js +0 -220
package/.prettierrc
ADDED
package/README.md
CHANGED
|
@@ -1,36 +1,84 @@
|
|
|
1
|
-
> [!IMPORTANT]
|
|
2
|
-
> Work in progress.
|
|
3
|
-
|
|
4
|
-
# ECS in TypeScript
|
|
5
|
-
|
|
6
|
-
A minimalistic game engine with zero dependencies based on the **Entity Component System (ECS)** architecture implemented in **TypeScript**
|
|
7
|
-
|
|
8
|
-
## Features
|
|
9
|
-
|
|
10
|
-
- **ECS architecture** with single responsability principles (SRP)
|
|
11
|
-
|
|
12
|
-
- **SparSet** for ultra-fast entities lookup
|
|
13
|
-
|
|
14
|
-
- **Structure of Arrays(SoA)** for optimized memory storage and access
|
|
15
|
-
|
|
16
|
-
- **Bitmask** for efficient component queries
|
|
17
|
-
|
|
18
|
-
## Core Components
|
|
19
|
-
|
|
20
|
-
- **EntityManager**: Manage creation, destruction and entity recycle. Uses a bitmask to track components of every entity.
|
|
21
|
-
|
|
22
|
-
- **ComponentStore**: Generic store that maintains components data in SoA format to maximise cache locality.
|
|
23
|
-
|
|
24
|
-
- **ECS**: Central Orchestator that coordinates entity and components, expose an API for queries and CRUD operations
|
|
25
|
-
|
|
26
|
-
- **SparseSet**: Data structure that grants O(1) for insertion, removal and lookups of entities.
|
|
27
|
-
|
|
28
|
-
## Technologies
|
|
29
|
-
|
|
30
|
-
- TypeScript for type safety
|
|
31
|
-
- Data oriented architecture for optimal performance
|
|
32
|
-
- Functional pattern without classes
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
1
|
+
> [!IMPORTANT]
|
|
2
|
+
> Work in progress.
|
|
3
|
+
|
|
4
|
+
# ECS in TypeScript
|
|
5
|
+
|
|
6
|
+
A minimalistic game engine with zero dependencies based on the **Entity Component System (ECS)** architecture implemented in **TypeScript**
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- **ECS architecture** with single responsability principles (SRP)
|
|
11
|
+
|
|
12
|
+
- **SparSet** for ultra-fast entities lookup
|
|
13
|
+
|
|
14
|
+
- **Structure of Arrays(SoA)** for optimized memory storage and access
|
|
15
|
+
|
|
16
|
+
- **Bitmask** for efficient component queries
|
|
17
|
+
|
|
18
|
+
## Core Components
|
|
19
|
+
|
|
20
|
+
- **EntityManager**: Manage creation, destruction and entity recycle. Uses a bitmask to track components of every entity.
|
|
21
|
+
|
|
22
|
+
- **ComponentStore**: Generic store that maintains components data in SoA format to maximise cache locality.
|
|
23
|
+
|
|
24
|
+
- **ECS**: Central Orchestator that coordinates entity and components, expose an API for queries and CRUD operations
|
|
25
|
+
|
|
26
|
+
- **SparseSet**: Data structure that grants O(1) for insertion, removal and lookups of entities.
|
|
27
|
+
|
|
28
|
+
## Technologies
|
|
29
|
+
|
|
30
|
+
- TypeScript for type safety
|
|
31
|
+
- Data oriented architecture for optimal performance
|
|
32
|
+
- Functional pattern without classes
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import { ECS } from '@ste_tisci/ecs';
|
|
36
|
+
|
|
37
|
+
// Define the type of the components
|
|
38
|
+
const World = ECS<{
|
|
39
|
+
Position: { x: number; y: number };
|
|
40
|
+
Velocity: { x: number; y: number };
|
|
41
|
+
Size: { width: number; height: number };
|
|
42
|
+
Sprite: { src: HTMLImageElement };
|
|
43
|
+
}>();
|
|
44
|
+
|
|
45
|
+
// Define the structure of components that will be converted in SoA Arrays with the types previously defined
|
|
46
|
+
// Position = { x: number[], y: number[] }
|
|
47
|
+
// Size = { src: HTMLImageElement[] }
|
|
48
|
+
// etc.
|
|
49
|
+
World.defineComponents({
|
|
50
|
+
Position: {},
|
|
51
|
+
Size: {},
|
|
52
|
+
Velocity: {},
|
|
53
|
+
Sprite: {},
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const ent = World.createEntity();
|
|
57
|
+
|
|
58
|
+
World.addComponent(ent, 'Position', { x: 100, y: 20 });
|
|
59
|
+
World.addComponent(ent, 'Velocity', { x: 2, y: 1 });
|
|
60
|
+
World.addComponent(ent, 'Size', { width: 20, height: 20 });
|
|
61
|
+
|
|
62
|
+
function movementSystem(World) {
|
|
63
|
+
const { Position, Velocity } = World.components;
|
|
64
|
+
|
|
65
|
+
for (const entity of World.query('Position', 'Velocity')) {
|
|
66
|
+
const posID = entity.Position.id;
|
|
67
|
+
const velID = entity.Velocity.id;
|
|
68
|
+
|
|
69
|
+
Position.x[posID] += Velocity.x[velID];
|
|
70
|
+
Position.y[posID] += Velocity.y[velID];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function gameLoop() {
|
|
75
|
+
movementSystem(World);
|
|
76
|
+
requestAnimationFrame(gameLoop);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
gameLoop();
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## ⚖️ License
|
|
83
|
+
|
|
84
|
+
MIT © 2025 SteTisci
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ste_tisci/ecs",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "A minimalistic game engine with zero dependencies based on the Entity Component System (ECS) architecture implemented in TypeScript",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { IComponentStore } from './types/IComponentStore.js';
|
|
2
|
+
import type { ComponentDataArrays } from './types/index.js';
|
|
3
|
+
import { SparseSet } from './utils/SparseSet.js';
|
|
4
|
+
|
|
5
|
+
export function ComponentStore<T extends Record<string, any>>(): IComponentStore<T> {
|
|
6
|
+
const componentSet = SparseSet();
|
|
7
|
+
const componentData = {} as ComponentDataArrays<T>;
|
|
8
|
+
|
|
9
|
+
function add(eid: number, data: T): void {
|
|
10
|
+
if (componentSet.has(eid)) throw new Error(`Entity ${eid} already has this component`);
|
|
11
|
+
|
|
12
|
+
// Use the current size (next index) to maintain data coherence with the dense array
|
|
13
|
+
const ID = componentSet.getSize();
|
|
14
|
+
|
|
15
|
+
componentSet.add(eid);
|
|
16
|
+
|
|
17
|
+
// Store each property of the component into its corresponding array
|
|
18
|
+
for (const key in data) {
|
|
19
|
+
if (!componentData[key]) {
|
|
20
|
+
componentData[key] = [] as T[typeof key][];
|
|
21
|
+
}
|
|
22
|
+
componentData[key][ID] = data[key];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function remove(eid: number): void {
|
|
27
|
+
if (!componentSet.has(eid)) throw new Error(`Entity ${eid} does not have this component`);
|
|
28
|
+
|
|
29
|
+
const ID = componentSet.getIndex(eid);
|
|
30
|
+
const lastID = componentSet.getSize() - 1;
|
|
31
|
+
|
|
32
|
+
// Swap the component data only if is not the last one inserted
|
|
33
|
+
if (ID !== lastID) {
|
|
34
|
+
for (const key of Object.keys(componentData)) {
|
|
35
|
+
componentData[key][ID] = componentData[key][lastID];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
componentSet.remove(eid);
|
|
40
|
+
|
|
41
|
+
// Keep component arrays dense and in sync with the SparseSet's dense array
|
|
42
|
+
for (const key in componentData) {
|
|
43
|
+
componentData[key].pop();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function getDense(): number[] {
|
|
48
|
+
return componentSet.getDense();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getData(): ComponentDataArrays<T> {
|
|
52
|
+
return componentData;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function getIndex(eid: number): number {
|
|
56
|
+
return componentSet.getIndex(eid);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function getSize(): number {
|
|
60
|
+
return componentSet.getSize();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return { add, remove, getIndex, getDense, getData, getSize };
|
|
64
|
+
}
|
package/src/Ecs.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { QueryResult, StoreDataMap, StoreMap } from './types/index.js';
|
|
2
|
+
import type { IECS } from './types/IEcs.js';
|
|
3
|
+
import { ComponentStore } from './ComponentStore.js';
|
|
4
|
+
import { EntityManager } from './EntityManager.js';
|
|
5
|
+
import { createComponentRegistry } from './utils/ComponentRegistry.js';
|
|
6
|
+
|
|
7
|
+
export function ECS<T extends Record<string, Record<string, any>>>(): IECS<T> {
|
|
8
|
+
const ComponentRegistry = createComponentRegistry<keyof T & string>();
|
|
9
|
+
const Entities = EntityManager(ComponentRegistry);
|
|
10
|
+
const internalStores = {} as StoreMap<T>;
|
|
11
|
+
const components = {} as StoreDataMap<T>;
|
|
12
|
+
|
|
13
|
+
// Initialize the structure of the components used
|
|
14
|
+
function defineComponents<K extends readonly (keyof T)[]>(...names: K): void {
|
|
15
|
+
for (const name of names) {
|
|
16
|
+
ComponentRegistry.register(name as string);
|
|
17
|
+
internalStores[name] = ComponentStore<T[typeof name]>();
|
|
18
|
+
components[name] = internalStores[name].getData();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function createEntity(): number {
|
|
23
|
+
return Entities.create();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function destroyEntity(eid: number): void {
|
|
27
|
+
const entityMask = Entities.getMask(eid);
|
|
28
|
+
|
|
29
|
+
let cid = 0;
|
|
30
|
+
let tempMask = entityMask;
|
|
31
|
+
|
|
32
|
+
// Bit operation on the entity mask to track the corresponding components and remove them
|
|
33
|
+
while (tempMask !== 0n) {
|
|
34
|
+
if ((tempMask & 1n) !== 0n) {
|
|
35
|
+
const name = ComponentRegistry.getName(cid);
|
|
36
|
+
internalStores[name].remove(eid);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
tempMask >>= 1n;
|
|
40
|
+
cid++;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
Entities.remove(eid);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function addComponent<K extends keyof T>(eid: number, name: K, data: T[K]): void {
|
|
47
|
+
Entities.addComponent(eid, name as keyof T & string);
|
|
48
|
+
internalStores[name].add(eid, data);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function removeComponent<K extends keyof T>(eid: number, name: K): void {
|
|
52
|
+
Entities.removeComponent(eid, name as keyof T & string);
|
|
53
|
+
internalStores[name].remove(eid);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Generator function to retrive the indexes of the entity components searched
|
|
57
|
+
function* query<C extends (keyof T)[]>(...componentName: C): Generator<QueryResult<T, C>> {
|
|
58
|
+
if (componentName.length === 0) return;
|
|
59
|
+
|
|
60
|
+
let smallestStore = internalStores[componentName[0]];
|
|
61
|
+
let smallestSize = smallestStore.getSize();
|
|
62
|
+
|
|
63
|
+
for (let i = 1; i < componentName.length; i++) {
|
|
64
|
+
const store = internalStores[componentName[i]];
|
|
65
|
+
const size = store.getSize();
|
|
66
|
+
|
|
67
|
+
if (size < smallestSize) {
|
|
68
|
+
smallestStore = store;
|
|
69
|
+
smallestSize = size;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let targetMask = 0n;
|
|
74
|
+
for (const name of componentName) {
|
|
75
|
+
targetMask |= 1n << BigInt(ComponentRegistry.getID(name as keyof T & string));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
for (const eid of smallestStore.getDense()) {
|
|
79
|
+
if ((Entities.getMask(eid) & targetMask) !== targetMask) continue;
|
|
80
|
+
|
|
81
|
+
const result = {} as any;
|
|
82
|
+
|
|
83
|
+
for (const name of componentName) {
|
|
84
|
+
const id = internalStores[name].getIndex(eid);
|
|
85
|
+
|
|
86
|
+
result[name] = { id };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
yield result as QueryResult<T, C>;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return { defineComponents, createEntity, destroyEntity, addComponent, removeComponent, query, components };
|
|
94
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { IComponentRegistry } from './types/IComponentRegistry.js';
|
|
2
|
+
import type { IEntityManager } from './types/IEntityManager.js';
|
|
3
|
+
|
|
4
|
+
export function EntityManager<K extends string>(registry: IComponentRegistry<K>): IEntityManager<K> {
|
|
5
|
+
let nextID: number = 0;
|
|
6
|
+
const recycledIDs: number[] = [];
|
|
7
|
+
const bitMasks: bigint[] = [];
|
|
8
|
+
|
|
9
|
+
function exists(eid: number): boolean {
|
|
10
|
+
const isWithinBounds = eid >= 0 && eid < nextID;
|
|
11
|
+
const hasMask = bitMasks[eid] !== undefined;
|
|
12
|
+
|
|
13
|
+
return isWithinBounds && hasMask;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function hasComponent(eid: number, name: K): boolean {
|
|
17
|
+
if (!exists(eid)) throw new Error(`Entity ${eid} does not exist`);
|
|
18
|
+
|
|
19
|
+
const cid = registry.getID(name);
|
|
20
|
+
return (bitMasks[eid] & (1n << BigInt(cid))) !== 0n;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function create(): number {
|
|
24
|
+
const ID = recycledIDs.length > 0 ? recycledIDs.pop()! : nextID++;
|
|
25
|
+
bitMasks[ID] = 0n;
|
|
26
|
+
|
|
27
|
+
return ID;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function remove(eid: number): void {
|
|
31
|
+
if (!exists(eid)) throw new Error(`Entity ${eid} does not exist`);
|
|
32
|
+
|
|
33
|
+
bitMasks[eid] = undefined as any;
|
|
34
|
+
recycledIDs.push(eid);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function addComponent(eid: number, name: K): void {
|
|
38
|
+
if (!exists(eid)) throw new Error(`Entity ${eid} does not exists`);
|
|
39
|
+
if (hasComponent(eid, name)) throw new Error(`Entity ${eid} already has component ${name}`);
|
|
40
|
+
|
|
41
|
+
const cid = registry.getID(name);
|
|
42
|
+
bitMasks[eid] |= 1n << BigInt(cid);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function removeComponent(eid: number, name: K): void {
|
|
46
|
+
if (!exists(eid)) throw new Error(`Entity ${eid} does not exists`);
|
|
47
|
+
if (!hasComponent(eid, name)) throw new Error(`Entity ${eid} does not have component ${name}`);
|
|
48
|
+
|
|
49
|
+
const cid = registry.getID(name);
|
|
50
|
+
bitMasks[eid] &= ~(1n << BigInt(cid));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function getMask(eid: number): bigint {
|
|
54
|
+
return bitMasks[eid];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return { exists, hasComponent, create, remove, addComponent, removeComponent, getMask };
|
|
58
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ECS } from './Ecs.js';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for managing component registration and ID mapping.
|
|
3
|
+
* Provides a centralized registry that maps component names to unique IDs
|
|
4
|
+
* and vice versa, used for efficient bitmask operations in the ECS.
|
|
5
|
+
*/
|
|
6
|
+
export interface IComponentRegistry<K extends string> {
|
|
7
|
+
/**
|
|
8
|
+
* Registers a new component by name.
|
|
9
|
+
* Assigns a unique incremental ID to the component if not already registered.
|
|
10
|
+
* @param name - The name of the component to register
|
|
11
|
+
*/
|
|
12
|
+
register: (name: K) => void;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Retrieves the unique ID associated with a component name.
|
|
16
|
+
* @param name - The name of the component
|
|
17
|
+
* @returns The unique ID assigned to the component
|
|
18
|
+
* @throws Error if the component is not registered
|
|
19
|
+
*/
|
|
20
|
+
getID: (name: K) => number;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Retrieves the component name associated with a given ID.
|
|
24
|
+
* @param cid - The unique ID of the component
|
|
25
|
+
* @returns The name of the component
|
|
26
|
+
* @throws Error if the ID is not registered
|
|
27
|
+
*/
|
|
28
|
+
getName: (cid: number) => K;
|
|
29
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { ComponentDataArrays } from './index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Interface for storing and managing component data using Structure of Arrays (SoA) pattern.
|
|
5
|
+
* Each component type has its own store that maintains entity-component associations
|
|
6
|
+
* and stores component data in separate arrays per property for cache efficiency.
|
|
7
|
+
* @template T - The component data type (object with properties)
|
|
8
|
+
*/
|
|
9
|
+
export interface IComponentStore<T> {
|
|
10
|
+
/**
|
|
11
|
+
* Adds a component to an entity with the provided data.
|
|
12
|
+
* Stores each property of the component in separate arrays for efficient iteration.
|
|
13
|
+
* @param eid - The entity ID to add the component to
|
|
14
|
+
* @param data - The component data to store
|
|
15
|
+
* @throws Error if the entity already has this component
|
|
16
|
+
*/
|
|
17
|
+
add: (eid: number, data: T) => void;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Removes a component from an entity.
|
|
21
|
+
* Uses swap-and-pop technique to maintain dense arrays.
|
|
22
|
+
* @param eid - The entity ID to remove the component from
|
|
23
|
+
* @throws Error if the entity doesn't have this component
|
|
24
|
+
*/
|
|
25
|
+
remove: (eid: number) => void;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Gets the index of an entity's component data in the dense arrays.
|
|
29
|
+
* @param eid - The entity ID
|
|
30
|
+
* @returns The index in the data arrays where this entity's component data is stored
|
|
31
|
+
*/
|
|
32
|
+
getIndex: (eid: number) => number;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Returns the dense array of entity IDs that have this component.
|
|
36
|
+
* Useful for efficient iteration over all entities with this component.
|
|
37
|
+
* @returns Array of entity IDs
|
|
38
|
+
*/
|
|
39
|
+
getDense: () => number[];
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Returns the component data arrays organized by property.
|
|
43
|
+
* Each property of the component type has its own array.
|
|
44
|
+
* @returns Object mapping property names to their value arrays
|
|
45
|
+
*/
|
|
46
|
+
getData: () => ComponentDataArrays<T>;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Returns the number of entities that have this component.
|
|
50
|
+
* @returns The count of entities with this component
|
|
51
|
+
*/
|
|
52
|
+
getSize: () => number;
|
|
53
|
+
}
|
|
@@ -1,103 +1,63 @@
|
|
|
1
|
-
|
|
2
|
-
* Represents component data in Structure of Arrays (SoA) format.
|
|
3
|
-
* Each property of the component is stored in a separate array,
|
|
4
|
-
* enabling cache-efficient iteration and SIMD operations.
|
|
5
|
-
* @template T - The component data type
|
|
6
|
-
* @example
|
|
7
|
-
* // For Position component: { x: number, y: number }
|
|
8
|
-
* // ComponentDataArrays would be: { x: number[], y: number[] }
|
|
9
|
-
*/
|
|
10
|
-
type ComponentDataArrays<T> = {
|
|
11
|
-
[K in keyof T]: T[K][];
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Maps component names to their data arrays for direct access.
|
|
16
|
-
* Used to expose component data to the user without store methods.
|
|
17
|
-
* @template T - Record type defining all component types
|
|
18
|
-
*/
|
|
19
|
-
type StoreDataMap<T> = {
|
|
20
|
-
[K in keyof T]: ComponentDataArrays<T[K]>;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Type helper for the query result.
|
|
25
|
-
* Creates an object with eid and all required components typed correctly.
|
|
26
|
-
* @template T - Record type with all component types.
|
|
27
|
-
* @template C - Array of the component names requested in the query.
|
|
28
|
-
*/
|
|
29
|
-
type QueryResult<T, C extends (keyof T)[]> = {
|
|
30
|
-
[K in C[number]]: { id: number };
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
type StoreInit<T> = {
|
|
34
|
-
[K in keyof T]: {
|
|
35
|
-
[P in keyof T[K]]: T[K][P][];
|
|
36
|
-
};
|
|
37
|
-
};
|
|
1
|
+
import type { QueryResult, StoreDataMap } from './index.js';
|
|
38
2
|
|
|
39
|
-
/**
|
|
40
|
-
* Interface for the main ECS registry.
|
|
41
|
-
* Provides high-level API for entity-component management,
|
|
42
|
-
* coordinating between EntityManager and ComponentStores.
|
|
43
|
-
* @template T - Record type defining all component types in the ECS
|
|
44
|
-
*/
|
|
45
|
-
interface IECS<T> {
|
|
46
|
-
/**
|
|
47
|
-
* Initializes the ECS with component definitions.
|
|
48
|
-
* Registers all components and creates their corresponding stores.
|
|
49
|
-
* Must be called before any other operations.
|
|
50
|
-
* @param
|
|
51
|
-
*/
|
|
52
|
-
defineComponents
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Creates a new entity in the ECS.
|
|
56
|
-
* @returns The ID of the newly created entity
|
|
57
|
-
*/
|
|
58
|
-
createEntity: () => number;
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Completely removes an entity and all its components from the ECS.
|
|
62
|
-
* Cleans up all component data and entity records.
|
|
63
|
-
* @param eid - The entity ID to destroy
|
|
64
|
-
*/
|
|
65
|
-
destroyEntity: (eid: number) => void;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Adds a component to an entity with the provided data.
|
|
69
|
-
* Updates both the entity's bitmask and the component store.
|
|
70
|
-
* @template K - The component name type (key of T)
|
|
71
|
-
* @param eid - The entity ID
|
|
72
|
-
* @param name - The component name
|
|
73
|
-
* @param data - The component data to store
|
|
74
|
-
*/
|
|
75
|
-
addComponent: <K extends keyof T>(eid: number, name: K, data: T[K]) => void;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Removes a component from an entity.
|
|
79
|
-
* Updates both the entity's bitmask and the component store.
|
|
80
|
-
* @template K - The component name type (key of T)
|
|
81
|
-
* @param eid - The entity ID
|
|
82
|
-
* @param name - The component name
|
|
83
|
-
*/
|
|
84
|
-
removeComponent: <K extends keyof T>(eid: number, name: K) => void;
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Retrieves all entities that contain the specified set of components.
|
|
88
|
-
* Performs a filtered search across component stores and yields each matching entity.
|
|
89
|
-
* The returned generator allows efficient iteration without allocating large arrays.
|
|
90
|
-
*
|
|
91
|
-
* @template C - Array of component names to query for
|
|
92
|
-
* @param components - List of component names that the entity must include
|
|
93
|
-
* @returns A generator that yields an object for each matching entity,
|
|
94
|
-
* containing the entity ID and the related component data
|
|
95
|
-
*/
|
|
96
|
-
query: <C extends (keyof T)[]>(...componentName: C) => Generator<QueryResult<T, C>>;
|
|
97
|
-
|
|
98
|
-
components: StoreDataMap<T>;
|
|
99
|
-
}
|
|
3
|
+
/**
|
|
4
|
+
* Interface for the main ECS registry.
|
|
5
|
+
* Provides high-level API for entity-component management,
|
|
6
|
+
* coordinating between EntityManager and ComponentStores.
|
|
7
|
+
* @template T - Record type defining all component types in the ECS
|
|
8
|
+
*/
|
|
9
|
+
export interface IECS<T> {
|
|
10
|
+
/**
|
|
11
|
+
* Initializes the ECS with component definitions.
|
|
12
|
+
* Registers all components and creates their corresponding stores.
|
|
13
|
+
* Must be called before any other operations.
|
|
14
|
+
* @param names -component names
|
|
15
|
+
*/
|
|
16
|
+
defineComponents<K extends readonly (keyof T)[]>(...names: K): void;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Creates a new entity in the ECS.
|
|
20
|
+
* @returns The ID of the newly created entity
|
|
21
|
+
*/
|
|
22
|
+
createEntity: () => number;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Completely removes an entity and all its components from the ECS.
|
|
26
|
+
* Cleans up all component data and entity records.
|
|
27
|
+
* @param eid - The entity ID to destroy
|
|
28
|
+
*/
|
|
29
|
+
destroyEntity: (eid: number) => void;
|
|
100
30
|
|
|
101
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Adds a component to an entity with the provided data.
|
|
33
|
+
* Updates both the entity's bitmask and the component store.
|
|
34
|
+
* @template K - The component name type (key of T)
|
|
35
|
+
* @param eid - The entity ID
|
|
36
|
+
* @param name - The component name
|
|
37
|
+
* @param data - The component data to store
|
|
38
|
+
*/
|
|
39
|
+
addComponent: <K extends keyof T>(eid: number, name: K, data: T[K]) => void;
|
|
102
40
|
|
|
103
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Removes a component from an entity.
|
|
43
|
+
* Updates both the entity's bitmask and the component store.
|
|
44
|
+
* @template K - The component name type (key of T)
|
|
45
|
+
* @param eid - The entity ID
|
|
46
|
+
* @param name - The component name
|
|
47
|
+
*/
|
|
48
|
+
removeComponent: <K extends keyof T>(eid: number, name: K) => void;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Retrieves all entities that contain the specified set of components.
|
|
52
|
+
* Performs a filtered search across component stores and yields each matching entity.
|
|
53
|
+
* The returned generator allows efficient iteration without allocating large arrays.
|
|
54
|
+
*
|
|
55
|
+
* @template C - Array of component names to query for
|
|
56
|
+
* @param components - List of component names that the entity must include
|
|
57
|
+
* @returns A generator that yields an object for each matching entity,
|
|
58
|
+
* containing the entity ID and the related component data
|
|
59
|
+
*/
|
|
60
|
+
query: <C extends (keyof T)[]>(...componentName: C) => Generator<QueryResult<T, C>>;
|
|
61
|
+
|
|
62
|
+
components: StoreDataMap<T>;
|
|
63
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for managing entities and their component associations.
|
|
3
|
+
* Uses bitmasks to efficiently track which components each entity has,
|
|
4
|
+
* enabling fast component checks and queries.
|
|
5
|
+
*/
|
|
6
|
+
export interface IEntityManager<K extends string> {
|
|
7
|
+
/**
|
|
8
|
+
* Checks if an entity exists in the system.
|
|
9
|
+
* @param eid - The entity ID to check
|
|
10
|
+
* @returns True if the entity exists, false otherwise
|
|
11
|
+
*/
|
|
12
|
+
exists: (eid: number) => boolean;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Checks if an entity has a specific component.
|
|
16
|
+
* Uses bitwise operations on the entity's bitmask for fast lookups.
|
|
17
|
+
* @param eid - The entity ID
|
|
18
|
+
* @param name - The component name
|
|
19
|
+
* @returns True if the entity has the component, false otherwise
|
|
20
|
+
*/
|
|
21
|
+
hasComponent: (eid: number, name: K) => boolean;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Creates a new entity.
|
|
25
|
+
* Recycles entity IDs when possible, otherwise assigns a new incremental ID.
|
|
26
|
+
* @returns The ID of the newly created entity
|
|
27
|
+
*/
|
|
28
|
+
create: () => number;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Removes an entity from the system.
|
|
32
|
+
* Marks the entity ID for recycling.
|
|
33
|
+
* @param eid - The entity ID to remove
|
|
34
|
+
* @throws Error if the entity doesn't exist
|
|
35
|
+
*/
|
|
36
|
+
remove: (eid: number) => void;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Adds a component flag to an entity's bitmask.
|
|
40
|
+
* Updates the entity's bitmask to indicate it has the specified component.
|
|
41
|
+
* @param eid - The entity ID
|
|
42
|
+
* @param name - The component name
|
|
43
|
+
* @throws Error if the entity doesn't exist or already has the component
|
|
44
|
+
*/
|
|
45
|
+
addComponent: (eid: number, name: K) => void;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Removes a component flag from an entity's bitmask.
|
|
49
|
+
* Updates the entity's bitmask to indicate it no longer has the specified component.
|
|
50
|
+
* @param eid - The entity ID
|
|
51
|
+
* @param name - The component name
|
|
52
|
+
* @throws Error if the entity doesn't exist
|
|
53
|
+
*/
|
|
54
|
+
removeComponent: (eid: number, name: K) => void;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Retrieves the complete bitmask for an entity.
|
|
58
|
+
* The bitmask represents all components the entity has,
|
|
59
|
+
* with each bit corresponding to a component ID.
|
|
60
|
+
* @param eid - The entity ID
|
|
61
|
+
* @returns The entity's component bitmask
|
|
62
|
+
*/
|
|
63
|
+
getMask: (eid: number) => bigint;
|
|
64
|
+
}
|