2024-10-12 01:45:37 +00:00
|
|
|
/**
|
|
|
|
* A unique identifier in the world, entity.
|
|
|
|
* The generic type T defines the data type when this entity is used as a component
|
|
|
|
*/
|
2025-01-02 09:39:29 +00:00
|
|
|
export type Entity<TData = unknown> = number & {
|
|
|
|
readonly __nominal_Entity: unique symbol;
|
|
|
|
readonly __type_TData: TData;
|
|
|
|
};
|
2024-10-12 01:45:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An entity with no associated data when used as a component
|
|
|
|
*/
|
|
|
|
export type Tag = Entity<undefined>;
|
|
|
|
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* A pair of entities:
|
|
|
|
* - `pred` is the type of the "predicate" entity.
|
|
|
|
* - `obj` is the type of the "object" entity.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
2025-01-02 09:39:29 +00:00
|
|
|
export type Pair<P = unknown, O = unknown> = number & {
|
|
|
|
readonly __nominal_Pair: unique symbol;
|
|
|
|
readonly __pred: P;
|
|
|
|
readonly __obj: O;
|
2024-10-12 01:45:37 +00:00
|
|
|
};
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* An `Id` can be either a single Entity or a Pair of Entities.
|
|
|
|
* By providing `TData`, you can specifically require an Id that yields that type.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
2025-01-02 09:39:29 +00:00
|
|
|
export type Id<TData = unknown> = Entity<TData> | Pair<TData, unknown> | Pair<undefined, TData>;
|
|
|
|
|
|
|
|
export type InferComponent<E> = E extends Entity<infer D>
|
|
|
|
? D
|
|
|
|
: E extends Pair<infer P, infer O>
|
2025-03-02 00:24:53 +00:00
|
|
|
? P extends undefined
|
|
|
|
? O
|
|
|
|
: P
|
|
|
|
: never;
|
2024-10-12 01:45:37 +00:00
|
|
|
|
2025-01-02 09:39:29 +00:00
|
|
|
type FlattenTuple<T extends unknown[]> = T extends [infer U] ? U : LuaTuple<T>;
|
2024-10-12 01:45:37 +00:00
|
|
|
type Nullable<T extends unknown[]> = { [K in keyof T]: T[K] | undefined };
|
2025-01-02 09:39:29 +00:00
|
|
|
type InferComponents<A extends Id[]> = { [K in keyof A]: InferComponent<A[K]> };
|
2024-10-12 01:45:37 +00:00
|
|
|
|
2025-03-02 00:24:53 +00:00
|
|
|
type ArchetypeId = number;
|
2025-01-31 14:29:32 +00:00
|
|
|
type Column = unknown[];
|
|
|
|
|
|
|
|
export type Archetype = {
|
2025-03-02 00:24:53 +00:00
|
|
|
id: number;
|
|
|
|
types: number[];
|
|
|
|
type: string;
|
|
|
|
entities: number[];
|
|
|
|
columns: Column[];
|
|
|
|
records: number[];
|
|
|
|
counts: number[];
|
|
|
|
};
|
2025-01-31 14:29:32 +00:00
|
|
|
|
2024-10-12 01:45:37 +00:00
|
|
|
type Iter<T extends unknown[]> = IterableFunction<LuaTuple<[Entity, ...T]>>;
|
|
|
|
|
2025-01-02 09:39:29 +00:00
|
|
|
export type CachedQuery<T extends unknown[]> = {
|
|
|
|
/**
|
|
|
|
* Returns an iterator that produces a tuple of [Entity, ...queriedComponents].
|
|
|
|
*/
|
|
|
|
iter(): Iter<T>;
|
2025-01-31 14:29:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the matched archetypes of the query
|
|
|
|
* @returns An array of archetypes of the query
|
|
|
|
*/
|
|
|
|
archetypes(): Archetype[];
|
2025-01-02 09:39:29 +00:00
|
|
|
} & Iter<T>;
|
|
|
|
|
2024-10-12 01:45:37 +00:00
|
|
|
export type Query<T extends unknown[]> = {
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Returns an iterator that produces a tuple of [Entity, ...queriedComponents].
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
|
|
|
iter(): Iter<T>;
|
|
|
|
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Creates and returns a cached version of this query for efficient reuse.
|
|
|
|
* Call refinement methods (with/without) on the query before caching.
|
|
|
|
* @returns A cached query
|
|
|
|
*/
|
|
|
|
cached(): CachedQuery<T>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modifies the query to include specified components.
|
|
|
|
* @param components The components to include.
|
|
|
|
* @returns A new Query with the inclusion applied.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
|
|
|
with(...components: Id[]): Query<T>;
|
|
|
|
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Modifies the Query to exclude specified components.
|
|
|
|
* @param components The components to exclude.
|
|
|
|
* @returns A new Query with the exclusion applied.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
|
|
|
without(...components: Id[]): Query<T>;
|
2025-01-31 14:29:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the matched archetypes of the query
|
|
|
|
* @returns An array of archetypes of the query
|
|
|
|
*/
|
|
|
|
archetypes(): Archetype[];
|
2024-10-12 01:45:37 +00:00
|
|
|
} & Iter<T>;
|
|
|
|
|
|
|
|
export class World {
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Creates a new World.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
|
|
|
constructor();
|
|
|
|
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Creates a new entity.
|
|
|
|
* @returns An entity (Tag) with no data.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
|
|
|
entity(): Tag;
|
|
|
|
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Creates a new entity in the first 256 IDs, typically used for static
|
|
|
|
* components that need fast access.
|
|
|
|
* @returns A typed Entity with `TData`.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
2025-01-02 09:39:29 +00:00
|
|
|
component<TData = unknown>(): Entity<TData>;
|
2024-10-12 01:45:37 +00:00
|
|
|
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Gets the target of a relationship. For example, if we say
|
|
|
|
* `world.target(entity, ChildOf)`, this returns the parent entity.
|
|
|
|
* @param entity The entity using a relationship pair.
|
|
|
|
* @param relation The "relationship" component/tag (e.g., ChildOf).
|
|
|
|
* @param index If multiple targets exist, specify an index. Defaults to 0.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
2025-01-02 09:39:29 +00:00
|
|
|
target(entity: Entity, relation: Entity, index?: number): Entity | undefined;
|
2024-10-12 01:45:37 +00:00
|
|
|
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Cleans up the world by removing empty archetypes and rebuilding the archetype collections.
|
|
|
|
* This helps maintain memory efficiency by removing unused archetype definitions.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
2025-01-02 09:39:29 +00:00
|
|
|
cleanup(): void;
|
2024-10-12 01:45:37 +00:00
|
|
|
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Clears all components and relationships from the given entity, but
|
|
|
|
* does not delete the entity from the world.
|
|
|
|
* @param entity The entity to clear.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
|
|
|
clear(entity: Entity): void;
|
|
|
|
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Deletes an entity (and its components/relationships) from the world entirely.
|
|
|
|
* @param entity The entity to delete.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
|
|
|
delete(entity: Entity): void;
|
|
|
|
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Adds a component (with no value) to the entity.
|
|
|
|
* @param entity The target entity.
|
|
|
|
* @param component The component (or tag) to add.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
2025-01-02 09:39:29 +00:00
|
|
|
add(entity: Entity, component: Id<undefined>): void;
|
2024-10-12 01:45:37 +00:00
|
|
|
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Assigns a value to a component on the given entity.
|
|
|
|
* @param entity The target entity.
|
|
|
|
* @param component The component definition (could be a Pair or Entity).
|
|
|
|
* @param value The value to store with that component.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
|
|
|
set<E extends Id<unknown>>(entity: Entity, component: E, value: InferComponent<E>): void;
|
|
|
|
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Removes a component from the given entity.
|
|
|
|
* @param entity The target entity.
|
|
|
|
* @param component The component to remove.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
|
|
|
remove(entity: Entity, component: Id): void;
|
|
|
|
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Retrieves the values of up to 4 components on a given entity. Missing
|
|
|
|
* components will return `undefined`.
|
|
|
|
* @param entity The entity to query.
|
|
|
|
* @param components Up to 4 components/tags to retrieve.
|
|
|
|
* @returns A tuple of data (or a single value), each possibly undefined.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
2025-01-02 09:39:29 +00:00
|
|
|
get<T extends [Id] | [Id, Id] | [Id, Id, Id] | [Id, Id, Id, Id]>(
|
|
|
|
entity: Entity,
|
|
|
|
...components: T
|
|
|
|
): FlattenTuple<Nullable<InferComponents<T>>>;
|
2024-10-12 01:45:37 +00:00
|
|
|
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Returns `true` if the given entity has all of the specified components.
|
|
|
|
* A maximum of 4 components can be checked at once.
|
|
|
|
* @param entity The entity to check.
|
|
|
|
* @param components Upto 4 components to check for.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
|
|
|
has(entity: Entity, ...components: Id[]): boolean;
|
|
|
|
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Checks if an entity exists in the world.
|
|
|
|
* @param entity The entity to verify.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
|
|
|
contains(entity: Entity): boolean;
|
|
|
|
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Gets the parent (the target of a `ChildOf` relationship) for an entity,
|
|
|
|
* if such a relationship exists.
|
|
|
|
* @param entity The entity whose parent is queried.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
|
|
|
parent(entity: Entity): Entity | undefined;
|
|
|
|
|
|
|
|
/**
|
2025-01-02 09:39:29 +00:00
|
|
|
* Searches the world for entities that match specified components.
|
|
|
|
* @param components The list of components to query.
|
|
|
|
* @returns A Query object to iterate over results.
|
2024-10-12 01:45:37 +00:00
|
|
|
*/
|
|
|
|
query<T extends Id[]>(...components: T): Query<InferComponents<T>>;
|
2025-01-02 09:39:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an iterator that yields all entities that have the specified component or relationship.
|
|
|
|
* @param id The component or relationship ID to search for
|
|
|
|
* @returns An iterator function that yields entities
|
|
|
|
*/
|
|
|
|
each(id: Id): IterableFunction<Entity>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an iterator that yields all child entities of the specified parent entity.
|
|
|
|
* Uses the ChildOf relationship internally.
|
|
|
|
* @param parent The parent entity to get children for
|
|
|
|
* @returns An iterator function that yields child entities
|
|
|
|
*/
|
|
|
|
children(parent: Entity): IterableFunction<Entity>;
|
2024-10-12 01:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a composite key (pair)
|
|
|
|
* @param pred The first entity (predicate)
|
|
|
|
* @param obj The second entity (object)
|
|
|
|
* @returns The composite key (pair)
|
|
|
|
*/
|
2025-01-02 09:39:29 +00:00
|
|
|
export function pair<P, O>(pred: Entity<P>, obj: Entity<O>): Pair<P, O>;
|
2024-10-12 01:45:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the entity is a composite key (pair)
|
|
|
|
* @param value The entity to check
|
|
|
|
* @returns If the entity is a pair
|
|
|
|
*/
|
|
|
|
export function IS_PAIR(value: Id): value is Pair;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the first entity (predicate) of a pair
|
|
|
|
* @param pair The pair to get the first entity from
|
|
|
|
* @returns The first entity (predicate) of the pair
|
|
|
|
*/
|
2025-01-02 09:39:29 +00:00
|
|
|
export function pair_first<P, O>(world: World, p: Pair<P, O>): Entity<P>;
|
2024-10-12 01:45:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the second entity (object) of a pair
|
|
|
|
* @param pair The pair to get the second entity from
|
|
|
|
* @returns The second entity (object) of the pair
|
|
|
|
*/
|
2025-01-02 09:39:29 +00:00
|
|
|
export function pair_second<P, O>(world: World, p: Pair<P, O>): Entity<O>;
|
|
|
|
|
|
|
|
export declare const OnAdd: Entity<(e: Entity) => void>;
|
|
|
|
export declare const OnRemove: Entity<(e: Entity) => void>;
|
2025-04-04 22:36:04 +00:00
|
|
|
export declare const OnChange: Entity<(e: Entity, value: unknown) => void>;
|
2025-01-20 08:50:42 +00:00
|
|
|
export declare const ChildOf: Tag;
|
2025-01-02 09:39:29 +00:00
|
|
|
export declare const Wildcard: Entity;
|
|
|
|
export declare const w: Entity;
|
2025-01-20 08:50:42 +00:00
|
|
|
export declare const OnDelete: Tag;
|
|
|
|
export declare const OnDeleteTarget: Tag;
|
|
|
|
export declare const Delete: Tag;
|
|
|
|
export declare const Remove: Tag;
|
2025-01-02 09:39:29 +00:00
|
|
|
export declare const Name: Entity<string>;
|
|
|
|
export declare const Rest: Entity;
|