-
Notifications
You must be signed in to change notification settings - Fork 12
/
ecs-world.ts
65 lines (57 loc) · 1.68 KB
/
ecs-world.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
import {type IWorldBuilder, WorldBuilder} from "../world/world-builder.ts";
import {SerDe} from "../serde/serde.ts";
import type {IPreptimeWorld} from "../world/preptime/preptime-world.spec.ts";
import type {IRuntimeWorld} from "../world/runtime/runtime-world.spec.ts";
import type {ISystem} from "../system/system.spec.ts";
import {PreptimeWorld} from "../world/preptime/preptime-world.ts";
const worlds = new Set<IPreptimeWorld | IRuntimeWorld>();
/**
* Register a world
* @param world
*/
export function addWorld(world: Readonly<IPreptimeWorld | IRuntimeWorld>) {
worlds.add(world);
}
/**
* Build a new world and automatically add it to the list of worlds inside the ECS
*/
export function buildWorld(): IWorldBuilder {
const serde = new SerDe();
return new WorldBuilder(serde).addCallback(world => worlds.add(world));
}
/**
* Get a world with a name
* @param name
*/
export function getWorld(name: string): IPreptimeWorld | IRuntimeWorld | undefined {
let world;
for (world of worlds) {
if (world.name == name) {
return world;
}
}
}
/**
* Iterate over all registered worlds
*/
export function getWorlds(): IterableIterator<IPreptimeWorld | IRuntimeWorld> {
return worlds.values();
}
export function hmrSwapSystem(system: ISystem): void {
let world;
let ptWorld;
for (world of worlds) {
if (world instanceof PreptimeWorld) {
for (ptWorld of world.getExistingRuntimeWorlds()) {
ptWorld.hmrReplaceSystem(system);
}
}
}
}
/**
* Remove a world
* @param world
*/
export function removeWorld(world: Readonly<IPreptimeWorld | IRuntimeWorld>) {
worlds.delete(world);
}