mirror of
				https://github.com/Ukendio/jecs.git
				synced 2025-11-04 02:49:18 +00:00 
			
		
		
		
	fixed typescript types (#115)
This commit is contained in:
		
							parent
							
								
									411138e1f7
								
							
						
					
					
						commit
						8e0a9409f5
					
				
					 1 changed files with 164 additions and 126 deletions
				
			
		
							
								
								
									
										112
									
								
								src/index.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										112
									
								
								src/index.d.ts
									
									
									
									
										vendored
									
									
								
							| 
						 | 
				
			
			@ -1,54 +1,76 @@
 | 
			
		|||
type Query<T extends unknown[]> = {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * this: Query<T> is necessary to use a colon instead of a period for emits.
 | 
			
		||||
/**
 | 
			
		||||
 * 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[]> = {
 | 
			
		||||
	/**
 | 
			
		||||
	 * Get the next result in the query. Drain must have been called beforehand or otherwise it will error.
 | 
			
		||||
	 */
 | 
			
		||||
    next: (this: Query<T>) => Query<T>;
 | 
			
		||||
	next: Item<T>;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Resets the Iterator for a query.
 | 
			
		||||
	 */
 | 
			
		||||
    drain: (this: Query<T>) => Query<T>
 | 
			
		||||
	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: Entity[]) => Query<T>
 | 
			
		||||
	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: Entity[]) => Query<T>;
 | 
			
		||||
	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[];
 | 
			
		||||
} & IterableFunction<LuaTuple<[Entity, ...T]>>;
 | 
			
		||||
 | 
			
		||||
// Utility Types
 | 
			
		||||
export type Entity<T = unknown> = number & { __T: T };
 | 
			
		||||
export type EntityType<T> = T extends Entity<infer A> ? A : never;
 | 
			
		||||
export type InferComponents<A extends Entity[]> = {
 | 
			
		||||
    [K in keyof A]: EntityType<A[K]>;
 | 
			
		||||
export type Archetype = {
 | 
			
		||||
	id: number;
 | 
			
		||||
	edges: { [key: number]: ArchetypeEdge };
 | 
			
		||||
	types: number[];
 | 
			
		||||
	type: string | number;
 | 
			
		||||
	entities: number[];
 | 
			
		||||
	columns: unknown[][];
 | 
			
		||||
	records: { [key: number]: ArchetypeRecord };
 | 
			
		||||
};
 | 
			
		||||
type Nullable<T extends unknown[]> = {
 | 
			
		||||
    [K in keyof T]: T[K] | undefined;
 | 
			
		||||
};
 | 
			
		||||
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]
 | 
			
		||||
type ArchetypeRecord = {
 | 
			
		||||
	count: number;
 | 
			
		||||
	column: number;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
type ArchetypeEdge = {
 | 
			
		||||
	add: Archetype;
 | 
			
		||||
	remove: Archetype;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export class World {
 | 
			
		||||
	/**
 | 
			
		||||
| 
						 | 
				
			
			@ -80,7 +102,7 @@ export class World {
 | 
			
		|||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Clears an entity from the world.
 | 
			
		||||
     * @praram entity Entity to be cleared
 | 
			
		||||
	 * @param entity Entity to be cleared
 | 
			
		||||
	 */
 | 
			
		||||
	clear(entity: Entity): void;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -95,7 +117,7 @@ export class World {
 | 
			
		|||
	 * @param entity Target Entity
 | 
			
		||||
	 * @param component Component
 | 
			
		||||
	 */
 | 
			
		||||
    add<T>(entity: Entity, component: Entity<T>): void;
 | 
			
		||||
	add<T>(entity: Entity, component: Id<T>): void;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Assigns a value to a component on the given entity
 | 
			
		||||
| 
						 | 
				
			
			@ -103,14 +125,14 @@ export class World {
 | 
			
		|||
	 * @param component Target Component
 | 
			
		||||
	 * @param data Component Data
 | 
			
		||||
	 */
 | 
			
		||||
    set<T>(entity: Entity, component: Entity<T>, data: T): void;
 | 
			
		||||
	set<T>(entity: Entity, component: Id<T>, data: T): void;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Removes a component from the given entity
 | 
			
		||||
	 * @param entity Target Entity
 | 
			
		||||
	 * @param component Target Component
 | 
			
		||||
	 */
 | 
			
		||||
    remove(entity: Entity, component: Entity): void;
 | 
			
		||||
	remove(entity: Entity, component: Id): void;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Retrieves the values of specified components for an entity.
 | 
			
		||||
| 
						 | 
				
			
			@ -120,7 +142,7 @@ export class World {
 | 
			
		|||
	 * @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>>>
 | 
			
		||||
	get<T extends TupleForWorldGet>(id: Entity, ...components: T): FlattenTuple<Nullable<InferComponents<T>>>;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Returns whether the entity has the specified components.
 | 
			
		||||
| 
						 | 
				
			
			@ -129,14 +151,29 @@ export class World {
 | 
			
		|||
	 * @param components Target Components
 | 
			
		||||
	 * @returns If the entity contains the components
 | 
			
		||||
	 */
 | 
			
		||||
    has<T extends TupleForWorldGet>(entity: Entity, ...components: T): boolean;
 | 
			
		||||
	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 Entity[]>(...components: T): Query<InferComponents<T>>;
 | 
			
		||||
	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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
| 
						 | 
				
			
			@ -145,27 +182,28 @@ export class World {
 | 
			
		|||
 * @param obj The second entity
 | 
			
		||||
 * @returns The composite key
 | 
			
		||||
 */
 | 
			
		||||
export const pair: (pred: Entity, obj: Entity) => Entity;
 | 
			
		||||
export function pair<R, T>(pred: Entity<R>, obj: Entity<T>): Pair;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 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;
 | 
			
		||||
export function IS_PAIR(e: Id): boolean;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Built-in Component used to find every component id
 | 
			
		||||
 */
 | 
			
		||||
/** Built-in Component used to find every component id */
 | 
			
		||||
export const Component: Entity;
 | 
			
		||||
 | 
			
		||||
export const OnAdd: Entity<(e: Entity) => void>;
 | 
			
		||||
export const OnRemove: Entity<(e: Entity) => void>;
 | 
			
		||||
export const OnSet: Entity<(e: Entity, value: unknown) => void>;
 | 
			
		||||
export const OnDeleteTarget: Entity;
 | 
			
		||||
export const ChildOf: Entity;
 | 
			
		||||
export const Wildcard: Entity;
 | 
			
		||||
export const w: Entity;
 | 
			
		||||
export const OnDelete: Entity;
 | 
			
		||||
export const OnDeleteTarget: Entity;
 | 
			
		||||
export const Delete: Entity;
 | 
			
		||||
export const Remove: Entity;
 | 
			
		||||
export const Tag: Entity;
 | 
			
		||||
export const Wildcard: Entity;
 | 
			
		||||
export const Name: Entity<string>;
 | 
			
		||||
export const Rest: Entity;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue