UNPKG

wolf-ecs

Version:

An entity component system framework for JavaScript and TypeScript

149 lines (144 loc) 5.46 kB
declare type TypedArrayConstructor = Int8ArrayConstructor | Uint8ArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor | BigInt64ArrayConstructor | BigUint64ArrayConstructor; declare type TypedArray = Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array; declare function custom<T>(init: () => T): [() => T]; declare function custom<T>(): T[]; declare const types: { custom: typeof custom; any: ArrayConstructor; int8: Int8ArrayConstructor; i8: Int8ArrayConstructor; char: Int8ArrayConstructor; uint8: Uint8ArrayConstructor; u8: Uint8ArrayConstructor; uchar: Uint8ArrayConstructor; int16: Int16ArrayConstructor; i16: Int16ArrayConstructor; short: Int16ArrayConstructor; uint16: Uint16ArrayConstructor; u16: Uint16ArrayConstructor; ushort: Uint16ArrayConstructor; int32: Int32ArrayConstructor; i32: Int32ArrayConstructor; int: Int32ArrayConstructor; uint32: Uint32ArrayConstructor; u32: Uint32ArrayConstructor; uint: Uint32ArrayConstructor; float32: Float32ArrayConstructor; f32: Float32ArrayConstructor; float: Float32ArrayConstructor; float64: Float64ArrayConstructor; f64: Float64ArrayConstructor; double: Float64ArrayConstructor; int64: BigInt64ArrayConstructor; bigint64: BigInt64ArrayConstructor; i64: BigInt64ArrayConstructor; long: BigInt64ArrayConstructor; uint64: BigUint64ArrayConstructor; biguint64: BigUint64ArrayConstructor; u64: BigUint64ArrayConstructor; ulong: BigUint64ArrayConstructor; }; declare type Tree<LeafType> = LeafType | { [key: string]: Tree<LeafType>; }; declare type InitFunc = () => unknown; declare type Type = TypedArrayConstructor | ArrayConstructor | [InitFunc] | unknown[]; declare type ComponentArray<T extends Tree<Type> = Tree<Type>> = (T extends [InitFunc] ? ReturnType<T[0]>[] : T extends unknown[] ? T : T extends ArrayConstructor ? any[] : T extends Exclude<Type, unknown[]> ? InstanceType<T> : { [key in keyof T]: T[key] extends Tree<Type> ? ComponentArray<T[key]> : never; }); declare class SparseSet { packed: number[]; sparse: number[]; has(x: number): boolean; add(x: number): void; remove(x: number): void; } declare class Archetype { sset: SparseSet; entities: number[]; e: number[]; mask: Uint32Array; change: Archetype[]; constructor(mask: Uint32Array); has(x: number): boolean; } declare function all<Q extends (RawQuery | ComponentArray)>(...cmps: Q[]): { op: typeof all; dt: Q[]; }; declare function not<Q extends (RawQuery | ComponentArray)>(cmp: Q): { op: typeof not; dt: Q | { op: typeof all; dt: Q[]; }; }; declare function any<Q extends (RawQuery | ComponentArray)>(...cmps: Q[]): { op: typeof any; dt: Q[]; }; declare type MLeaf = { op: typeof all | typeof any; dt: Uint32Array; }; declare type Group = { op: typeof all | typeof any; dt: [MLeaf, ...QueryMask[]]; }; declare type Not = { op: typeof not; dt: QueryMask; }; declare type QueryMask = Group | Not | MLeaf; declare type RawQuery = { op: typeof all | typeof any; dt: (RawQuery | ComponentArray)[]; } | { op: typeof not; dt: RawQuery | ComponentArray; }; declare class Query { mask: QueryMask; archetypes: Archetype[]; a: Archetype[]; ecs: ECS; constructor(ecs: ECS, q: RawQuery | undefined); forEach(callbackfn: (id: number, ecs: ECS) => void): void; _forEach(callbackfn: (id: number, ecs: ECS) => void): void; static match(target: Uint32Array, mask: QueryMask): boolean; protected static partial(target: Uint32Array, mask: MLeaf): boolean; } declare class ECS { protected _arch: Map<string, Archetype>; protected _queries: Query[]; protected _ent: Archetype[]; protected _updateTo: Archetype[]; protected _toUpdate: SparseSet; protected _toDestroy: SparseSet; protected _rm: SparseSet; protected _empty: Archetype; protected cmpID: number; protected entID: number; readonly MAX_ENTITIES: number; readonly DEFAULT_DEFER: boolean; constructor(max?: number, defer?: boolean); bind(): Pick<ECS, Exclude<{ [K in keyof ECS]: ECS[K] extends Function ? K : never; }[keyof ECS], "bind">>; defineComponent<T extends Tree<Type>>(def: T): ComponentArray<T>; defineComponent(): {}; registerComponent<T>(cmp: T): T; createQuery(...raw: RawQuery[]): Query; protected _validID(id: unknown): boolean; protected _getArch(mask: Uint32Array): Archetype; protected _hasComponent(mask: Uint32Array, i: number): number; protected _archChange(arch: Archetype, i: number): Archetype; protected _crEnt(id: number): void; createEntity(): number; destroyEntity(id: number, defer?: boolean): void; destroyPending(): void; addComponent(id: number, cmp: ComponentArray, defer?: boolean): this; removeComponent(id: number, cmp: ComponentArray, defer?: boolean): this; updatePending(): void; } export { ComponentArray, ECS, Tree, TypedArray, TypedArrayConstructor, all, any, not, types };