-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecs.ts
100 lines (100 loc) · 3.16 KB
/
ecs.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
export class Component {
[key: string]: any;
constructor(components: {
[key: string]: any
}) {
Array.from(Object.keys((this.constructor as any).schema))
.forEach((key: string) => {
if (components.hasOwnProperty(key))
this[key] = components[key];
});
};
};
export class ComponentManager {
public components: Map < Component, Component > ;
constructor() {
this.components = new Map();
};
};
export class Entity {
static schema: {
[key: string]: any;
};
public componentManager: ComponentManager;
constructor() {
this.componentManager = new ComponentManager();
};
};
export class EntityManager {
public entities: Set < Entity > ;
constructor() {
this.entities = new Set();
};
};
export class Query {
public componentManager: ComponentManager;
public entityManager: EntityManager;
constructor() {
this.componentManager = new ComponentManager();
this.entityManager = new EntityManager();
};
};
export class QueryManager {
public queries: Map < any, any > // Query fix before commit
constructor() {
this.queries = new Map();
};
};
export class System {
static queryManager: QueryManager = new QueryManager();
public queryManager: QueryManager;
public constructor() {
this.queryManager = new QueryManager();
};
public execute(): void {};
};
export class SystemManager {
public systems: Set < System > ;
constructor() {
this.systems = new Set();
};
};
export class World {
public systemManager: SystemManager;
public entityManager: EntityManager;
public componentManager: ComponentManager;
constructor() {
this.systemManager = new SystemManager();
this.entityManager = new EntityManager();
this.componentManager = new ComponentManager();
};
public registerEntity(entity: Entity): void {
this.systemManager.systems.forEach((system: System) => {
Array.from((system.constructor as unknown as System).queryManager.queries.keys())
.forEach((query: Query) => {
Array.from(system.queryManager.queries.get(query).componentManager.components.keys()).forEach((component) => {
if (entity.componentManager.components.has(component))
if (system.queryManager.queries.get(query).entityManager.entities.has(entity) === false)
system.queryManager.queries.get(query).entityManager.entities.add(entity);
});
});
});
}
public unregisterEntity(entity: Entity): void {
this.systemManager.systems.forEach((system: System) => {
Array.from((system.constructor as unknown as System).queryManager.queries.keys())
.forEach((query: Query) => {
Array.from(system.queryManager.queries.get(query).componentManager.components.keys()).forEach((component) => {
if (entity.componentManager.components.has(component))
if (system.queryManager.queries.get(query).entityManager.entities.has(entity) === true)
system.queryManager.queries.get(query).entityManager.entities.delete(entity);
});
});
});
}
public execute(): void {
this.systemManager.systems.forEach((system: System) => {
system.execute();
});
}
};