2024-09-09 19:44:45 +00:00
|
|
|
/**
|
|
|
|
* Represents an entity
|
|
|
|
* The generic type T defines the data type when this entity is used as a component
|
|
|
|
*/
|
|
|
|
export type Entity<T = unknown> = number & { __T: T };
|
|
|
|
|
|
|
|
export type Pair = number;
|
|
|
|
|
|
|
|
export type Id<T = unknown> = Entity<T> | Pair;
|
|
|
|
|
|
|
|
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 InferComponents<A extends Id[]> = { [K in keyof A]: A[K] extends Id<infer T> ? T : never };
|
|
|
|
type TupleForWorldGet = [Id] | [Id, Id] | [Id, Id, Id] | [Id, Id, Id, Id];
|
|
|
|
|
|
|
|
type Item<T extends unknown[]> = (this: Query<T>) => LuaTuple<[Entity, ...T]>;
|
|
|
|
|
|
|
|
export type Query<T extends unknown[]> = {
|
2024-09-12 14:25:13 +00:00
|
|
|
/**
|
|
|
|
* Get the next result in the query. Drain must have been called beforehand or otherwise it will error.
|
|
|
|
*/
|
|
|
|
next: Item<T>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the Iterator for a query.
|
|
|
|
*/
|
|
|
|
drain: (this: Query<T>) => Query<T>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modifies the query to include specified components, but will not include the values.
|
|
|
|
* @param components The components to include
|
|
|
|
* @returns Modified Query
|
|
|
|
*/
|
|
|
|
with: (this: Query<T>, ...components: Id[]) => Query<T>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modifies the Query to exclude specified components
|
|
|
|
* @param components The components to exclude
|
|
|
|
* @returns Modified Query
|
|
|
|
*/
|
|
|
|
without: (this: Query<T>, ...components: Id[]) => Query<T>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modifies component data with a callback function
|
|
|
|
* @param fn The function to modify data
|
|
|
|
*/
|
|
|
|
replace: (this: Query<T>, fn: (...components: T) => FlattenTuple<T>) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the archetypes associated with this query.
|
|
|
|
*/
|
|
|
|
archetypes: () => Archetype[];
|
2024-06-15 20:03:04 +00:00
|
|
|
} & IterableFunction<LuaTuple<[Entity, ...T]>>;
|
|
|
|
|
2024-09-09 19:44:45 +00:00
|
|
|
export type Archetype = {
|
2024-09-12 14:25:13 +00:00
|
|
|
id: number;
|
|
|
|
edges: { [key: number]: ArchetypeEdge };
|
|
|
|
types: number[];
|
|
|
|
type: string | number;
|
|
|
|
entities: number[];
|
|
|
|
columns: unknown[][];
|
|
|
|
records: { [key: number]: ArchetypeRecord };
|
2024-06-15 20:03:04 +00:00
|
|
|
};
|
2024-09-09 19:44:45 +00:00
|
|
|
|
|
|
|
type ArchetypeRecord = {
|
2024-09-12 14:25:13 +00:00
|
|
|
count: number;
|
|
|
|
column: number;
|
2024-06-15 20:03:04 +00:00
|
|
|
};
|
2024-08-05 15:49:18 +00:00
|
|
|
|
2024-09-09 19:44:45 +00:00
|
|
|
type ArchetypeEdge = {
|
2024-09-12 14:25:13 +00:00
|
|
|
add: Archetype;
|
|
|
|
remove: Archetype;
|
2024-09-09 19:44:45 +00:00
|
|
|
};
|
2024-06-15 20:03:04 +00:00
|
|
|
|
|
|
|
export class World {
|
2024-09-12 14:25:13 +00:00
|
|
|
/**
|
|
|
|
* Creates a new World
|
|
|
|
*/
|
|
|
|
constructor();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new entity
|
|
|
|
* @returns Entity
|
|
|
|
*/
|
|
|
|
entity(): Entity;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new entity located in the first 256 ids.
|
|
|
|
* These should be used for static components for fast access.
|
|
|
|
* @returns Entity<T>
|
|
|
|
*/
|
|
|
|
component<T = unknown>(): Entity<T>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the target of a relationship. For example, when a user calls
|
|
|
|
* `world.target(entity, ChildOf(parent))`, you will obtain the parent entity.
|
|
|
|
* @param entity Entity
|
2024-09-23 12:29:53 +00:00
|
|
|
* @param index Target index
|
2024-09-12 14:25:13 +00:00
|
|
|
* @param relation The Relationship
|
|
|
|
* @returns The Parent Entity if it exists
|
|
|
|
*/
|
|
|
|
target(entity: Entity, relation: Entity, index: number): Entity | undefined;
|
|
|
|
|
2024-09-23 12:29:53 +00:00
|
|
|
/**
|
|
|
|
* Gets the target of a relationship. For example, when a user calls
|
|
|
|
* `world.target(entity, ChildOf(parent))`, you will obtain the parent entity.
|
|
|
|
* @param entity Entity
|
|
|
|
* @param relation The Relationship
|
|
|
|
* @returns The Parent Entity if it exists
|
|
|
|
*/
|
|
|
|
target(entity: Entity, relation: Entity): Entity | undefined;
|
|
|
|
|
2024-09-12 14:25:13 +00:00
|
|
|
/**
|
|
|
|
* Clears an entity from the world.
|
|
|
|
* @param entity Entity to be cleared
|
|
|
|
*/
|
|
|
|
clear(entity: Entity): void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes an entity and all its related components and relationships.
|
|
|
|
* @param entity Entity to be destroyed
|
|
|
|
*/
|
|
|
|
delete(entity: Entity): void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a component to the entity with no value
|
|
|
|
* @param entity Target Entity
|
|
|
|
* @param component Component
|
|
|
|
*/
|
|
|
|
add<T>(entity: Entity, component: Id<T>): void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assigns a value to a component on the given entity
|
|
|
|
* @param entity Target Entity
|
|
|
|
* @param component Target Component
|
|
|
|
* @param data Component Data
|
|
|
|
*/
|
2024-09-28 13:36:34 +00:00
|
|
|
set<T>(entity: Entity, component: Id<T>, data: NoInfer<T>): void;
|
2024-09-12 14:25:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a component from the given entity
|
|
|
|
* @param entity Target Entity
|
|
|
|
* @param component Target Component
|
|
|
|
*/
|
|
|
|
remove(entity: Entity, component: Id): void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the values of specified components for an entity.
|
|
|
|
* Some values may not exist when called.
|
|
|
|
* A maximum of 4 components are allowed at a time.
|
|
|
|
* @param id Target Entity
|
|
|
|
* @param components Target Components
|
|
|
|
* @returns Data associated with target components if it exists.
|
|
|
|
*/
|
|
|
|
get<T extends TupleForWorldGet>(id: Entity, ...components: T): FlattenTuple<Nullable<InferComponents<T>>>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the entity has the specified components.
|
|
|
|
* A maximum of 4 components are allowed at a time.
|
|
|
|
* @param entity Target Entity
|
|
|
|
* @param components Target Components
|
|
|
|
* @returns If the entity contains the components
|
|
|
|
*/
|
|
|
|
has(entity: Entity, ...components: Id[]): boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if an entity exists in the world
|
|
|
|
* @param entity Entity to check
|
|
|
|
* @returns Whether the entity exists in the world
|
|
|
|
*/
|
|
|
|
contains(entity: Entity): boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Searches the world for entities that match a given query
|
|
|
|
* @param components Queried Components
|
|
|
|
* @returns Query
|
|
|
|
*/
|
|
|
|
query<T extends Id[]>(...components: T): Query<InferComponents<T>>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get parent (target of ChildOf relationship) for entity.
|
|
|
|
* If there is no ChildOf relationship pair, it will return undefined.
|
|
|
|
* @param entity Target Entity
|
|
|
|
* @returns Parent Entity or undefined
|
|
|
|
*/
|
|
|
|
parent(entity: Entity): Entity | undefined;
|
2024-06-15 20:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a composite key.
|
|
|
|
* @param pred The first entity
|
|
|
|
* @param obj The second entity
|
|
|
|
* @returns The composite key
|
|
|
|
*/
|
2024-09-12 14:25:13 +00:00
|
|
|
export function pair(pred: Entity, obj: Entity): Pair;
|
2024-06-15 20:03:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the entity is a composite key
|
|
|
|
* @param e The entity to check
|
|
|
|
* @returns If the entity is a pair
|
|
|
|
*/
|
2024-09-09 19:44:45 +00:00
|
|
|
export function IS_PAIR(e: Id): boolean;
|
2024-06-15 20:03:04 +00:00
|
|
|
|
2024-09-09 19:44:45 +00:00
|
|
|
/** Built-in Component used to find every component id */
|
2024-07-07 20:29:30 +00:00
|
|
|
export const Component: Entity;
|
|
|
|
|
2024-09-07 20:42:35 +00:00
|
|
|
export const OnAdd: Entity<(e: Entity) => void>;
|
|
|
|
export const OnRemove: Entity<(e: Entity) => void>;
|
|
|
|
export const OnSet: Entity<(e: Entity, value: unknown) => void>;
|
2024-09-09 19:44:45 +00:00
|
|
|
export const ChildOf: Entity;
|
|
|
|
export const Wildcard: Entity;
|
|
|
|
export const w: Entity;
|
2024-09-07 20:42:35 +00:00
|
|
|
export const OnDelete: Entity;
|
2024-09-09 19:44:45 +00:00
|
|
|
export const OnDeleteTarget: Entity;
|
2024-08-17 00:38:00 +00:00
|
|
|
export const Delete: Entity;
|
2024-09-07 20:42:35 +00:00
|
|
|
export const Remove: Entity;
|
2024-09-09 19:44:45 +00:00
|
|
|
export const Name: Entity<string>;
|
2024-08-03 20:22:36 +00:00
|
|
|
export const Rest: Entity;
|