-
Notifications
You must be signed in to change notification settings - Fork 2
/
ecstatic.d.ts
382 lines (302 loc) · 9.24 KB
/
ecstatic.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* eslint-disable @typescript-eslint/no-explicit-any */
export type Tag = string | number;
export type ClassConstructor<T> = { new (...args: any[]): T };
export type EntityId = string;
export type EntityState =
| "creating"
| "created"
| "destroying"
| "destroyed"
| "error";
export class Entity<CT> {
get id(): string;
get world(): World<CT>;
/**
* Get the current state of the entity.
*/
get state(): EntityState;
constructor(world: World<CT>);
/**
* Add a component to an Entity
*/
add<T extends CT>(component: T): this;
/**
* Add a tag to a component
*/
addTag(tag: Tag): this;
/**
* Check to see if the entity has a specific component.
*/
has<T extends CT>(cType: ClassConstructor<T>): boolean;
/**
* Check to see if an entity tagged with a given tag.
*/
hasTag(tag: Tag): boolean;
/**
* Get a component that belongs to an entity.
*/
get<T extends CT>(cl: ClassConstructor<T>): T;
/**
* Get all components that have been added to an entity, via a ComponentCollection
*/
getAll(): ComponentCollection<CT>;
/**
* Get all components that have been added to an entity, via a ComponentCollection.
* Does the same thing as entityInstance.getAll().
*/
get components(): ComponentCollection<CT>;
/**
* Retrieves all the tags that have been added to this entity.
*/
get tags(): Set<Tag>;
/**
* Remove a component from an entity.
* @param cType A component class, eg MyComponent
*/
remove(cType: ClassConstructor<CT>): this;
/**
* Remove a tag from an entity
*/
removeTag(tag: Tag): this;
/**
* Clears all components from an Entity
*/
clear(): this;
/**
* Remove all tags on an entity
*/
clearTags(): this;
/**
* Sets the state of the entity to 'created'. that's it.
*/
finishCreation(): void;
/**
* Destroy an entity. Actual destruction is deferred until after the next pass of systems.
* This gives the systems a chance to do any cleanup that might be needed.
*/
destroy(): void;
/**
* Immediately destroy an entity. does not defer until after a systems pass like entity.destroy() does.
*/
destroyImmediately(): void;
/**
* Convert Entity to a DevEntity. Very helpful in for debugging.
*/
toDevEntity(): DevEntity<CT>;
}
export interface DevEntityTableRow {
id: string;
components: string;
tags: string;
systems: string;
}
export class DevEntity<CT> {
id: string;
components: Record<string, CT>;
tags: Tag[];
systems: string[];
constructor(entity: Entity<CT>, world: World<CT>);
toTableRow(): DevEntityTableRow;
}
interface DevSystemComps {
system: string;
components: string;
}
declare class DevTools<CT> {
world: World<CT>;
constructor(world: World<CT>);
/**
* display the all systems of the world, and the components required by each system.
* Super helpful to use with console.table()
* @example
* ```
* console.table(world.dev.systemComponents);
* ```
*/
get systemComponents(): DevSystemComps[];
/**
* Create an array of DevEntites. Can be very helpful for things like inspecting component state,
* and which systems will be called on an entity.
* @example
* ```
* console.table(world.dev.entities);
*
* // Pro tip! try displaying a table of entities with console.table and DevEntity.toTableRow().
* console.table(world.dev.entities.map(devEntity => devEntity.toTableRow()));
* ```
*/
get entities(): DevEntity<CT>[];
}
export class ComponentCollection<CT> {
components: Map<string, CT>;
add(component: CT): void;
update<T extends CT>(
cl: ClassConstructor<CT>,
func: (c: T) => T
): void;
/**
* Remove a component.
* @param cType Class of component to remove.
*/
remove(cType: ClassConstructor<CT>): void;
/**
* Get a component that matches the passed class.
* Will throw an error if an instance of the given component
* doesn't exist in the collection, so if you don't know if it's safe
* to get a component, you should test with has() or hasByName() first.
* You have been warned.
* @param cl component Class reference.
*/
get<T extends CT>(cl: ClassConstructor<T>): T;
/**
* Test to see if the collection contains a specific Class or Classes.
* @param cType component Class, or array of component Classes.
*/
has(cType: ClassConstructor<CT> | ClassConstructor<CT>[]): boolean;
/**
* Test to see if the collection has a component instance based on a
* class name. Some build steps/minifiers will change the name of Classes,
* so it's usually best to pass in a MyClass.name instead of 'MyClass'.
* @param cName The name of a Class, or array of Class names.
*/
hasByName(cName: string | string[]): boolean;
/**
* Get the component type names that are currently being used in the collection.
*/
get componentTypes(): string[];
/**
* Get the current number of components that are in the collection.
*/
get size(): number;
toDevComponents(): Record<string, CT>;
}
export type System = () => void;
export interface SystemFuncArgs<CT> {
entity: Entity<CT>;
components: ComponentCollection<CT>;
world: World<CT>;
index: number;
size: number;
isFirst: boolean;
isLast: boolean;
}
export type SystemFunc<CT> = (
args: SystemFuncArgs<CT>
) => void;
declare class Systems<CT> {
world: World<CT>;
systemFuncBySystemName: Map<string, SystemFunc<CT>>;
compNamesBySystemName: Map<string, string[]>;
constructor(world: World<CT>);
add(cTypes: ClassConstructor<CT>[], systemFunc: SystemFunc<CT>, funcName?: string): this;
run(): void;
}
declare class World<CT> {
componentCollections: Map<EntityId, ComponentCollection<CT>>;
entities: Map<EntityId, Entity<CT>>;
entitiesByCTypes: Map<string[], Set<EntityId>>;
entitiesByTags: Map<Tag, Set<EntityId>>;
/**
* Provides access to systems added to the world.
* Exposes the all important `world.systems.run()` method, so you can run your systems.
*/
systems: Systems<CT>;
/**
* Lots of cool things to help view the state of the world. Check it out!
*/
dev: DevTools<CT>;
/**
* "finds" a single entity based on a predicate
*/
find: (predicate: (entity: Entity<CT>) => boolean) => Entity<CT> | null;
/**
* "finds" all entities based on a predicate, kinda like filter.
*/
findAll: (predicate: (entity: Entity<CT>) => boolean) => Entity<CT>[];
/**
* "locates" a single entity based on its Components.
*/
locate: (cl: ClassConstructor<CT> | ClassConstructor<CT>[]) => Entity<CT> | null;
/**
* Locates all entities that contain the components named
*/
locateAll: (cl: ClassConstructor<CT> | ClassConstructor<CT>[]) => Entity<CT>[];
/**
* Grabs the first entity, and its related component, that matches the component type.
* @example
* ```
* const { entity, component } = world.grab(MyComponent);
* ```
*/
grab: <T extends CT>(
cl: ClassConstructor<T>
) => { entity: Entity<CT>; component: T } | null;
/**
* Grab single component based on component type and predicate.
*
* @example
* ```typescript
* const { entity, component } = world.grabBy(FirstComponent, (comp) => comp.id == 'awesome')
* ```
*/
grabBy: <T extends CT>(
cl: ClassConstructor<T>,
predicate: (comp: T) => boolean
) => { entity: Entity<CT>; component: T } | null;
/**
* Grab all the components primarily, and the entities if needed
*/
grabAll: <T extends CT>(
cl: ClassConstructor<T>
) => { entity: Entity<CT>; component: T }[];
/**
* Given an entity id and componentType, returns component
*/
get: <T extends CT>(eid: EntityId, cl: ClassConstructor<T>) => T;
/**
* Find and get the first instance of a component, without any associated entities.
* Helpful is you know that only one instance of a component exists across all entities.
* @param cl Component Class Contructor
* @param defaultValue A default component instance if no components are found.
*/
getComponent<T extends CT>(cl: ClassConstructor<T>): T | null;
getComponent<T extends CT>(cl: ClassConstructor<T>, defaultValue?: T): T;
/**
* Get an entity that has been tagged with the given tag, or return null;
*/
getTagged(tag: Tag): Entity<CT> | null;
/**
* Get all entities that have been given a tag.
*/
getAllTagged(tag: Tag): Entity<CT>[];
/**
* Add a component on the given entity
*/
add: <T extends CT>(eid: EntityId, component: T) => this;
/**
* Remove a component from the given entity.
* NOTE: This will change what systems will be called on the entity.
*/
remove: (eid: EntityId, cType: ClassConstructor<CT>) => this;
/**
* Add a system to the world.
*/
addSystem(cTypes: ClassConstructor<CT>[], systemFunc: SystemFunc<CT>, funcName?: string): this;
/**
* Setup an entity to exist in the given world. This is mostly an internal method, but exposed just in case.
*/
registerEntity(entity: Entity<CT>): World<CT>;
/**
* Remove all components from a given entity
*/
clearEntityComponents(entityId: EntityId): this;
/**
* Create an entity that is in the world.
* Basically just new Entity(world), but saves an import of Entity.
*/
createEntity(): Entity<CT>;
/**
* Destroys an entity
*/
destroyEntity(entityId: EntityId): this;
}