archetype-ecs 1.3.1 → 2.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/README.md +97 -11
- package/bench/allocations-1m.js +1 -1
- package/bench/multi-ecs-bench.js +1 -1
- package/bench/typed-vs-bitecs-1m.js +1 -1
- package/bench/typed-vs-untyped.js +81 -81
- package/bench/vs-bitecs.js +31 -56
- package/dist/src/ComponentRegistry.d.ts +13 -0
- package/dist/src/ComponentRegistry.js +29 -0
- package/dist/src/EntityManager.d.ts +52 -0
- package/dist/src/EntityManager.js +787 -0
- package/dist/src/Profiler.d.ts +12 -0
- package/dist/src/Profiler.js +38 -0
- package/dist/src/System.d.ts +37 -0
- package/dist/src/System.js +139 -0
- package/dist/src/index.d.ts +10 -0
- package/dist/src/index.js +29 -0
- package/dist/tests/EntityManager.test.d.ts +1 -0
- package/dist/tests/EntityManager.test.js +499 -0
- package/dist/tests/System.test.d.ts +1 -0
- package/dist/tests/System.test.js +630 -0
- package/dist/tests/types.d.ts +1 -0
- package/dist/tests/types.js +129 -0
- package/package.json +8 -7
- package/src/ComponentRegistry.ts +45 -0
- package/src/EntityManager.ts +909 -0
- package/src/{Profiler.js → Profiler.ts} +18 -5
- package/src/System.ts +201 -0
- package/src/index.ts +38 -0
- package/tests/{EntityManager.test.js → EntityManager.test.ts} +182 -57
- package/tests/System.test.ts +546 -0
- package/tests/types.ts +69 -68
- package/tsconfig.json +8 -5
- package/src/ComponentRegistry.js +0 -21
- package/src/EntityManager.js +0 -578
- package/src/index.d.ts +0 -118
- package/src/index.js +0 -37
package/src/index.d.ts
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
// === Basis types ===
|
|
2
|
-
export type EntityId = number;
|
|
3
|
-
|
|
4
|
-
// === Field reference descriptor ===
|
|
5
|
-
export interface FieldRef<V = number> {
|
|
6
|
-
readonly _sym: symbol;
|
|
7
|
-
readonly _field: string;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
// === Component definition ===
|
|
11
|
-
declare const __phantom: unique symbol;
|
|
12
|
-
export type ComponentDef<T = unknown> = {
|
|
13
|
-
readonly _sym: symbol;
|
|
14
|
-
readonly _name: string;
|
|
15
|
-
readonly [__phantom]?: T;
|
|
16
|
-
} & (T extends Record<string, number | string>
|
|
17
|
-
? { readonly [K in keyof T & string]: FieldRef<T[K]> }
|
|
18
|
-
: {});
|
|
19
|
-
|
|
20
|
-
/** @deprecated Use ComponentDef<T> instead */
|
|
21
|
-
export type Component<T = unknown> = ComponentDef<T>;
|
|
22
|
-
export type ComponentType = ComponentDef;
|
|
23
|
-
|
|
24
|
-
// === TypedArray schema ===
|
|
25
|
-
export type TypedArrayType = 'f32' | 'f64' | 'i8' | 'i16' | 'i32' | 'u8' | 'u16' | 'u32' | 'string';
|
|
26
|
-
|
|
27
|
-
export type Schema = Record<string, TypedArrayType>;
|
|
28
|
-
|
|
29
|
-
/** Maps a schema type to its runtime value type */
|
|
30
|
-
type FieldToType<T extends TypedArrayType> = T extends 'string' ? string : number;
|
|
31
|
-
|
|
32
|
-
/** Maps a schema definition to its runtime value type */
|
|
33
|
-
type SchemaToType<S extends Schema> = { [K in keyof S]: FieldToType<S[K]> };
|
|
34
|
-
|
|
35
|
-
export declare const TYPED: unique symbol;
|
|
36
|
-
|
|
37
|
-
export declare const componentSchemas: Map<symbol, Record<string, Float32ArrayConstructor | Float64ArrayConstructor | Int8ArrayConstructor | Int16ArrayConstructor | Int32ArrayConstructor | Uint8ArrayConstructor | Uint16ArrayConstructor | Uint32ArrayConstructor | ArrayConstructor>>;
|
|
38
|
-
|
|
39
|
-
// === TypedArray union ===
|
|
40
|
-
type TypedArray = Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array;
|
|
41
|
-
|
|
42
|
-
// === ArchetypeView (forEach callback) ===
|
|
43
|
-
export interface ArchetypeView {
|
|
44
|
-
readonly id: number;
|
|
45
|
-
readonly entityIds: EntityId[];
|
|
46
|
-
readonly count: number;
|
|
47
|
-
readonly snapshotEntityIds: EntityId[] | null;
|
|
48
|
-
readonly snapshotCount: number;
|
|
49
|
-
field(ref: FieldRef<any>): TypedArray | unknown[] | undefined;
|
|
50
|
-
snapshot(ref: FieldRef<any>): TypedArray | unknown[] | undefined;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// === Serialize/Deserialize ===
|
|
54
|
-
export interface SerializeOptions {
|
|
55
|
-
serializers?: Map<string, (data: unknown) => unknown>;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export interface DeserializeOptions {
|
|
59
|
-
deserializers?: Map<string, (data: unknown) => unknown>;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export interface SerializedData {
|
|
63
|
-
nextId: number;
|
|
64
|
-
entities: EntityId[];
|
|
65
|
-
components: Record<string, Record<string, unknown>>;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// === EntityManager ===
|
|
69
|
-
export interface EntityManager {
|
|
70
|
-
createEntity(): EntityId;
|
|
71
|
-
destroyEntity(id: EntityId): void;
|
|
72
|
-
addComponent<T>(entityId: EntityId, type: ComponentDef<T>, data: T): void;
|
|
73
|
-
removeComponent(entityId: EntityId, type: ComponentDef): void;
|
|
74
|
-
getComponent<T>(entityId: EntityId, type: ComponentDef<T>): T | undefined;
|
|
75
|
-
get<V>(entityId: EntityId, fieldRef: FieldRef<V>): V | undefined;
|
|
76
|
-
set<V>(entityId: EntityId, fieldRef: FieldRef<V>, value: V): void;
|
|
77
|
-
hasComponent(entityId: EntityId, type: ComponentDef): boolean;
|
|
78
|
-
query(include: ComponentDef[], exclude?: ComponentDef[]): EntityId[];
|
|
79
|
-
getAllEntities(): EntityId[];
|
|
80
|
-
createEntityWith(...args: unknown[]): EntityId;
|
|
81
|
-
count(include: ComponentDef[], exclude?: ComponentDef[]): number;
|
|
82
|
-
forEach(include: ComponentDef[], callback: (view: ArchetypeView) => void, exclude?: ComponentDef[]): void;
|
|
83
|
-
enableTracking(filterComponent: ComponentDef): void;
|
|
84
|
-
flushChanges(): { created: Set<EntityId>; destroyed: Set<EntityId> };
|
|
85
|
-
flushSnapshots(): void;
|
|
86
|
-
serialize(
|
|
87
|
-
symbolToName: Map<symbol, string>,
|
|
88
|
-
stripComponents?: ComponentDef[],
|
|
89
|
-
skipEntitiesWith?: ComponentDef[],
|
|
90
|
-
options?: SerializeOptions
|
|
91
|
-
): SerializedData;
|
|
92
|
-
deserialize(
|
|
93
|
-
data: SerializedData,
|
|
94
|
-
nameToSymbol: Record<string, ComponentDef>,
|
|
95
|
-
options?: DeserializeOptions
|
|
96
|
-
): void;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// === Profiler ===
|
|
100
|
-
export interface ProfilerEntry {
|
|
101
|
-
avg: number;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export interface Profiler {
|
|
105
|
-
readonly enabled: boolean;
|
|
106
|
-
setEnabled(value: boolean): void;
|
|
107
|
-
begin(): number;
|
|
108
|
-
end(name: string, t0: number): void;
|
|
109
|
-
record(name: string, ms: number): void;
|
|
110
|
-
getData(): Map<string, ProfilerEntry>;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// === Exports ===
|
|
114
|
-
export function createEntityManager(): EntityManager;
|
|
115
|
-
export function component(name: string): ComponentDef;
|
|
116
|
-
export function component<T extends TypedArrayType, F extends string>(name: string, type: T, fields: F[]): ComponentDef<Record<F, FieldToType<T>>>;
|
|
117
|
-
export function component<S extends Schema>(name: string, schema: S): ComponentDef<SchemaToType<S>>;
|
|
118
|
-
export const profiler: Profiler;
|
package/src/index.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
export { createEntityManager } from './EntityManager.js';
|
|
2
|
-
export { profiler } from './Profiler.js';
|
|
3
|
-
export { TYPED, componentSchemas } from './ComponentRegistry.js';
|
|
4
|
-
import { TYPE_MAP, componentSchemas } from './ComponentRegistry.js';
|
|
5
|
-
|
|
6
|
-
export function component(name, typeOrSchema, fields) {
|
|
7
|
-
const sym = Symbol(name);
|
|
8
|
-
const comp = { _sym: sym, _name: name };
|
|
9
|
-
|
|
10
|
-
let schema;
|
|
11
|
-
|
|
12
|
-
if (typeof typeOrSchema === 'string' && Array.isArray(fields)) {
|
|
13
|
-
// Short form: component('Position', 'f32', ['x', 'y'])
|
|
14
|
-
const Ctor = TYPE_MAP[typeOrSchema];
|
|
15
|
-
if (!Ctor) throw new Error(`Unknown type "${typeOrSchema}"`);
|
|
16
|
-
schema = {};
|
|
17
|
-
for (const f of fields) {
|
|
18
|
-
schema[f] = Ctor;
|
|
19
|
-
comp[f] = { _sym: sym, _field: f };
|
|
20
|
-
}
|
|
21
|
-
} else if (typeOrSchema && typeof typeOrSchema === 'object') {
|
|
22
|
-
// Schema form: component('Position', { x: 'f32', y: 'f32' })
|
|
23
|
-
schema = {};
|
|
24
|
-
for (const [field, type] of Object.entries(typeOrSchema)) {
|
|
25
|
-
const Ctor = TYPE_MAP[type];
|
|
26
|
-
if (!Ctor) throw new Error(`Unknown type "${type}" for field "${field}"`);
|
|
27
|
-
schema[field] = Ctor;
|
|
28
|
-
comp[field] = { _sym: sym, _field: field };
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (schema) {
|
|
33
|
-
componentSchemas.set(sym, schema);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return comp;
|
|
37
|
-
}
|