@ste_tisci/ecs 0.1.6 → 0.1.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/README.md +33 -0
- package/dist/index.cjs +309 -0
- package/dist/index.d.cts +142 -0
- package/dist/index.d.ts +142 -0
- package/dist/index.js +282 -0
- package/package.json +6 -2
- package/.prettierrc +0 -12
- package/src/ComponentStore.ts +0 -96
- package/src/Ecs.ts +0 -98
- package/src/EntityManager.ts +0 -60
- package/src/index.ts +0 -1
- package/src/types/IComponentRegistry.d.ts +0 -29
- package/src/types/IComponentStore.d.ts +0 -53
- package/src/types/IEcs.d.ts +0 -64
- package/src/types/IEntityManager.d.ts +0 -65
- package/src/types/ISparseSet.d.ts +0 -49
- package/src/types/index.d.ts +0 -43
- package/src/utils/ComponentRegistry.ts +0 -32
- package/src/utils/SparseSet.ts +0 -54
- package/tsconfig.json +0 -17
- package/tsup.config.ts +0 -10
package/src/types/IEcs.d.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import type { QueryResult, StoreDataMap } from './index.js';
|
|
2
|
-
|
|
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
|
-
* @throws Error if a component name is already defined
|
|
16
|
-
*/
|
|
17
|
-
defineComponents<K extends readonly (keyof T)[]>(...names: K): void;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Creates a new entity in the ECS.
|
|
21
|
-
* @returns The ID of the newly created entity
|
|
22
|
-
*/
|
|
23
|
-
createEntity: () => number;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Completely removes an entity and all its components from the ECS.
|
|
27
|
-
* Cleans up all component data and entity records.
|
|
28
|
-
* @param eid - The entity ID to destroy
|
|
29
|
-
*/
|
|
30
|
-
destroyEntity: (eid: number) => void;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Adds a component to an entity with the provided data.
|
|
34
|
-
* Updates both the entity's bitmask and the component store.
|
|
35
|
-
* @template K - The component name type (key of T)
|
|
36
|
-
* @param eid - The entity ID
|
|
37
|
-
* @param name - The component name
|
|
38
|
-
* @param data - The component data to store
|
|
39
|
-
*/
|
|
40
|
-
addComponent: <K extends keyof T>(eid: number, name: K, data: T[K]) => void;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Removes a component from an entity.
|
|
44
|
-
* Updates both the entity's bitmask and the component store.
|
|
45
|
-
* @template K - The component name type (key of T)
|
|
46
|
-
* @param eid - The entity ID
|
|
47
|
-
* @param name - The component name
|
|
48
|
-
*/
|
|
49
|
-
removeComponent: <K extends keyof T>(eid: number, name: K) => void;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Retrieves all entities that contain the specified set of components.
|
|
53
|
-
* Performs a filtered search across component stores and yields each matching entity.
|
|
54
|
-
* The returned generator allows efficient iteration without allocating large arrays.
|
|
55
|
-
*
|
|
56
|
-
* @template C - Array of component names to query for
|
|
57
|
-
* @param components - List of component names that the entity must include
|
|
58
|
-
* @returns A generator that yields an object for each matching entity,
|
|
59
|
-
* containing the entity ID and the related component data
|
|
60
|
-
*/
|
|
61
|
-
query: <C extends (keyof T)[]>(...componentName: C) => Generator<QueryResult<C>>;
|
|
62
|
-
|
|
63
|
-
components: StoreDataMap<T>;
|
|
64
|
-
}
|
|
@@ -1,65 +0,0 @@
|
|
|
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
|
-
* @throws Error if the entity doesn't exist
|
|
63
|
-
*/
|
|
64
|
-
getMask: (eid: number) => bigint;
|
|
65
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Interface used for fast entity lookups, insertion and removal.
|
|
3
|
-
*
|
|
4
|
-
* Maintain two parallel arrays:
|
|
5
|
-
*
|
|
6
|
-
* - sparse: a direct lookup table that maps an entity ID (eid) to its index in the dense array.
|
|
7
|
-
* - dense: a compact array that stores all active entity IDs contiguously.
|
|
8
|
-
*/
|
|
9
|
-
export interface ISparseSet {
|
|
10
|
-
/**
|
|
11
|
-
* Check if an entity ID exists in the set
|
|
12
|
-
* @param eid The entity ID to check
|
|
13
|
-
* @returns True if the entity ID exists, false otherwise
|
|
14
|
-
*/
|
|
15
|
-
has: (eid: number) => boolean;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Add the entity ID to the set
|
|
19
|
-
* @param eid The entity ID to add
|
|
20
|
-
* @thows Error if the entity ID is already been added
|
|
21
|
-
*/
|
|
22
|
-
add: (eid: number) => void;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Remove an Entity ID from the set
|
|
26
|
-
* @param eid The entity ID to remove
|
|
27
|
-
* @throws Error if the ID does not exists in the set
|
|
28
|
-
*/
|
|
29
|
-
remove: (eid: number) => void;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Retrive the index of the entity ID in the set
|
|
33
|
-
* @param eid The entity ID
|
|
34
|
-
* @returns The index corresponding to an entity ID in the set
|
|
35
|
-
*/
|
|
36
|
-
getIndex: (eid: number) => number;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Retrive the full dense array that contain all the entity IDs of the set
|
|
40
|
-
* @returns an Array with all the current entity IDs
|
|
41
|
-
*/
|
|
42
|
-
getDense: () => number[];
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Retrive the size representing the number of active entities in the set.
|
|
46
|
-
* @returns The current size of the Dense array
|
|
47
|
-
*/
|
|
48
|
-
getSize: () => number;
|
|
49
|
-
}
|
package/src/types/index.d.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import type { IComponentStore } from './IComponentStore.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Represents component data in Structure of Arrays (SoA) format.
|
|
5
|
-
* Each property of the component is stored in a separate array,
|
|
6
|
-
* enabling cache-efficient iteration and SIMD operations.
|
|
7
|
-
* Numeric properties are backed by a Float32Array for a compact, homogeneous
|
|
8
|
-
* memory layout; non-numeric properties fall back to a plain JS array.
|
|
9
|
-
* @template T - The component data type
|
|
10
|
-
* @example
|
|
11
|
-
* // For Position component: { x: number, y: number }
|
|
12
|
-
* // ComponentDataArrays would be: { x: Float32Array, y: Float32Array }
|
|
13
|
-
*/
|
|
14
|
-
export type ComponentDataArrays<T> = {
|
|
15
|
-
[K in keyof T]: T[K] extends number ? Float32Array : T[K][];
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Maps component names to their corresponding ComponentStore instances.
|
|
20
|
-
* Used internally by CreateWorld to manage all component stores.
|
|
21
|
-
* @template T - Record type defining all component types
|
|
22
|
-
*/
|
|
23
|
-
export type StoreMap<T> = {
|
|
24
|
-
[K in keyof T]: IComponentStore<T[K]>;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Maps component names to their data arrays for direct access.
|
|
29
|
-
* Used to expose component data to the user without store methods.
|
|
30
|
-
* @template T - Record type defining all component types
|
|
31
|
-
*/
|
|
32
|
-
export type StoreDataMap<T> = {
|
|
33
|
-
[K in keyof T]: ComponentDataArrays<T[K]>;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Type helper for the query result.
|
|
38
|
-
* Creates an object with an entry for each requested component, holding its store index.
|
|
39
|
-
* @template C - Array of the component names requested in the query.
|
|
40
|
-
*/
|
|
41
|
-
export type QueryResult<C extends readonly PropertyKey[]> = {
|
|
42
|
-
[K in C[number]]: { id: number };
|
|
43
|
-
};
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { IComponentRegistry } from '../types/IComponentRegistry.js';
|
|
2
|
-
|
|
3
|
-
export function createComponentRegistry<K extends string>(): IComponentRegistry<K> {
|
|
4
|
-
const nameToID = new Map<K, number>();
|
|
5
|
-
const IDtoName = new Map<number, K>();
|
|
6
|
-
let nextID: number = 0;
|
|
7
|
-
|
|
8
|
-
function register(name: K): void {
|
|
9
|
-
if (!nameToID.has(name)) {
|
|
10
|
-
const eid = nextID++;
|
|
11
|
-
|
|
12
|
-
nameToID.set(name, eid);
|
|
13
|
-
IDtoName.set(eid, name);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function getID(name: K): number {
|
|
18
|
-
const eid = nameToID.get(name);
|
|
19
|
-
if (eid === undefined) throw new Error(`Component ${name} not registered`);
|
|
20
|
-
|
|
21
|
-
return eid;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function getName(cid: number): K {
|
|
25
|
-
const name = IDtoName.get(cid);
|
|
26
|
-
if (!name) throw new Error(`Component ID ${cid} not registered`);
|
|
27
|
-
|
|
28
|
-
return name;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return { register, getID, getName };
|
|
32
|
-
}
|
package/src/utils/SparseSet.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import type { ISparseSet } from '../types/ISparseSet.js';
|
|
2
|
-
|
|
3
|
-
export function SparseSet(): ISparseSet {
|
|
4
|
-
const sparse: number[] = [];
|
|
5
|
-
const dense: number[] = [];
|
|
6
|
-
let size = 0;
|
|
7
|
-
|
|
8
|
-
function has(eid: number): boolean {
|
|
9
|
-
const ID = getIndex(eid);
|
|
10
|
-
|
|
11
|
-
const exist = ID !== undefined && dense[ID] === eid;
|
|
12
|
-
const isWhitinBounds = ID >= 0 && ID < size;
|
|
13
|
-
|
|
14
|
-
return exist && isWhitinBounds;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function add(eid: number): void {
|
|
18
|
-
if (has(eid)) throw new Error(`ID ${eid} already present in the set`);
|
|
19
|
-
|
|
20
|
-
sparse[eid] = size;
|
|
21
|
-
dense[size] = eid;
|
|
22
|
-
size++;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function remove(eid: number): void {
|
|
26
|
-
if (!has(eid)) throw new Error(`Cannot remove ID ${eid} because does not exists`);
|
|
27
|
-
|
|
28
|
-
const ID = getIndex(eid);
|
|
29
|
-
const last = dense[size - 1];
|
|
30
|
-
|
|
31
|
-
// Swap with the last element inserted
|
|
32
|
-
dense[ID] = last;
|
|
33
|
-
sparse[last] = ID;
|
|
34
|
-
|
|
35
|
-
// Remove the element
|
|
36
|
-
dense.pop();
|
|
37
|
-
size--;
|
|
38
|
-
delete sparse[eid];
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function getIndex(eid: number): number {
|
|
42
|
-
return sparse[eid];
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function getDense(): number[] {
|
|
46
|
-
return dense;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function getSize(): number {
|
|
50
|
-
return size;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return { has, add, remove, getIndex, getDense, getSize };
|
|
54
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "es2024",
|
|
4
|
-
"allowSyntheticDefaultImports": true,
|
|
5
|
-
"esModuleInterop": true,
|
|
6
|
-
"outDir": "dist",
|
|
7
|
-
"forceConsistentCasingInFileNames": true,
|
|
8
|
-
"resolveJsonModule": true,
|
|
9
|
-
"strict": true,
|
|
10
|
-
"sourceMap": true,
|
|
11
|
-
"rootDir": "src",
|
|
12
|
-
"moduleResolution": "nodenext",
|
|
13
|
-
"module": "node20"
|
|
14
|
-
},
|
|
15
|
-
"exclude": ["node_modules/"],
|
|
16
|
-
"include": ["src"]
|
|
17
|
-
}
|