@javelin/ecs
Version:
93 lines (92 loc) • 3.41 kB
TypeScript
import { PackedSparseArray, Schema } from "@javelin/core";
import { Component, ComponentOf } from "./component";
import { Entity } from "./entity";
import { Type } from "./type";
export declare type ComponentStoreColumn<S extends Schema> = ComponentOf<S>[];
export declare type ComponentStoreTable<S extends Schema[]> = {
readonly [K in keyof S]: ComponentStoreColumn<S[K] extends Schema ? S[K] : never>;
};
export declare type ComponentStore<$Signature extends Schema[]> = {
/**
* Two-dimensional array of component type->component[] where each index
* (column) of the component array corresponds to an entity.
*
*```
* (index) 0 1 2
* (entity) 1 3 9
* (Position) [[ p, p, p ]
* (Velocity) [ v, v, v ]]
*```
* The index of each entity is tracked in the `indices` array.
*/
table: ComponentStoreTable<$Signature>;
/**
* Array of entities tracked by this archetype. Not used internally:
* primarily a convenience for iteration/checks by consumers.
*/
entities: Entity[];
/**
* Array where each index corresponds to an entity, and each value
* corresponds to that entity's index in the component table. In the example
* above, this array might look like:
*
*```
* 1 3 9
* [empty, 0, empty, 1, empty x5, 2]
*```
*/
indices: number[];
/**
* An array of schema ids that describes the type signature of an archetype.
* The position of a schema id in this array is equivalent to the index of
* the column where instances of that schema are stored.
*/
type: Type;
};
export declare type ReadonlyComponentStore<$Signature extends Schema[]> = {
readonly [K in keyof ComponentStore<$Signature>]: Readonly<ComponentStore<$Signature>[K]>;
};
/**
* A JSON-serializable snapshot of an archetype. An archetype snapshot is
* composed of a type signature, entities (in the form of a packed sparse-
* array), and component data.
*/
export declare type ArchetypeSnapshot<$Signature extends Schema[] = Schema[]> = Pick<ComponentStore<$Signature>, "table" | "type"> & {
indices: PackedSparseArray<number>;
};
/**
* A collection of entities that share components of the same type.
*/
export declare type Archetype<$Signature extends Schema[] = Schema[]> = ReadonlyComponentStore<$Signature> & {
/**
* A flipped version of the archetype's signature where each index is a
* schema id and the corresponding index is the schema's column index in
* the component table.
*/
readonly typeInverse: number[];
/**
* Insert an entity into the Archetype.
* @param entity Subject entity
* @param components Array of components
* @returns void
*/
insert(entity: Entity, components: Component[]): void;
/**
* Remove an entity from the Archetype.
* @param entity Subject entity
* @returns void
*/
remove(entity: Entity): void;
};
export declare type ArchetypeOptions<$Signature extends Schema[]> = {
type: number[];
} | {
snapshot: ArchetypeSnapshot<$Signature>;
};
/**
* Create an Archetype.
* @param signature
* @param table
*/
export declare function createArchetype<$Signature extends Schema[]>(options: ArchetypeOptions<$Signature>): Archetype<$Signature>;
//# sourceMappingURL=archetype.d.ts.map