mani-game-engine 1.0.0-pre.5 → 1.0.0-pre.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/entity.ts CHANGED
@@ -1,116 +1,202 @@
1
- import {GameEngine} from './gameEngine';
2
- import {Signal} from 'mani-signal';
3
- import {Class} from './types';
4
- import {entityComponents} from './ecsInjector';
5
-
6
- let idCounter = 0;
7
-
8
- export class Entity {
9
-
10
- readonly id: number;
11
- readonly children = new Set<Entity>();
12
- private gameEngine?: GameEngine;
13
-
14
- // TODO: use single rootEntity class instead of undefined?
15
- private _parent?: Entity;
16
- readonly parentChanged = new Signal();
17
- readonly onRemove = new Signal();
18
- private markRemoval = false;
19
-
20
- constructor() {
21
- this.id = idCounter++;
22
- }
23
-
24
- async onBeforeRemove?(): Promise<void>;
25
-
26
- private setParent(parent: Entity | undefined) {
27
- if (parent === this._parent) {
28
- //TODO: i think this check can be removed
29
- throw Error('same parent');
30
- }
31
- this._parent = parent;
32
- this.parentChanged.dispatch();
33
- }
34
-
35
- get parent(): Entity | undefined {
36
- return this._parent;
37
- }
38
-
39
- getComponent<T extends Class>(componentClass: T): InstanceType<T> | undefined {
40
- const componentMap = entityComponents.get(this.constructor as Class);
41
- if (!componentMap) {
42
- throw new Error(`No components in entity of type '${this.constructor.name}'.`);
43
- }
44
- const key = componentMap.get(componentClass as T);
45
- return key && (<any>this)[key];
46
- }
47
-
48
- async add(entity: Entity) {
49
- if (entity.parent === this) {
50
- throw new Error('Entity is already a child');
51
- }
52
- if (entity === this) {
53
- throw new Error('Could not add Entity as a child of itself.');
54
- }
55
-
56
- if (entity.parent) {
57
- entity.parent.children.delete(entity);
58
- }
59
- this.children.add(entity);
60
- entity.setParent(this);
61
-
62
- if (entity.gameEngine) {
63
- if (this.gameEngine !== entity.gameEngine) {
64
- throw new Error('cant add an active to a passive entity');
65
- }
66
- } else {
67
- if (this.gameEngine) {
68
-
69
- await this.gameEngine.add(entity);
70
- }
71
- }
72
-
73
- // TODO: systems should be informed so they can do stuff (renderer system should add object 3d etc...)
74
-
75
- if (this.gameEngine && this.gameEngine !== entity.gameEngine) {
76
-
77
- }
78
- }
79
-
80
- async remove(entity: Entity) {
81
- if (!this.children.delete(entity)) {
82
- throw new Error('Could not remove. Entity is not a child.');
83
- }
84
- entity.setParent(undefined);
85
- if (this.gameEngine) {
86
- return await this.gameEngine.remove(entity);
87
- }
88
- }
89
-
90
- async detach() {
91
- if (this.parent) {
92
- this.parent.remove(this);
93
- } else {
94
- if (!this.gameEngine) {
95
- throw new Error('entity isn\'t attached to a parent or the game engine');
96
- }
97
- await this.gameEngine.remove(this);
98
- }
99
- }
100
-
101
- has(entity: Entity) {
102
- return this.children.has(entity);
103
- }
104
-
105
- /**
106
- * this includes the entity itself
107
- * @param target
108
- */
109
- getAllEntities(target: Entity[] = []) {
110
- target.push(this);
111
- for (const child of this.children) {
112
- child.getAllEntities(target);
113
- }
114
- return target;
115
- }
116
- }
1
+ import {GameEngine} from './gameEngine';
2
+ import {Signal} from 'mani-signal';
3
+ import {Class} from './types';
4
+ import {ID, putIfAbsent} from './injector';
5
+ import {EcsInjector} from './ecsInjector';
6
+
7
+ let idCounter = 0;
8
+
9
+ export class Entity {
10
+
11
+ readonly children = new Set<Entity>();
12
+ private gameEngine?: GameEngine;
13
+
14
+ get isAdded() {
15
+ return !!this.gameEngine;
16
+ }
17
+
18
+ // TODO: use single rootEntity class instead of undefined?
19
+ private _parent?: Entity;
20
+ readonly parentChanged = new Signal();
21
+ readonly onRemove = new Signal();
22
+ private markRemoval = false;
23
+ private _detached = false;
24
+ private readonly signalMap: Map<ID, Signal> = new Map<ID, Signal>();
25
+
26
+ private _dynamicComponents = new Map<Class, InstanceType<any>>();
27
+
28
+ readonly dynamicComponentAdded = new Signal<InstanceType<any>>();
29
+ readonly dynamicComponentRemoved = new Signal<InstanceType<any>>();
30
+
31
+ get dynamicComponents(): ReadonlyMap<Class, InstanceType<any>> {
32
+ return this._dynamicComponents;
33
+ }
34
+
35
+ // TODO: move the logic to gameEngine?
36
+ addDynamicComponent<T extends Class>(component: InstanceType<T>) {
37
+ if (this.hasDynamicComponentClass(component.constructor)) {
38
+ throw new Error('component of this class already added');
39
+ }
40
+ this._dynamicComponents.set(component.constructor as T, component);
41
+
42
+ this.dynamicComponentAdded.dispatch(component);
43
+ if (!this.gameEngine) return;
44
+
45
+ const systemContexts = this.gameEngine.injector.createSystemsForDynamicComponents(component.constructor, this);
46
+
47
+ if (!systemContexts) return;
48
+ const preparePromises = [];
49
+ for (const context of systemContexts) {
50
+ context.system.onPrepare && preparePromises.push(context.system.onPrepare());
51
+ }
52
+ for (const context of systemContexts) {
53
+ this.gameEngine['entitySystemMap'].get(this)?.add(context);
54
+ }
55
+
56
+ if (preparePromises.length === 0) {
57
+ this.gameEngine['startSystems'](systemContexts, () => {});
58
+ } else {
59
+ Promise.all(preparePromises).then(() => this.gameEngine?.['startSystems'](systemContexts));
60
+ }
61
+ }
62
+
63
+ hasDynamicComponentClass<T extends Class>(componentClass: T): boolean {
64
+ return this._dynamicComponents.has(componentClass);
65
+ }
66
+
67
+ // TODO: move the logic to gameEngine?
68
+ removeDynamicComponentClass<T extends Class>(componentClass: T) {
69
+ const systemContexts = this.gameEngine?.['entitySystemMap'].get(this);
70
+ systemContexts?.forEach(systemContext => {
71
+ const systemToDependencies = this.gameEngine?.injector['dynamicComponentDependencyMap'];
72
+ const dependencies = systemToDependencies?.get((systemContext.system as any).constructor);
73
+ if (!dependencies) return;
74
+ if (dependencies.some(dependency => dependency.type === componentClass)) {
75
+ this.gameEngine?.['disposeContext']?.(systemContext);
76
+ systemContexts?.delete(systemContext);
77
+ }
78
+ });
79
+ const component = this._dynamicComponents.get(componentClass);
80
+ if (!this._dynamicComponents.delete(componentClass)) {
81
+ throw new Error('component not found: ' + JSON.stringify(componentClass));
82
+ }
83
+ this.dynamicComponentRemoved.dispatch(component);
84
+ }
85
+
86
+ removeDynamicComponent<T extends Class>(component: InstanceType<T>) {
87
+ this.removeDynamicComponentClass(component.constructor as T);
88
+ }
89
+
90
+ getDynamicComponent<T extends Class>(componentClass: T): InstanceType<T> | undefined {
91
+ return this._dynamicComponents.get(componentClass);
92
+ }
93
+
94
+ get detached(): boolean { return this._detached; }
95
+
96
+ async onBeforeRemove?(): Promise<void>;
97
+
98
+ getSignal<T>(id: ID): Signal<T> {
99
+ return putIfAbsent(this.signalMap, id, () => new Signal<any>());
100
+ }
101
+
102
+ private setParent(parent: Entity | undefined) {
103
+ if (parent === this._parent) {
104
+ //TODO: i think this check can be removed
105
+ throw Error('same parent');
106
+ }
107
+ this._parent = parent;
108
+ this.parentChanged.dispatch();
109
+ }
110
+
111
+ get parent(): Entity | undefined {
112
+ return this._parent;
113
+ }
114
+
115
+ getComponent<T extends Class>(componentClass: T): InstanceType<T> | undefined {
116
+ const componentMap = EcsInjector.entityComponents.get(this.constructor as Class);
117
+ if (!componentMap) {
118
+ throw new Error(`No components in entity of type '${this.constructor.name}'.`);
119
+ }
120
+ const key = componentMap.get(componentClass as T);
121
+ return key && (<any>this)[key];
122
+ }
123
+
124
+ async add(entity: Entity) {
125
+ if (entity.parent === this) {
126
+ throw new Error('Entity is already a child');
127
+ }
128
+ if (entity === this) {
129
+ throw new Error('Could not add Entity as a child of itself.');
130
+ }
131
+
132
+ if (entity.parent) {
133
+ entity.parent.children.delete(entity);
134
+ }
135
+ this.children.add(entity);
136
+ entity.setParent(this);
137
+
138
+ if (entity.gameEngine) {
139
+ if (this.gameEngine !== entity.gameEngine) {
140
+ throw new Error('cant add an active to a passive entity');
141
+ }
142
+ } else {
143
+ if (this.gameEngine) {
144
+
145
+ await this.gameEngine.add(entity);
146
+ }
147
+ }
148
+
149
+ // TODO: systems should be informed so they can do stuff (renderer system should add object 3d etc...)
150
+
151
+ if (this.gameEngine && this.gameEngine !== entity.gameEngine) {
152
+
153
+ }
154
+ }
155
+
156
+ async remove(entity: Entity) {
157
+ if (!this.children.delete(entity)) {
158
+ throw new Error('Could not remove. Entity is not a child.');
159
+ }
160
+ entity.setParent(undefined);
161
+ if (this.gameEngine) {
162
+ return await this.gameEngine.remove(entity);
163
+ }
164
+ this.signalMap.forEach(signal => signal.detachAll());
165
+ }
166
+
167
+ async detach() {
168
+ if (this._detached) {
169
+ throw new Error('entity already detached');
170
+ }
171
+ this._detached = true;
172
+ // TODO: mark for detach and prevent another call, because this.gameEngine is set to undefined until next frame
173
+ if (this.parent) {
174
+ await this.parent.remove(this);
175
+ } else {
176
+ if (!this.gameEngine) {
177
+ throw new Error('entity isn\'t attached to a parent or the game engine');
178
+ }
179
+ await this.gameEngine.remove(this);
180
+ }
181
+ }
182
+
183
+ has(entity: Entity) {
184
+ return this.children.has(entity);
185
+ }
186
+
187
+ /**
188
+ * this includes the entity itself
189
+ * @param target
190
+ */
191
+ getAllEntities(target: Entity[] = []) {
192
+ target.push(this);
193
+ for (const child of this.children) {
194
+ child.getAllEntities(target);
195
+ }
196
+ return target;
197
+ }
198
+
199
+ hasDynamicComponents() {
200
+ return this._dynamicComponents.size > 0;
201
+ }
202
+ }