diff --git a/lib/index.d.ts b/lib/index.d.ts index 826f62a..e96da39 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1,158 +1,172 @@ type Archetype = { - id: number, - edges: { - [key: number]: { - add: Archetype, - remove: Archetype, - }, - }, - types: Array, - type: string | number, - entities: Array, - columns: Array>, - records: { [key: number]: number }, -} + id: number; + edges: { + [key: number]: { + add: Archetype; + remove: Archetype; + }; + }; + types: Array; + type: string | number; + entities: Array; + columns: Array>; + records: { [key: number]: number }; +}; type ArchetypeMap = { - cache: Array, - first: ArchetypeMap, - second: ArchetypeMap, - parent: ArchetypeMap, - size: number, -} + cache: Array; + first: ArchetypeMap; + second: ArchetypeMap; + parent: ArchetypeMap; + size: number; +}; type Query = { - without: (...components: Entity[]) => Query; -} & IterableFunction> + without: (...components: Entity[]) => Query; +} & IterableFunction>; // Exported due to functions below requiring this type. export type EntityIndex = { - dense: { - [key: number]: number + dense: { + [key: number]: number; + }; + sparse: { + [key: number]: { + archetype: Archetype; + row: number; + dense: number; + componentRecord: ArchetypeMap; }; - sparse: { - [key: number]: { - archetype: Archetype, - row: number, - dense: number, - componentRecord: ArchetypeMap, - } - } -} + }; +}; // Utility Types -export type Entity = number & { __nominal_type_dont_use: T } +export type Entity = number & { __nominal_type_dont_use: T }; export type EntityType = T extends Entity ? A : never; export type InferComponents = { - [K in keyof A]: EntityType + [K in keyof A]: EntityType; }; type Nullable = { - [K in keyof T]: T[K] | undefined -} + [K in keyof T]: T[K] | undefined; +}; export class World { + /** + * Creates a new World + */ + constructor(); - /** - * Creates a new World - */ - constructor(); + /** + * Creates a new entity + * @returns Entity + */ + entity(): Entity; - /** - * Creates a new entity - * @returns Entity - */ - entity(): Entity; + /** + * Creates a new entity located in the first 256 ids. + * These should be used for static components for fast access. + * @returns Entity + */ + component(): Entity; - /** - * Creates a new entity located in the first 256 ids. - * These should be used for static components for fast access. - * @returns Entity - */ - component(): Entity; + /** + * Gets the target of a relationship. For example, when a user calls + * `world.target(id, ChildOf(parent))`, you will obtain the parent entity. + * @param id Entity + * @param relation The Relationship + * @returns The Parent Entity if it exists + */ + target(id: Entity, relation: Entity): Entity | undefined; - /** - * Gets the target of a relationship. For example, when a user calls - * `world.target(id, ChildOf(parent))`, you will obtain the parent entity. - * @param id Entity - * @param relation The Relationship - * @returns The Parent Entity if it exists - */ - target(id: Entity, relation: Entity): Entity | undefined; + /** + * Deletes an entity and all its related components and relationships. + * @param id Entity to be destroyed + */ + delete(id: Entity): void; - /** - * Deletes an entity and all its related components and relationships. - * @param id Entity to be destroyed - */ - delete(id: Entity): void; + /** + * Adds a component to the entity with no value + * @param id Target Entity + * @param component Component + */ + add(id: Entity, component: Entity): void; - /** - * Adds a component to the entity with no value - * @param id Target Entity - * @param component Component - */ - add(id: Entity, component: Entity): void; + /** + * Assigns a value to a component on the given entity + * @param id Target Entity + * @param component Target Component + * @param data Component Data + */ + set(id: Entity, component: Entity, data: T): void; - /** - * Assigns a value to a component on the given entity - * @param id Target Entity - * @param component Target Component - * @param data Component Data - */ - set(id: Entity, component: Entity, data: T): void; + /** + * Removes a component from the given entity + * @param id Target Entity + * @param component Target Component + */ + remove(id: Entity, component: Entity): void; - /** - * Removes a component from the given entity - * @param id Target Entity - * @param component Target Component - */ - remove(id: Entity, component: Entity): void; + // Manually typed out get since there is a hard limit. - // Manually typed out get since there is a hard limit. + /** + * Retrieves the value of one component. This value may be undefined. + * @param id Target Entity + * @param component Target Component + * @returns Data associated with the component if it exists + */ + get(id: number, component: Entity): A | undefined; - /** - * Retrieves the value of one component. This value may be undefined. - * @param id Target Entity - * @param component Target Component - * @returns Data associated with the component if it exists - */ - get(id: number, component: Entity): A | undefined; + /** + * Retrieves the value of two components. This value may be undefined. + * @param id Target Entity + * @param component Target Component 1 + * @param component2 Target Component 2 + * @returns Data associated with the components if it exists + */ + get( + id: number, + component: Entity, + component2: Entity + ): LuaTuple>; - /** - * Retrieves the value of two components. This value may be undefined. - * @param id Target Entity - * @param component Target Component 1 - * @param component2 Target Component 2 - * @returns Data associated with the components if it exists - */ - get(id: number, component: Entity, component2: Entity): LuaTuple>; + /** + * Retrieves the value of three components. This value may be undefined. + * @param id Target Entity + * @param component Target Component 1 + * @param component2 Target Component 2 + * @param component3 Target Component 3 + * @returns Data associated with the components if it exists + */ + get( + id: number, + component: Entity, + component2: Entity, + component3: Entity + ): LuaTuple>; - /** - * Retrieves the value of three components. This value may be undefined. - * @param id Target Entity - * @param component Target Component 1 - * @param component2 Target Component 2 - * @param component3 Target Component 3 - * @returns Data associated with the components if it exists - */ - get(id: number, component: Entity, component2: Entity, component3: Entity): LuaTuple>; + /** + * Retrieves the value of four components. This value may be undefined. + * @param id Target Entity + * @param component Target Component 1 + * @param component2 Target Component 2 + * @param component3 Target Component 3 + * @param component4 Target Component 4 + * @returns Data associated with the components if it exists + */ + get( + id: number, + component: Entity, + component2: Entity, + component3: Entity, + component4: Entity + ): LuaTuple>; - /** - * Retrieves the value of four components. This value may be undefined. - * @param id Target Entity - * @param component Target Component 1 - * @param component2 Target Component 2 - * @param component3 Target Component 3 - * @param component4 Target Component 4 - * @returns Data associated with the components if it exists - */ - get(id: number, component: Entity, component2: Entity, component3: Entity, component4: Entity): LuaTuple>; - - /** - * Searches the world for entities that match a given query - * @param components Queried Components - * @returns Iterable function - */ - query(...components: T): Query> + /** + * Searches the world for entities that match a given query + * @param components Queried Components + * @returns Iterable function + */ + query(...components: T): Query>; } export const pair: (pred: Entity, obj: Entity) => Entity; @@ -169,7 +183,13 @@ export const ECS_ID: (e: Entity) => Entity; export const ECS_PAIR: (pred: Entity, obj: Entity) => Entity; export const ECS_GENERATION_INC: (e: Entity) => Entity; export const ECS_GENERATION: (e: Entity) => Entity; -export const ECS_PAIR_RELATION: (entityIndex: EntityIndex, e: Entity) => Entity; -export const ECS_PAIR_OBJECT: (entityIndex: EntityIndex, e: Entity) => Entity; +export const ECS_PAIR_RELATION: ( + entityIndex: EntityIndex, + e: Entity +) => Entity; +export const ECS_PAIR_OBJECT: ( + entityIndex: EntityIndex, + e: Entity +) => Entity; -export const getAlive: (entityIndex: EntityIndex, id: Entity) => Entity; \ No newline at end of file +export const getAlive: (entityIndex: EntityIndex, id: Entity) => Entity;