Compare commits

..

1 commit

Author SHA1 Message Date
Laud Boateng
9514138b10
Merge a2a9d080b9 into c8884c8eac 2024-12-27 18:20:10 -08:00

232
jecs.d.ts vendored
View file

@ -2,10 +2,7 @@
* A unique identifier in the world, entity. * A unique identifier in the world, entity.
* The generic type T defines the data type when this entity is used as a component * The generic type T defines the data type when this entity is used as a component
*/ */
export type Entity<TData = unknown> = number & { export type Entity<T = undefined | unknown> = number & { __jecs_value: T };
readonly __nominal_Entity: unique symbol;
readonly __type_TData: TData;
};
/** /**
* An entity with no associated data when used as a component * An entity with no associated data when used as a component
@ -13,66 +10,55 @@ export type Entity<TData = unknown> = number & {
export type Tag = Entity<undefined>; export type Tag = Entity<undefined>;
/** /**
* A pair of entities: * A pair of entities
* - `pred` is the type of the "predicate" entity. * P is the type of the predicate, O is the type of the object, and V is the type of the value (defaults to P)
* - `obj` is the type of the "object" entity.
*/ */
export type Pair<P = unknown, O = unknown> = number & { export type Pair<P = Entity, O = Entity> = number & {
readonly __nominal_Pair: unique symbol; __jecs_pair_pred: P;
readonly __pred: P; __jecs_pair_obj: O;
readonly __obj: O; __jecs_pair_value: P extends Entity<undefined>
? O extends Entity<infer V>
? V
: never
: P extends Entity<infer V> ? V : never
}; };
/**
* 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.
*/
export type Id<TData = unknown> = Entity<TData> | Pair<TData, unknown> | Pair<undefined, TData>;
export type InferComponent<E> = E extends Entity<infer D> /**
? D * Either an Entity or a Pair
: E extends Pair<infer P, infer O> */
? P extends undefined export type Id<T = unknown> = Entity<T> | Pair<Entity<T>, Entity<T>>;
? O
: P type InferComponent<E> = E extends Entity<infer T>
? T
: E extends Pair
? E["__jecs_pair_value"]
: never; : never;
type FlattenTuple<T extends unknown[]> = T extends [infer U] ? U : LuaTuple<T>; type FlattenTuple<T extends any[]> = T extends [infer U] ? U : LuaTuple<T>;
type Nullable<T extends unknown[]> = { [K in keyof T]: T[K] | undefined }; type Nullable<T extends unknown[]> = { [K in keyof T]: T[K] | undefined };
type InferComponents<A extends Id[]> = { [K in keyof A]: InferComponent<A[K]> }; type InferComponents<A extends Id[]> = {
[K in keyof A]: InferComponent<A[K]>;
};
type Iter<T extends unknown[]> = IterableFunction<LuaTuple<[Entity, ...T]>>; type Iter<T extends unknown[]> = IterableFunction<LuaTuple<[Entity, ...T]>>;
export type CachedQuery<T extends unknown[]> = {
/**
* Returns an iterator that produces a tuple of [Entity, ...queriedComponents].
*/
iter(): Iter<T>;
} & Iter<T>;
export type Query<T extends unknown[]> = { export type Query<T extends unknown[]> = {
/** /**
* Returns an iterator that produces a tuple of [Entity, ...queriedComponents]. * Returns an iterator that returns a tuple of an entity and queried components
*/ */
iter(): Iter<T>; iter(): Iter<T>;
/** /**
* Creates and returns a cached version of this query for efficient reuse. * Modifies the query to include specified components
* Call refinement methods (with/without) on the query before caching. * @param components The components to include
* @returns A cached query * @returns Modified 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.
*/ */
with(...components: Id[]): Query<T>; with(...components: Id[]): Query<T>;
/** /**
* Modifies the Query to exclude specified components. * Modifies the Query to exclude specified components
* @param components The components to exclude. * @param components The components to exclude
* @returns A new Query with the exclusion applied. * @returns Modified Query
*/ */
without(...components: Id[]): Query<T>; without(...components: Id[]): Query<T>;
/** /**
@ -83,127 +69,117 @@ export type Query<T extends unknown[]> = {
export class World { export class World {
/** /**
* Creates a new World. * Creates a new World
*/ */
constructor(); constructor();
/** /**
* Creates a new entity. * Creates a new entity
* @returns An entity (Tag) with no data. * @returns Entity
*/ */
entity(): Tag; entity(): Tag;
/** /**
* Creates a new entity in the first 256 IDs, typically used for static * Creates a new entity located in the first 256 ids.
* components that need fast access. * These should be used for static components for fast access.
* @returns A typed Entity with `TData`. * @returns Entity<T>
*/ */
component<TData = unknown>(): Entity<TData>; component<T = unknown>(): Entity<T>;
/** /**
* Gets the target of a relationship. For example, if we say * Gets the target of a relationship. For example, when a user calls
* `world.target(entity, ChildOf)`, this returns the parent entity. * `world.target(entity, ChildOf(parent))`, you will obtain the parent entity.
* @param entity The entity using a relationship pair. * @param entity Entity
* @param relation The "relationship" component/tag (e.g., ChildOf). * @param relation The Relationship
* @param index If multiple targets exist, specify an index. Defaults to 0. * @returns The Parent Entity if it exists
*/ */
target(entity: Entity, relation: Entity, index?: number): Entity | undefined; target(entity: Entity, relation: Entity): Entity | undefined;
/** /**
* Cleans up the world by removing empty archetypes and rebuilding the archetype collections. * Gets the target of a relationship at a specific index.
* This helps maintain memory efficiency by removing unused archetype definitions. * For example, when a user calls `world.target(entity, ChildOf(parent), 0)`,
* you will obtain the parent entity.
* @param entity Entity
* @param relation The Relationship
* @param index Target index
* @returns The Parent Entity if it exists
*/ */
cleanup(): void; target(entity: Entity, relation: Entity, index: number): Entity | undefined;
/** /**
* Clears all components and relationships from the given entity, but * Clears an entity from the world
* does not delete the entity from the world. * @param entity Entity to be cleared
* @param entity The entity to clear.
*/ */
clear(entity: Entity): void; clear(entity: Entity): void;
/** /**
* Deletes an entity (and its components/relationships) from the world entirely. * Deletes an entity and all its related components and relationships
* @param entity The entity to delete. * @param entity Entity to be destroyed
*/ */
delete(entity: Entity): void; delete(entity: Entity): void;
/** /**
* Adds a component (with no value) to the entity. * Adds a component to the entity with no value
* @param entity The target entity. * @param entity Target Entity
* @param component The component (or tag) to add. * @param component Component
*/ */
add(entity: Entity, component: Id<undefined>): void; add(entity: Entity, component: Id): void;
/** /**
* Assigns a value to a component on the given entity. * Assigns a value to a component on the given entity
* @param entity The target entity. * @param entity Target Entity
* @param component The component definition (could be a Pair or Entity). * @param component Target Component
* @param value The value to store with that component. * @param value Component Value
*/ */
set<E extends Id<unknown>>(entity: Entity, component: E, value: InferComponent<E>): void; set<E extends Id<unknown>>(entity: Entity, component: E, value: InferComponent<E>): void;
/** /**
* Removes a component from the given entity. * Removes a component from the given entity
* @param entity The target entity. * @param entity Target Entity
* @param component The component to remove. * @param component Target Component
*/ */
remove(entity: Entity, component: Id): void; remove(entity: Entity, component: Id): void;
/** /**
* Retrieves the values of up to 4 components on a given entity. Missing * Retrieves the values of specified components for an entity.
* components will return `undefined`. * Some values may not exist when called.
* @param entity The entity to query. * A maximum of 4 components are allowed at a time.
* @param components Up to 4 components/tags to retrieve. * @param id Target Entity
* @returns A tuple of data (or a single value), each possibly undefined. * @param components Target Components
* @returns Data associated with target components if it exists.
*/ */
get<T extends [Id] | [Id, Id] | [Id, Id, Id] | [Id, Id, Id, Id]>( get<T extends [Id] | [Id, Id] | [Id, Id, Id] | [Id, Id, Id, Id]>(id: Entity, ...components: T): FlattenTuple<Nullable<InferComponents<T>>>;
entity: Entity,
...components: T
): FlattenTuple<Nullable<InferComponents<T>>>;
/** /**
* Returns `true` if the given entity has all of the specified components. * Returns whether the entity has the specified components.
* A maximum of 4 components can be checked at once. * A maximum of 4 components are allowed at a time.
* @param entity The entity to check. * @param entity Target Entity
* @param components Upto 4 components to check for. * @param components Target Components
* @returns If the entity contains the components
*/ */
has(entity: Entity, ...components: Id[]): boolean; has(entity: Entity, ...components: Id[]): boolean;
/** /**
* Checks if an entity exists in the world. * Checks if an entity exists in the world
* @param entity The entity to verify. * @param entity Entity to check
* @returns Whether the entity exists in the world
*/ */
contains(entity: Entity): boolean; contains(entity: Entity): boolean;
/** /**
* Gets the parent (the target of a `ChildOf` relationship) for an entity, * Get parent (target of ChildOf relationship) for entity.
* if such a relationship exists. * If there is no ChildOf relationship pair, it will return undefined.
* @param entity The entity whose parent is queried. * @param entity Target Entity
* @returns Parent Entity or undefined
*/ */
parent(entity: Entity): Entity | undefined; parent(entity: Entity): Entity | undefined;
/** /**
* Searches the world for entities that match specified components. * Searches the world for entities that match a given query
* @param components The list of components to query. * @param components Queried Components
* @returns A Query object to iterate over results. * @returns Query
*/ */
query<T extends Id[]>(...components: T): Query<InferComponents<T>>; query<T extends Id[]>(...components: T): Query<InferComponents<T>>;
/**
* 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>;
} }
/** /**
@ -212,7 +188,7 @@ export class World {
* @param obj The second entity (object) * @param obj The second entity (object)
* @returns The composite key (pair) * @returns The composite key (pair)
*/ */
export function pair<P, O>(pred: Entity<P>, obj: Entity<O>): Pair<P, O>; export function pair<P, O>(pred: Entity<P>, obj: Entity<O>): Pair<Entity<P>, Entity<O>>;
/** /**
* Checks if the entity is a composite key (pair) * Checks if the entity is a composite key (pair)
@ -226,24 +202,24 @@ export function IS_PAIR(value: Id): value is Pair;
* @param pair The pair to get the first entity from * @param pair The pair to get the first entity from
* @returns The first entity (predicate) of the pair * @returns The first entity (predicate) of the pair
*/ */
export function pair_first<P, O>(world: World, p: Pair<P, O>): Entity<P>; export function pair_first<P, O>(pair: Pair<P, O>): Entity<P>;
/** /**
* Gets the second entity (object) of a pair * Gets the second entity (object) of a pair
* @param pair The pair to get the second entity from * @param pair The pair to get the second entity from
* @returns The second entity (object) of the pair * @returns The second entity (object) of the pair
*/ */
export function pair_second<P, O>(world: World, p: Pair<P, O>): Entity<O>; export function pair_second<P, O>(pair: Pair<P, O>): Entity<O>;
export declare const OnAdd: Entity<(e: Entity) => void>; export const OnAdd: Entity<(e: Entity) => void>;
export declare const OnRemove: Entity<(e: Entity) => void>; export const OnRemove: Entity<(e: Entity) => void>;
export declare const OnSet: Entity<(e: Entity, value: unknown) => void>; export const OnSet: Entity<(e: Entity, value: unknown) => void>;
export declare const ChildOf: Entity; export const ChildOf: Entity;
export declare const Wildcard: Entity; export const Wildcard: Entity;
export declare const w: Entity; export const w: Entity;
export declare const OnDelete: Entity; export const OnDelete: Entity;
export declare const OnDeleteTarget: Entity; export const OnDeleteTarget: Entity;
export declare const Delete: Entity; export const Delete: Entity;
export declare const Remove: Entity; export const Remove: Entity;
export declare const Name: Entity<string>; export const Name: Entity<string>;
export declare const Rest: Entity; export const Rest: Entity;