import { ComponentClassesMap } from './component-classes.js'; import { ComponentMap } from './component-map.js'; import { ComponentIterator } from './iterator.js'; import { ComponentQuery } from './query.js'; import { type Component, type ComponentClass, type ComponentClasses, type SingleOrArray } from './types.js'; /** * Class for storing entities and their relationships. * @category Maps */ export declare class EntityMap { /** * Registered component classes that contain the component data */ components: ComponentClassesMap; private nextId; private freeIds; /** * Registers component classes with the {@link EntityMap} * @param componentClasses - One or more component classes to register * @returns The {@link EntityMap} instance for chaining * @throws {@link ComponentTypeKeyMissing} when the specified component type is missing a 'name' parameter (e.g. anonymous classes) * @throws {@link ComponentAlreadyRegistered} when the specified component is already registered * @example * // component class * class MyComponent { * constructor(x) { * this.x = x; * } * } * * ecs.register(MyComponent); * // or multiple * ecs.register(MyComponent1, MyComponent2); */ register[]>(...componentClasses: TComponentClasses): this; /** * Gets a component class map * @param component - The component class to get the map for * @returns The {@link ComponentMap} for the specified component, or undefined if not found * @throws {@link ComponentNotRegistered} when the specified component is not registered * @example * const positionMap = ecs.getMap(Position) * for(const [entityId, position] of positionMap) { * position.x += 1 * } */ getMap(component: ComponentClass): ComponentMap | undefined; /** * @deprecated Use {@link EntityMap.firstEntity} instead * * Returns an array of component data for the first entity associated with the keyComponent * @param keyComponent - The component class used to find the first entity * @returns An array of components for the first entity, or undefined if not found * @throws {@link ComponentNotRegistered} when the specified component is not registered * @example * // get the first entity * const playerEntity = ecs.first(Player) ?? [] */ first(keyComponent: ComponentClass): Component[] | undefined; /** * Returns an array of component data for the first entity associated with the keyComponent * @param keyComponent - The component class used to find the first entity * @returns An array of components for the first entity, or undefined if not found * @throws {@link ComponentNotRegistered} when the specified component is not registered * @example * // get the first entity * const playerEntity = ecs.firstEntity(Player) ?? [] */ firstEntity(keyComponent: ComponentClass): Component[] | undefined; /** * Returns an array of component data arrays associated with the keyComponent * @param keyComponent - The component class used to find the entities * @returns An array of component arrays, or undefined if the key component map is not found * @throws {@link ComponentNotRegistered} when the specified component is not registered * @example * // get an array of component arrays: e.g. [[Player, Position, Velocity], [Player, Position, Velocity], ...] * const entities = ecs.entityValues(Player) ?? [] */ entityValues(keyComponent: ComponentClass): Array | undefined; /** * Gets the first entity entry for a component class * @param keyComponent - The component class to find the first entry for * @param components - Additional component classes to retrieve for the same entity * @returns A tuple containing the entity id followed by the component data, or undefined if not found * @throws {@link ComponentNotRegistered} when the specified component is not registered * @example * // return the first entry * const [entityId, player] = ecs.firstEntry(Player) ?? [] * * // or return multiple related components in addition to the first entry * const [entityId, player, position, direction] = ecs.firstEntry( * Player, * Position, * Direction * ) ?? [] */ firstEntry(keyComponent: ComponentClass, ...components: ComponentClasses): [number, TKey, ...TRelated] | undefined; /** * Gets the first entity id for a component class * and optionally any related component data * @param keyComponent - The component class used to find the first entity * @param components - Additional component classes to retrieve for the same entity * @returns If only keyComponent is provided, returns the entity id. * * If related components are provided, returns a tuple with the id and component data. * * Returns undefined if not found. * @throws {@link ComponentNotRegistered} when any of specified component(s) are not registered * @example * // return the first entity id * const entityId = ecs.firstKey(Player) * * // or return multiple related component in addition to entity id * const [entityId, position, direction] = ecs.firstKey( * Player, * Position, * Direction * ) ?? [] */ firstKey(keyComponent: TKey, ...components: ComponentClasses): SingleOrArray<[number, ...TRelated]> | undefined; /** * Gets the first entity component data for a component class * and optionally any related component data * @param keyComponent - The component class used to find the first entity * @param components - Additional component classes to retrieve for the same entity * @returns If only keyComponent is provided, returns the component data. * * If related components are provided, returns a tuple with all component data. * * Returns undefined if not found. * @throws {@link ComponentNotRegistered} when any of specified component(s) are not registered * @example * // return the first component data * const player = ecs.firstValue(Player) * * // or multiple related data in addition to the first component * const [player, position, direction] = ecs.firstValue( * Player, * Position, * Direction * ) ?? [] */ firstValue(keyComponent: ComponentClass, ...components: ComponentClasses): SingleOrArray<[TKey, ...TRelated]> | undefined; /** * Internal helper to get component data for an entity * @param entityId - The entity id to get the component for * @param component - The component class to get the data of * @returns The component data if it exists, otherwise undefined * @throws {@link ComponentNotRegistered} when the specified component is not registered */ private getEntity; /** * Gets component data related to an entity id * @param entityId - The entity id to get component(s) for * @param components - One or more component classes to retrieve. If none provided, retrieves all components for the entity. * @returns Depending on parameters: a single component, an array of specified components, or an array of all components for the entity. * @throws {@link ComponentNotRegistered} when any of specified component(s) are not registered * @example * // get one by id * const player = ecs.get(entityId, Player) * * // get multiple by id * const [player, position] = ecs.get(entityId, Player, Position) ?? [] * * // get all by id * const playerEntity = ecs.get(entityId) ?? [] */ get(entityId: number): Component[] | undefined; get(entityId: number, component: ComponentClass): T | undefined; get(entityId: number, ...components: ComponentClasses): SingleOrArray | undefined; /** * Check if a component exists for an entity * @param entityId - The entity id to check for the component * @param component - The component class to check * @returns True if the entity has the component, otherwise false * @throws {@link ComponentNotRegistered} when the specified component is not registered * @example * const exists = ecs.has(entityId, Position) */ has(entityId: number, component: ComponentClass): boolean; /** * Checks if all of the specified components exist for an entity * @param entityId - The entity id to check * @param components - One or more component classes to check for * @returns True if the entity has ALL specified components, otherwise false * @throws {@link ComponentNotRegistered} when any of the specified component(s) are not registered * @example * const hasAll = ecs.hasAll(entityId, Position, Velocity) */ hasAll[]>(entityId: number, ...components: T): boolean; /** * Checks if any of the specified components exist for an entity * @param entityId - The entity id to check * @param components - One or more component classes to check for * @returns True if the entity has ANY of the specified components, otherwise false * @throws {@link ComponentNotRegistered} when any of the specified component(s) are not registered * @example * const hasAny = ecs.hasAny(entityId, Position, Velocity) */ hasAny[]>(entityId: number, ...components: T): boolean; /** * Internal helper to set component data for an entity * @param entityId - The entity id to set the component for * @param componentData - The component data to set * @returns The component data that was set * @throws {@link ComponentNotRegistered} when the component class of the instance is not registered */ private setEntity; /** * Add or update multiple component data for an entity * @param entityId - The entity id to set components for * @param component - A single component data instance to set * @param components - Multiple component data instances to set * @returns The single component data or an array of component data that were set * @throws {@link ComponentNotRegistered} when any of the component classes are not registered * @example * // set one * const player = ecs.set(entityId, new Player()); * * // or set multiple * const [player, position] = ecs.set( * entityId, * new Player(), * new Position() * ); */ set(entityId: number, component: T): T; set(entityId: number, ...components: T): T; /** * Removes the specified component(s) from an entity * @param entityId - The entity id to remove components from * @param components - One or more component classes to remove * @throws {@link ComponentNotRegistered} when any of the specified component(s) are not registered * @example * ecs.remove(entityId, Position); */ remove[]>(entityId: number, ...components: T): void; /** * Removes the specified component from an entity * @param entityId - The entity id to remove the component from * @param component - The component class to remove * @returns True if the component was successfully removed, otherwise false * @throws {@link ComponentNotRegistered} when the specified component is not registered * @example * ecs.removeByKey(entityId, Position); */ removeByKey(entityId: number, component: ComponentClass): boolean; /** * Deletes all components from an entity and reclaims the id for reuse * @param entityIds - One or more entity ids to destroy * @returns The total number of components destroyed * @example * const destroyedCount = ecs.destroyEntity(entityId1) * * // or multiple * const destroyedCount = ecs.destroyEntity(entityId1, entityId2) */ destroyEntity(...entityIds: number[]): number; /** * Creates a new entity id for the EntityMap. * * Reuses Ids from destroyed entities otherwise increments the Id counter. * @returns A new unique entity id * @example * const newEntityId = ecs.getNextId() * ecs.set(newEntityId, new Player()) */ getNextId(): number; /** * Clears all registered component classes, all entity data and all reclaimed IDs * @returns The {@link EntityMap} instance for chaining * @example * ecs.clear() */ clear(): this; /** * Clears all component data and reclaimed IDs while keeping the registered component classes * @returns The {@link EntityMap} instance for chaining * @example * ecs.clearComponents() */ clearComponents(): this; /** * Iterates over each entity that contains the specified key component * @param keyComponent - The primary component class used to filter entities * @param components - Additional component classes to retrieve for each entity * @returns A {@link ComponentIterator} that yields tuples of [entityId, keyComponentData, ...relatedComponentData] * @throws {@link ComponentNotRegistered} when any of specified component(s) are not registered * @example * // iterate each component data that is related to the Player entity * const iterator = ecs.iterator(Player, Position) * * for(const [playerId, player, position] of iterator) { } * * // you can also declare the type of iterator before it's assigned * // using the ComponentIterator type * let iterator: ComponentIterator<[Player, Position]> * * // then with late bound assignment (keeping the iterator intellisense) * iterator = ecs.iterator(Player, Position) * * for(const [playerId, player, position] of iterator) { * const moving = player.isMoving * } */ iterator(keyComponent: ComponentClass, ...components: ComponentClasses): ComponentIterator; /** * Creates a query that can be stored and reused * @param keyComponent - The primary component class used to filter entities * @param components - Additional component classes to retrieve for each entity * @returns A {@link ComponentQuery} instance * @throws {@link ComponentNotRegistered} when any of specified component(s) are not registered * @example * const query = ecs.query(Player, Position) * * // get the first entry * const [playerId, player, position] = query.firstEntry() ?? [] * * // get the first key * const [playerId, position] = query.firstKey() ?? [] * * // get the first value * const [player, position] = query.firstValue() ?? [] * * // iterate * for (const [playerId, player, position] of query) { * * } */ query(keyComponent: ComponentClass, ...components: ComponentClasses): ComponentQuery; /** * Prints all component maps in a tabular format to the console. * * Additional generated columns are: * * 'Entity.Key' is the entity id * * 'Entity.Type' is the component name * * @param components - Optional filter. Only includes specific component class names. * @param properties - Optional filter. Specifies which property columns to display in the tables. * @returns The {@link EntityMap} instance for chaining */ printTable(components?: string[]): this; printTable(components?: string[], properties?: string[]): this; /** * Prints all component data for the specified entity id in a tabular format to the console * * Additional generated columns are: * * 'Entity.Type' is the component name * * @param entityId - The entity id to print * @param properties - Optional filter. Specifies which property columns to display in the tables. * @returns The {@link EntityMap} instance for chaining */ printEntity(entityId: number, properties?: string[]): this; /** * Parses the JSON and returns an {@link EntityMap} object * @param json - The JSON string representing an {@link EntityMap} * @returns A restored {@link EntityMap} instance * @example * const json = JSON.stringify(ecs); * const restoredMap = EntityMap.parse(json); */ static parse(json: string): EntityMap; /** * A tracing method used for debugging. * Intercepts all functions specified and logs each call to the console. * @param funcFilter - A list of function names you want to intercept. If no function names are specified then will log all functions called. * @returns A new proxy of an {@link EntityMap} with tracing enabled * @example * // trace all method calls * const ecs = EntityMap.createWithTracing(); * * // trace only 'set' and 'remove' calls * const ecs = EntityMap.createWithTracing(['set', 'remove']); */ static createWithTracing(funcFilter?: string[]): any; } //# sourceMappingURL=entity-map.d.ts.map