@javelin/ecs
Version:
81 lines • 3 kB
TypeScript
import { FieldExtract, Schema } from "@javelin/core";
export declare const $type: unique symbol;
export declare const $pool: unique symbol;
export declare type ComponentMetadata = {
[$type]?: number;
[$pool]?: boolean;
};
export declare type ComponentOf<$Schema extends Schema> = FieldExtract<$Schema> & ComponentMetadata;
export declare type ComponentsOf<$Signature extends Schema[]> = {
[K in keyof $Signature]: $Signature[K] extends Schema ? ComponentOf<$Signature[K]> : never;
};
export declare type Component = ComponentOf<Schema>;
/**
* Check if a component is an instance of a component type.
* @param component
* @param schema
* @returns
* @example
* const A = {}
* const B = {}
* const a = component(A)
* isComponentOf(a, A) // true
* isComponentOf(a, B) // false
*/
export declare function isComponentOf<$Schema extends Schema>(component: Component, schema: $Schema): component is ComponentOf<$Schema>;
export declare function createComponentPool<$Schema extends Schema>(schema: $Schema, poolSize: number): import("@javelin/core").StackPool<ComponentOf<$Schema>>;
/**
* Manually register a component type. Optionally specify a unique, integer id
* and/or size for the component type's object pool.
* @param schema
* @param schemaId
* @param [poolSize=1000]
* @returns
* @example <caption>register a schema as a component type (optional)</caption>
* ```ts
* const Vehicle = { torque: number }
* registerSchema(Vehicle)
* ```
* @example <caption>register a schema with a fixed id</caption>
* ```ts
* const Vehicle = { torque: number }
* registerSchema(Vehicle, 22)
* ```
* @example <caption>register a schema with a fixed id and pool size</caption>
* ```ts
* const Particle = { color: number }
* registerSchema(Particle, 3, 10_000)
* ```
*/
export declare function registerSchema(schema: Schema, schemaId?: number, poolSize?: number): number;
/**
* Use a Schema to create a component. The second parameter is an optional
* object that will be used to assign initial values to the new component
* instance.
* @param schema
* @param props
* @returns
* @example
* ```ts
* const Quaternion = { x: number, y: number, z: number, w: number }
* const quaternion = component(Quaternion, { w: 1 })
* ```
*/
export declare function component<$Schema extends Schema>(schema: $Schema, props?: Partial<FieldExtract<$Schema>>): ComponentOf<$Schema>;
export declare function toComponentFromType(object: object, type: number): Component;
/**
* Instruct the ECS to treat an object as a component instance of a given
* schema.
* @param object
* @param schema
* @returns
*/
export declare function toComponent<$Schema extends Schema>(object: FieldExtract<$Schema>, schema: $Schema): ComponentOf<$Schema>;
/**
* Get the type id (number) of a component. Throws an error if the object is
* not a valid component.
* @param component
* @returns
*/
export declare function getSchemaId(component: object): number;
//# sourceMappingURL=component.d.ts.map