UNPKG

ecspresso

Version:

A minimal Entity-Component-System library for typescript and javascript.

77 lines (76 loc) 4.05 kB
import { SystemBuilderWithBundle } from './system-builder'; import type ECSpresso from './ecspresso'; /** * Bundle class that encapsulates a set of components, resources, events, and systems * that can be merged into a ECSpresso instance */ export default class Bundle<ComponentTypes extends Record<string, any> = {}, EventTypes extends Record<string, any> = {}, ResourceTypes extends Record<string, any> = {}> { private _systems; private _resources; private _id; constructor(id?: string); /** * Get the unique ID of this bundle */ get id(): string; /** * Set the ID of this bundle * @internal Used by combineBundles */ set id(value: string); /** * Add a system to this bundle, by label (creating a new builder) or by reusing an existing one */ addSystem<Q extends Record<string, any>>(builder: SystemBuilderWithBundle<ComponentTypes, EventTypes, ResourceTypes, Q>): SystemBuilderWithBundle<ComponentTypes, EventTypes, ResourceTypes, Q>; addSystem(label: string): SystemBuilderWithBundle<ComponentTypes, EventTypes, ResourceTypes, {}>; /** * Add a resource to this bundle * @param label The resource key * @param resource The resource value or a factory function that returns the resource */ addResource<K extends keyof ResourceTypes>(label: K, resource: ResourceTypes[K] | ((ecs: ECSpresso<ComponentTypes, EventTypes, ResourceTypes>) => ResourceTypes[K] | Promise<ResourceTypes[K]>)): this; /** * Get all systems defined in this bundle * Returns built System objects instead of SystemBuilders */ getSystems(): SystemBuilderWithBundle<ComponentTypes, EventTypes, ResourceTypes, any>[]; /** * Register all systems in this bundle with an ECSpresso instance * @internal Used by ECSpresso when adding a bundle */ registerSystemsWithEcspresso(ecspresso: ECSpresso<ComponentTypes, EventTypes, ResourceTypes>): void; /** * Get all resources defined in this bundle */ getResources(): Map<keyof ResourceTypes, ResourceTypes[keyof ResourceTypes]>; /** * Get a specific resource by key * @param key The resource key * @returns The resource value or undefined if not found */ getResource<K extends keyof ResourceTypes>(key: K): ResourceTypes[K]; /** * Get all system builders in this bundle */ getSystemBuilders(): SystemBuilderWithBundle<ComponentTypes, EventTypes, ResourceTypes, any>[]; /** * Check if this bundle has a specific resource * @param key The resource key to check * @returns True if the resource exists */ hasResource<K extends keyof ResourceTypes>(key: K): boolean; } type Exactly<T, U> = T extends U ? U extends T ? true : false : false; type IncompatibleBundles<C1 extends Record<string, any>, C2 extends Record<string, any>, E1 extends Record<string, any>, E2 extends Record<string, any>, R1 extends Record<string, any>, R2 extends Record<string, any>> = { [K in keyof C1 & keyof C2]: Exactly<C1[K], C2[K]> extends false ? never : unknown; } & { [K in keyof E1 & keyof E2]: Exactly<E1[K], E2[K]> extends false ? never : unknown; } & { [K in keyof R1 & keyof R2]: Exactly<R1[K], R2[K]> extends false ? never : unknown; }; /** * Function that merges multiple bundles into a single bundle */ export declare function mergeBundles<C1 extends Record<string, any>, E1 extends Record<string, any>, R1 extends Record<string, any>, C2 extends Record<string, any>, E2 extends Record<string, any>, R2 extends Record<string, any>>(id: string, bundle1: Bundle<C1, E1, R1>, bundle2: Bundle<C2, E2, R2> & IncompatibleBundles<C1, C2, E1, E2, R1, R2>): Bundle<C1 & C2, E1 & E2, R1 & R2>; export declare function mergeBundles<ComponentTypes extends Record<string, any>, EventTypes extends Record<string, any>, ResourceTypes extends Record<string, any>>(id: string, ...bundles: Array<Bundle<ComponentTypes, EventTypes, ResourceTypes>>): Bundle<ComponentTypes, EventTypes, ResourceTypes>; export {};