2024-06-15 20:03:04 +00:00
|
|
|
type Query<T extends unknown[]> = {
|
2024-07-07 20:29:30 +00:00
|
|
|
|
2024-08-03 20:22:36 +00:00
|
|
|
/**
|
|
|
|
* this: Query<T> is necessary to use a colon instead of a period for emits.
|
|
|
|
*/
|
2024-08-05 15:49:18 +00:00
|
|
|
|
2024-08-12 12:36:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next result in the query. Drain must have been called beforehand or otherwise it will error.
|
|
|
|
*/
|
|
|
|
next: (this: Query<T>) => Query<T>;
|
2024-08-05 15:49:18 +00:00
|
|
|
/**
|
|
|
|
* Resets the Iterator for a query.
|
|
|
|
*/
|
2024-08-03 20:22:36 +00:00
|
|
|
drain: (this: Query<T>) => Query<T>
|
2024-08-05 15:49:18 +00:00
|
|
|
/**
|
|
|
|
* Modifies the query to include specified components, but will not include the values.
|
|
|
|
* @param components The components to include
|
|
|
|
* @returns Modified Query
|
|
|
|
*/
|
2024-08-03 20:22:36 +00:00
|
|
|
with: (this: Query<T>, ...components: Entity[]) => Query<T>
|
|
|
|
/**
|
|
|
|
* Modifies the Query to exclude specified components
|
|
|
|
* @param components The components to exclude
|
|
|
|
* @returns Modified Query
|
|
|
|
*/
|
|
|
|
without: (this: Query<T>, ...components: Entity[]) => Query<T>;
|
|
|
|
/**
|
|
|
|
* Modifies component data with a callback function
|
|
|
|
* @param fn The function to modify data
|
|
|
|
*/
|
2024-08-05 15:49:18 +00:00
|
|
|
replace: (this: Query<T>, fn: (...components: T) => FlattenTuple<T>) => void;
|
2024-06-15 20:03:04 +00:00
|
|
|
} & IterableFunction<LuaTuple<[Entity, ...T]>>;
|
|
|
|
|
|
|
|
// Utility Types
|
2024-08-17 00:38:00 +00:00
|
|
|
export type Entity<T = unknown> = number & { __T: T };
|
2024-06-15 20:03:04 +00:00
|
|
|
export type EntityType<T> = T extends Entity<infer A> ? A : never;
|
|
|
|
export type InferComponents<A extends Entity[]> = {
|
2024-08-03 20:22:36 +00:00
|
|
|
[K in keyof A]: EntityType<A[K]>;
|
2024-06-15 20:03:04 +00:00
|
|
|
};
|
|
|
|
type Nullable<T extends unknown[]> = {
|
2024-08-03 20:22:36 +00:00
|
|
|
[K in keyof T]: T[K] | undefined;
|
2024-06-15 20:03:04 +00:00
|
|
|
};
|
2024-08-05 15:49:18 +00:00
|
|
|
type FlattenTuple<T extends any[]> = T extends [infer U] ? U : LuaTuple<T>;
|
|
|
|
|
|
|
|
// Utility type for world:get
|
|
|
|
type TupleForWorldGet =
|
|
|
|
| [Entity]
|
|
|
|
| [Entity, Entity]
|
|
|
|
| [Entity, Entity, Entity]
|
|
|
|
| [Entity, Entity, Entity, Entity]
|
2024-06-15 20:03:04 +00:00
|
|
|
|
|
|
|
export class World {
|
2024-08-03 20:22:36 +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
|
2024-08-12 12:36:48 +00:00
|
|
|
* `world.target(entity, ChildOf(parent))`, you will obtain the parent entity.
|
|
|
|
* @param entity Entity
|
2024-08-03 20:22:36 +00:00
|
|
|
* @param relation The Relationship
|
|
|
|
* @returns The Parent Entity if it exists
|
|
|
|
*/
|
2024-08-12 12:36:48 +00:00
|
|
|
target(entity: Entity, relation: Entity): Entity | undefined;
|
2024-08-03 20:22:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears an entity from the world.
|
2024-08-12 12:36:48 +00:00
|
|
|
* @praram entity Entity to be cleared
|
2024-08-03 20:22:36 +00:00
|
|
|
*/
|
2024-08-12 12:36:48 +00:00
|
|
|
clear(entity: Entity): void;
|
2024-08-03 20:22:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes an entity and all its related components and relationships.
|
2024-08-12 12:36:48 +00:00
|
|
|
* @param entity Entity to be destroyed
|
2024-08-03 20:22:36 +00:00
|
|
|
*/
|
2024-08-12 12:36:48 +00:00
|
|
|
delete(entity: Entity): void;
|
2024-08-03 20:22:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a component to the entity with no value
|
2024-08-12 12:36:48 +00:00
|
|
|
* @param entity Target Entity
|
2024-08-03 20:22:36 +00:00
|
|
|
* @param component Component
|
|
|
|
*/
|
2024-08-12 12:36:48 +00:00
|
|
|
add<T>(entity: Entity, component: Entity<T>): void;
|
2024-08-03 20:22:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assigns a value to a component on the given entity
|
2024-08-12 12:36:48 +00:00
|
|
|
* @param entity Target Entity
|
2024-08-03 20:22:36 +00:00
|
|
|
* @param component Target Component
|
|
|
|
* @param data Component Data
|
|
|
|
*/
|
2024-08-12 12:36:48 +00:00
|
|
|
set<T>(entity: Entity, component: Entity<T>, data: T): void;
|
2024-08-03 20:22:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a component from the given entity
|
2024-08-12 12:36:48 +00:00
|
|
|
* @param entity Target Entity
|
2024-08-03 20:22:36 +00:00
|
|
|
* @param component Target Component
|
|
|
|
*/
|
2024-08-12 12:36:48 +00:00
|
|
|
remove(entity: Entity, component: Entity): void;
|
2024-08-03 20:22:36 +00:00
|
|
|
|
|
|
|
/**
|
2024-08-05 15:49:18 +00:00
|
|
|
* Retrieves the values of specified components for an entity.
|
|
|
|
* Some values may not exist when called.
|
2024-08-12 12:36:48 +00:00
|
|
|
* A maximum of 4 components are allowed at a time.
|
2024-08-03 20:22:36 +00:00
|
|
|
* @param id Target Entity
|
2024-08-05 15:49:18 +00:00
|
|
|
* @param components Target Components
|
|
|
|
* @returns Data associated with target components if it exists.
|
2024-08-03 20:22:36 +00:00
|
|
|
*/
|
2024-08-05 15:49:18 +00:00
|
|
|
get<T extends TupleForWorldGet>(id: Entity, ...components: T): FlattenTuple<Nullable<InferComponents<T>>>
|
2024-08-03 20:22:36 +00:00
|
|
|
|
2024-08-12 12:36:48 +00:00
|
|
|
/**
|
|
|
|
* 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<T extends TupleForWorldGet>(entity: Entity, ...components: T): boolean;
|
|
|
|
|
2024-08-03 20:22:36 +00:00
|
|
|
/**
|
|
|
|
* Searches the world for entities that match a given query
|
|
|
|
* @param components Queried Components
|
|
|
|
* @returns Query
|
|
|
|
*/
|
|
|
|
query<T extends Entity[]>(...components: T): Query<InferComponents<T>>;
|
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
|
|
|
|
*/
|
|
|
|
export const pair: (pred: Entity, obj: Entity) => Entity;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the entity is a composite key
|
|
|
|
* @param e The entity to check
|
|
|
|
* @returns If the entity is a pair
|
|
|
|
*/
|
|
|
|
export const IS_PAIR: (e: Entity) => boolean;
|
|
|
|
|
2024-07-07 20:29:30 +00:00
|
|
|
/**
|
|
|
|
* Built-in Component used to find every component id
|
|
|
|
*/
|
|
|
|
export const Component: Entity;
|
|
|
|
|
2024-06-15 20:03:04 +00:00
|
|
|
export const OnAdd: Entity;
|
|
|
|
export const OnRemove: Entity;
|
|
|
|
export const OnSet: Entity;
|
2024-08-17 00:38:00 +00:00
|
|
|
export const OnDeleteTarget: Entity;
|
|
|
|
export const Delete: Entity;
|
2024-06-15 20:03:04 +00:00
|
|
|
export const Wildcard: Entity;
|
2024-08-03 20:22:36 +00:00
|
|
|
export const Rest: Entity;
|