UNPKG

ecspresso

Version:

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

112 lines (111 loc) 3.92 kB
/** * Asset management for ECSpresso ECS framework */ import type EventBus from './event-bus'; import type { AssetStatus, AssetDefinition, AssetHandle, AssetsResource, AssetEvents, AssetConfigurator } from './asset-types'; /** * Manages asset loading and access for ECSpresso */ export default class AssetManager<AssetTypes extends Record<string, unknown> = Record<string, never>, AssetGroupNames extends string = string> { private readonly assets; private readonly groups; private eventBus; /** * Set the event bus for asset events * @internal */ setEventBus(eventBus: EventBus<AssetEvents<keyof AssetTypes & string, AssetGroupNames>>): void; /** * Register an asset definition */ register<K extends string, T>(key: K, definition: AssetDefinition<T>): void; /** * Load all assets marked as eager */ loadEagerAssets(): Promise<void>; /** * Load a single asset by key */ loadAsset<K extends keyof AssetTypes>(key: K): Promise<AssetTypes[K]>; /** * Load all assets in a group */ loadAssetGroup(groupName: AssetGroupNames): Promise<void>; /** * Get a loaded asset. Throws if not loaded. */ get<K extends keyof AssetTypes>(key: K): AssetTypes[K]; /** * Get a loaded asset or undefined */ tryGet<K extends keyof AssetTypes>(key: K): AssetTypes[K] | undefined; /** * Get a handle to an asset with status information */ getHandle<K extends keyof AssetTypes>(key: K): AssetHandle<AssetTypes[K]>; /** * Get the status of an asset */ getStatus<K extends keyof AssetTypes>(key: K): AssetStatus; /** * Check if an asset is loaded */ isLoaded<K extends keyof AssetTypes>(key: K): boolean; /** * Check if all assets in a group are loaded */ isGroupLoaded(groupName: AssetGroupNames): boolean; /** * Get the loading progress of a group (0-1) */ getGroupProgress(groupName: AssetGroupNames): number; /** * Get detailed group progress */ getGroupProgressDetails(groupName: AssetGroupNames): { loaded: number; total: number; progress: number; }; /** * Check group progress and emit events */ private checkGroupProgress; /** * Create the $assets resource object */ createResource(): AssetsResource<AssetTypes, AssetGroupNames>; /** * Get all registered asset keys */ getKeys(): Array<keyof AssetTypes>; /** * Get all group names */ getGroupNames(): string[]; /** * Get all asset keys in a group */ getGroupKeys(groupName: string): Array<keyof AssetTypes>; } /** * Implementation of AssetConfigurator for builder pattern */ export declare class AssetConfiguratorImpl<Assets extends Record<string, unknown> = {}, AssetGroups extends string = never> implements AssetConfigurator<Assets, AssetGroups> { private readonly manager; constructor(manager: AssetManager<Assets, AssetGroups>); add<K extends string, T>(key: K, loader: () => Promise<T>): AssetConfigurator<Assets & Record<K, T>, AssetGroups>; addWithConfig<K extends string, T>(key: K, definition: AssetDefinition<T>): AssetConfigurator<Assets & Record<K, T>, AssetGroups>; addGroup<GN extends string, T extends Record<string, () => Promise<unknown>>>(groupName: GN, assets: T): AssetConfigurator<Assets & { [K in keyof T]: Awaited<ReturnType<T[K]>>; }, AssetGroups | GN>; /** * Get the underlying manager * @internal */ getManager(): AssetManager<Assets, AssetGroups>; } /** * Create a new AssetConfigurator for builder pattern usage */ export declare function createAssetConfigurator<Assets extends Record<string, unknown> = {}, AssetGroups extends string = never>(manager?: AssetManager<Assets, AssetGroups>): AssetConfiguratorImpl<Assets, AssetGroups>;