-
Notifications
You must be signed in to change notification settings - Fork 29
/
jecs.d.ts
213 lines (185 loc) · 6.16 KB
/
jecs.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* A unique identifier in the world, entity.
* The generic type T defines the data type when this entity is used as a component
*/
export type Entity<T = undefined | unknown> = number & { __jecs_value: T };
/**
* An entity with no associated data when used as a component
*/
export type Tag = Entity<undefined>;
/**
* A pair of entities
* 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)
*/
export type Pair<P = undefined, O = undefined, V = P> = number & {
__jecs_pair_pred: P;
__jecs_pair_obj: O;
__jecs_pair_value: V;
};
/**
* Either an Entity or a Pair
*/
export type Id<T = unknown> = Entity<T> | Pair<unknown, unknown, T>;
type InferComponent<E> = E extends Id<infer T> ? T : never;
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]: InferComponent<A[K]>;
};
type TupleForWorldGet = [Id] | [Id, Id] | [Id, Id, Id] | [Id, Id, Id, Id];
type Iter<T extends unknown[]> = IterableFunction<LuaTuple<[Entity, ...T]>>;
export type Query<T extends unknown[]> = {
/**
* Returns an iterator that returns a tuple of an entity and queried components
*/
iter(): Iter<T>;
/**
* Modifies the query to include specified components
* @param components The components to include
* @returns Modified Query
*/
with(...components: Id[]): Query<T>;
/**
* Modifies the Query to exclude specified components
* @param components The components to exclude
* @returns Modified Query
*/
without(...components: Id[]): Query<T>;
} & Iter<T>;
export class World {
/**
* Creates a new World
*/
constructor();
/**
* Creates a new entity
* @returns Entity
*/
entity(): Tag;
/**
* 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
* @param relation The Relationship
* @returns The Parent Entity if it exists
*/
target(entity: Entity, relation: Entity): Entity | undefined;
/**
* Gets the target of a relationship at a specific index.
* 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
*/
target(entity: Entity, relation: Entity, index: number): Entity | undefined;
/**
* 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(entity: Entity, component: Id): void;
/**
* Assigns a value to a component on the given entity
* @param entity Target Entity
* @param component Target Component
* @param value Component Value
*/
set<E extends Id<unknown>>(entity: Entity, component: E, value: InferComponent<E>): void;
/**
* 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;
/**
* 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;
/**
* 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>>;
}
/**
* Creates a composite key (pair)
* @param pred The first entity (predicate)
* @param obj The second entity (object)
* @returns The composite key (pair)
*/
export function pair<P, O, V = P>(pred: Entity<P>, obj: Entity<O>): Pair<P, O, V>;
/**
* 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
*/
export function pair_first<P, O, V = P>(pair: Pair<P, O, V>): Entity<P>;
/**
* 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
*/
export function pair_second<P, O, V = P>(pair: Pair<P, O, V>): Entity<O>;
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 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 Name: Entity<string>;
export const Rest: Entity;