mani-game-engine 1.0.0-pre.12 → 1.0.0-pre.15
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/LICENSE +21 -21
- package/README.md +69 -69
- package/lib/clock.d.ts +40 -40
- package/lib/clock.js +79 -79
- package/lib/ecsInjector.d.ts +26 -26
- package/lib/ecsInjector.js +139 -139
- package/lib/entity.d.ts +23 -21
- package/lib/entity.js +91 -85
- package/lib/entity.js.map +1 -1
- package/lib/gameEngine.d.ts +79 -79
- package/lib/gameEngine.js +327 -327
- package/lib/index.d.ts +12 -12
- package/lib/index.js +10 -10
- package/lib/scope/scopeContext.d.ts +54 -54
- package/lib/scope/scopeContext.js +244 -244
- package/lib/systemContext.d.ts +12 -12
- package/lib/systemContext.js +19 -19
- package/lib/types.d.ts +69 -69
- package/lib/types.js +11 -11
- package/lib/utils/map2k.d.ts +6 -6
- package/lib/utils/map2k.js +39 -39
- package/package.json +41 -41
- package/src/clock.ts +115 -115
- package/src/ecsInjector.ts +184 -184
- package/src/entity.ts +123 -116
- package/src/gameEngine.ts +424 -424
- package/src/index.ts +17 -17
- package/src/scope/scopeContext.ts +314 -314
- package/src/systemContext.ts +27 -27
- package/src/types.ts +84 -84
- package/src/utils/map2k.ts +52 -52
package/src/ecsInjector.ts
CHANGED
|
@@ -1,184 +1,184 @@
|
|
|
1
|
-
// TODO: what if systems for entity without components are created?
|
|
2
|
-
import {Class, createDependencyAnnotation, Dependency, ID, Injector, putIfAbsent, ResolverContext, ResolverFunction} from 'mani-injector';
|
|
3
|
-
import {Signal} from 'mani-signal';
|
|
4
|
-
import {SystemContext} from './systemContext';
|
|
5
|
-
|
|
6
|
-
type ComponentClass = Class;
|
|
7
|
-
type EntityClass = Class;
|
|
8
|
-
|
|
9
|
-
type SystemResolvers<T extends Class> = [T, ResolverFunction[]]
|
|
10
|
-
type ComponentDependency = Dependency & { kind: 'component'; index: number; type: Class; };
|
|
11
|
-
type EntityDependency = Dependency & { kind: 'entity'; index: number; };
|
|
12
|
-
type ContextDependency = Dependency & { kind: 'context'; index: number; };
|
|
13
|
-
type SignalDependency = Dependency & { kind: 'signal'; index: number; id: ID; };
|
|
14
|
-
|
|
15
|
-
type EntityResolverContext = ResolverContext & { entityClass: Object; };
|
|
16
|
-
|
|
17
|
-
export const entityComponents = new Map<EntityClass, Map<ComponentClass, string>>();
|
|
18
|
-
|
|
19
|
-
export const GetComponent = createDependencyAnnotation((type, index): ComponentDependency => ({kind: 'component', type, index}));
|
|
20
|
-
export const GetEntity = createDependencyAnnotation((_type, index): EntityDependency => ({kind: 'entity', index}));
|
|
21
|
-
export const GetContext = createDependencyAnnotation((_type, index): ContextDependency => ({kind: 'context', index}));
|
|
22
|
-
export const GetSignal = (id: ID) => createDependencyAnnotation((_type, index): SignalDependency => ({kind: 'signal', index, id}));
|
|
23
|
-
export const EntityComponent = (target: object, propertyKey: string): any => {
|
|
24
|
-
const entityClass = target.constructor;
|
|
25
|
-
const componentClass = Reflect.getMetadata('design:type', target, propertyKey);
|
|
26
|
-
if (componentClass === Object) {
|
|
27
|
-
throw new Error(`Object component type not allowed. Forgot to specify type of ${entityClass.name}.${propertyKey}?`);
|
|
28
|
-
}
|
|
29
|
-
const componentSet = putIfAbsent(entityComponents, entityClass, () => new Map<ComponentClass, string>());
|
|
30
|
-
componentSet.set(componentClass, propertyKey);
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export const signalHandlers = new Map<Object, [string, ID][]>();
|
|
34
|
-
export const staticSignalHandlers = new Map<Object, [string, ID][]>();
|
|
35
|
-
export const OnSignal = (id: ID) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
|
|
36
|
-
if (target instanceof Function) {
|
|
37
|
-
let mappingList = putIfAbsent(staticSignalHandlers, target, (): [string, ID][] => []);
|
|
38
|
-
mappingList.push([propertyKey, id]);
|
|
39
|
-
} else {
|
|
40
|
-
let mappingList = putIfAbsent(signalHandlers, target.constructor, (): [string, ID][] => []);
|
|
41
|
-
mappingList.push([propertyKey, id]);
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
const getComponentDependencies = (system: Class) => {
|
|
46
|
-
const dependencies = Injector.dependencyMap.get(system);
|
|
47
|
-
return dependencies
|
|
48
|
-
? dependencies.filter((dependency): dependency is ComponentDependency => dependency.kind === 'component')
|
|
49
|
-
: [];
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const getEntityClassesForComponentDependencies = (componentTypes: Class[]): Class[] => {
|
|
53
|
-
// TODO: refactor class, use map, filter etc...
|
|
54
|
-
const result = [];
|
|
55
|
-
for (const [entityClass, componentMap] of entityComponents) {
|
|
56
|
-
let allDependenciesMet = true;
|
|
57
|
-
for (const componentType of componentTypes) {
|
|
58
|
-
if (!componentMap.has(componentType)) {
|
|
59
|
-
allDependenciesMet = false;
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
if (allDependenciesMet) {
|
|
64
|
-
result.push(entityClass);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return result;
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
const componentResolver = (context: ResolverContext, dependency: Dependency): ResolverFunction => {
|
|
71
|
-
const kind = context.kind;
|
|
72
|
-
const type = context.type;
|
|
73
|
-
if (kind !== 'system') {
|
|
74
|
-
throw new Error(`Could not resolve Component ${type.name}. @GetComponent only allowed in system scope.`);
|
|
75
|
-
}
|
|
76
|
-
const entityClass = (context as EntityResolverContext).entityClass;
|
|
77
|
-
const key = entityComponents.get(<any>entityClass as Class)!.get((dependency as ComponentDependency).type);
|
|
78
|
-
return (context: SystemContext) => (context.entity as any)[key!];
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
const entityResolver = ({type, kind}: ResolverContext, _dependency: Dependency): ResolverFunction => {
|
|
82
|
-
if (kind !== 'system') {
|
|
83
|
-
throw new Error(`Could not resolve Entity in ${type.name}. @GetEntity only allowed in system scope.`);
|
|
84
|
-
}
|
|
85
|
-
return (context: SystemContext) => context.entity;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
const contextResolver = ({type, kind}: ResolverContext, _dependency: Dependency): ResolverFunction => {
|
|
89
|
-
if (kind !== 'system') {
|
|
90
|
-
throw new Error(`Could not resolve Context in ${type.name}. @GetContext only allowed in system scope.`);
|
|
91
|
-
}
|
|
92
|
-
return (context: SystemContext) => context;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
// const isSignalResolver = (dependency: Dependency): dependency is SignalDependency => dependency.kind === 'signal';
|
|
96
|
-
|
|
97
|
-
export class EcsInjector<SystemClass extends Class = Class> extends Injector {
|
|
98
|
-
// protected readonly entitySystemMap = new Map<EntityClass, SystemClass[]>();
|
|
99
|
-
protected entitySystemMap: Map<Class, SystemClass[]>;
|
|
100
|
-
protected entitySystemResolverTuples: Map<Class, SystemResolvers<SystemClass>[]>;
|
|
101
|
-
private signalMap: Map<ID, Signal>;
|
|
102
|
-
|
|
103
|
-
constructor(parent?: EcsInjector<SystemClass>) {
|
|
104
|
-
super(parent);
|
|
105
|
-
this.signalMap = parent ? parent.signalMap : new Map<ID, Signal>();
|
|
106
|
-
this.entitySystemMap = parent ? parent.entitySystemMap : new Map<EntityClass, SystemClass[]>();
|
|
107
|
-
this.entitySystemResolverTuples = parent ? parent.entitySystemResolverTuples : new Map<EntityClass, SystemResolvers<SystemClass>[]>();
|
|
108
|
-
this.map(EcsInjector).toValue(this);
|
|
109
|
-
|
|
110
|
-
if (!parent) {
|
|
111
|
-
// only add extension resolvers to the main/parent injector
|
|
112
|
-
this.addExtensionResolver('entity', entityResolver);
|
|
113
|
-
this.addExtensionResolver('component', componentResolver);
|
|
114
|
-
this.addExtensionResolver('context', contextResolver);
|
|
115
|
-
|
|
116
|
-
const signalResolver = ({kind}: ResolverContext, dependency: Dependency): ResolverFunction => {
|
|
117
|
-
const signal = putIfAbsent(this.signalMap, (<SignalDependency>dependency).id, () => new Signal());
|
|
118
|
-
return (entity: any) => signal;
|
|
119
|
-
};
|
|
120
|
-
this.addExtensionResolver('signal', signalResolver);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
registerSystem<T extends SystemClass>(systemClass: T) {
|
|
125
|
-
// TODO: check if system is already mapped
|
|
126
|
-
const componentDependencies = getComponentDependencies(systemClass).map(dependency => dependency.type);
|
|
127
|
-
if (componentDependencies.length === 0) {
|
|
128
|
-
throw new Error(`${systemClass.name} needs at least one component dependency.`);
|
|
129
|
-
}
|
|
130
|
-
const entityClasses = getEntityClassesForComponentDependencies(componentDependencies);
|
|
131
|
-
if (entityClasses.length === 0) {
|
|
132
|
-
console.warn(`System '${systemClass.name}' has no matching entities.`);
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
for (let entityClass of entityClasses) {
|
|
136
|
-
const systemClasses = putIfAbsent(this.entitySystemMap, entityClass, (): SystemClass[] => []);
|
|
137
|
-
systemClasses.push(systemClass);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
createSystems(entity: Object): SystemContext[] {
|
|
142
|
-
const systemResolverTuples = putIfAbsent(this.entitySystemResolverTuples, entity.constructor, (): SystemResolvers<SystemClass>[] => {
|
|
143
|
-
const systems = this.entitySystemMap.get(entity.constructor as Class);
|
|
144
|
-
if (!systems) {
|
|
145
|
-
return [];
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const result: SystemResolvers<SystemClass>[] = [];
|
|
149
|
-
for (const systemClass of systems) {
|
|
150
|
-
result.push([systemClass, this.createResolverArray({type: systemClass, kind: 'system', entityClass: entity.constructor})]);
|
|
151
|
-
}
|
|
152
|
-
return result;
|
|
153
|
-
});
|
|
154
|
-
const systemInstances: SystemContext[] = [];
|
|
155
|
-
for (const [system, resolver] of systemResolverTuples) {
|
|
156
|
-
const args = new Array(resolver.length);
|
|
157
|
-
|
|
158
|
-
const systemContext = new SystemContext<InstanceType<SystemClass>>(entity);
|
|
159
|
-
|
|
160
|
-
for (let i = 0; i < args.length; i++) {
|
|
161
|
-
args[i] = resolver[i](systemContext);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// friend class :)
|
|
165
|
-
(systemContext as any).system = new system(...args); // TODO: new class(...args) is very slow in firefox :/
|
|
166
|
-
|
|
167
|
-
// let systemInstance = new (system.bind(system, args[0], args[1], args[2], args[3], args[4]));
|
|
168
|
-
|
|
169
|
-
systemInstances.push(systemContext);
|
|
170
|
-
}
|
|
171
|
-
return systemInstances;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
getSignal<T>(id: ID): Signal<T> {
|
|
175
|
-
return putIfAbsent(this.signalMap, id, () => new Signal<any>());
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
dispose() {
|
|
179
|
-
// TODO: implement dispose in Injector
|
|
180
|
-
for (const [key, signal] of this.signalMap) {
|
|
181
|
-
signal.detachAll();
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
1
|
+
// TODO: what if systems for entity without components are created?
|
|
2
|
+
import {Class, createDependencyAnnotation, Dependency, ID, Injector, putIfAbsent, ResolverContext, ResolverFunction} from 'mani-injector';
|
|
3
|
+
import {Signal} from 'mani-signal';
|
|
4
|
+
import {SystemContext} from './systemContext';
|
|
5
|
+
|
|
6
|
+
type ComponentClass = Class;
|
|
7
|
+
type EntityClass = Class;
|
|
8
|
+
|
|
9
|
+
type SystemResolvers<T extends Class> = [T, ResolverFunction[]]
|
|
10
|
+
type ComponentDependency = Dependency & { kind: 'component'; index: number; type: Class; };
|
|
11
|
+
type EntityDependency = Dependency & { kind: 'entity'; index: number; };
|
|
12
|
+
type ContextDependency = Dependency & { kind: 'context'; index: number; };
|
|
13
|
+
type SignalDependency = Dependency & { kind: 'signal'; index: number; id: ID; };
|
|
14
|
+
|
|
15
|
+
type EntityResolverContext = ResolverContext & { entityClass: Object; };
|
|
16
|
+
|
|
17
|
+
export const entityComponents = new Map<EntityClass, Map<ComponentClass, string>>();
|
|
18
|
+
|
|
19
|
+
export const GetComponent = createDependencyAnnotation((type, index): ComponentDependency => ({kind: 'component', type, index}));
|
|
20
|
+
export const GetEntity = createDependencyAnnotation((_type, index): EntityDependency => ({kind: 'entity', index}));
|
|
21
|
+
export const GetContext = createDependencyAnnotation((_type, index): ContextDependency => ({kind: 'context', index}));
|
|
22
|
+
export const GetSignal = (id: ID) => createDependencyAnnotation((_type, index): SignalDependency => ({kind: 'signal', index, id}));
|
|
23
|
+
export const EntityComponent = (target: object, propertyKey: string): any => {
|
|
24
|
+
const entityClass = target.constructor;
|
|
25
|
+
const componentClass = Reflect.getMetadata('design:type', target, propertyKey);
|
|
26
|
+
if (componentClass === Object) {
|
|
27
|
+
throw new Error(`Object component type not allowed. Forgot to specify type of ${entityClass.name}.${propertyKey}?`);
|
|
28
|
+
}
|
|
29
|
+
const componentSet = putIfAbsent(entityComponents, entityClass, () => new Map<ComponentClass, string>());
|
|
30
|
+
componentSet.set(componentClass, propertyKey);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const signalHandlers = new Map<Object, [string, ID][]>();
|
|
34
|
+
export const staticSignalHandlers = new Map<Object, [string, ID][]>();
|
|
35
|
+
export const OnSignal = (id: ID) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
|
|
36
|
+
if (target instanceof Function) {
|
|
37
|
+
let mappingList = putIfAbsent(staticSignalHandlers, target, (): [string, ID][] => []);
|
|
38
|
+
mappingList.push([propertyKey, id]);
|
|
39
|
+
} else {
|
|
40
|
+
let mappingList = putIfAbsent(signalHandlers, target.constructor, (): [string, ID][] => []);
|
|
41
|
+
mappingList.push([propertyKey, id]);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const getComponentDependencies = (system: Class) => {
|
|
46
|
+
const dependencies = Injector.dependencyMap.get(system);
|
|
47
|
+
return dependencies
|
|
48
|
+
? dependencies.filter((dependency): dependency is ComponentDependency => dependency.kind === 'component')
|
|
49
|
+
: [];
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const getEntityClassesForComponentDependencies = (componentTypes: Class[]): Class[] => {
|
|
53
|
+
// TODO: refactor class, use map, filter etc...
|
|
54
|
+
const result = [];
|
|
55
|
+
for (const [entityClass, componentMap] of entityComponents) {
|
|
56
|
+
let allDependenciesMet = true;
|
|
57
|
+
for (const componentType of componentTypes) {
|
|
58
|
+
if (!componentMap.has(componentType)) {
|
|
59
|
+
allDependenciesMet = false;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (allDependenciesMet) {
|
|
64
|
+
result.push(entityClass);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const componentResolver = (context: ResolverContext, dependency: Dependency): ResolverFunction => {
|
|
71
|
+
const kind = context.kind;
|
|
72
|
+
const type = context.type;
|
|
73
|
+
if (kind !== 'system') {
|
|
74
|
+
throw new Error(`Could not resolve Component ${type.name}. @GetComponent only allowed in system scope.`);
|
|
75
|
+
}
|
|
76
|
+
const entityClass = (context as EntityResolverContext).entityClass;
|
|
77
|
+
const key = entityComponents.get(<any>entityClass as Class)!.get((dependency as ComponentDependency).type);
|
|
78
|
+
return (context: SystemContext) => (context.entity as any)[key!];
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const entityResolver = ({type, kind}: ResolverContext, _dependency: Dependency): ResolverFunction => {
|
|
82
|
+
if (kind !== 'system') {
|
|
83
|
+
throw new Error(`Could not resolve Entity in ${type.name}. @GetEntity only allowed in system scope.`);
|
|
84
|
+
}
|
|
85
|
+
return (context: SystemContext) => context.entity;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const contextResolver = ({type, kind}: ResolverContext, _dependency: Dependency): ResolverFunction => {
|
|
89
|
+
if (kind !== 'system') {
|
|
90
|
+
throw new Error(`Could not resolve Context in ${type.name}. @GetContext only allowed in system scope.`);
|
|
91
|
+
}
|
|
92
|
+
return (context: SystemContext) => context;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// const isSignalResolver = (dependency: Dependency): dependency is SignalDependency => dependency.kind === 'signal';
|
|
96
|
+
|
|
97
|
+
export class EcsInjector<SystemClass extends Class = Class> extends Injector {
|
|
98
|
+
// protected readonly entitySystemMap = new Map<EntityClass, SystemClass[]>();
|
|
99
|
+
protected entitySystemMap: Map<Class, SystemClass[]>;
|
|
100
|
+
protected entitySystemResolverTuples: Map<Class, SystemResolvers<SystemClass>[]>;
|
|
101
|
+
private signalMap: Map<ID, Signal>;
|
|
102
|
+
|
|
103
|
+
constructor(parent?: EcsInjector<SystemClass>) {
|
|
104
|
+
super(parent);
|
|
105
|
+
this.signalMap = parent ? parent.signalMap : new Map<ID, Signal>();
|
|
106
|
+
this.entitySystemMap = parent ? parent.entitySystemMap : new Map<EntityClass, SystemClass[]>();
|
|
107
|
+
this.entitySystemResolverTuples = parent ? parent.entitySystemResolverTuples : new Map<EntityClass, SystemResolvers<SystemClass>[]>();
|
|
108
|
+
this.map(EcsInjector).toValue(this);
|
|
109
|
+
|
|
110
|
+
if (!parent) {
|
|
111
|
+
// only add extension resolvers to the main/parent injector
|
|
112
|
+
this.addExtensionResolver('entity', entityResolver);
|
|
113
|
+
this.addExtensionResolver('component', componentResolver);
|
|
114
|
+
this.addExtensionResolver('context', contextResolver);
|
|
115
|
+
|
|
116
|
+
const signalResolver = ({kind}: ResolverContext, dependency: Dependency): ResolverFunction => {
|
|
117
|
+
const signal = putIfAbsent(this.signalMap, (<SignalDependency>dependency).id, () => new Signal());
|
|
118
|
+
return (entity: any) => signal;
|
|
119
|
+
};
|
|
120
|
+
this.addExtensionResolver('signal', signalResolver);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
registerSystem<T extends SystemClass>(systemClass: T) {
|
|
125
|
+
// TODO: check if system is already mapped
|
|
126
|
+
const componentDependencies = getComponentDependencies(systemClass).map(dependency => dependency.type);
|
|
127
|
+
if (componentDependencies.length === 0) {
|
|
128
|
+
throw new Error(`${systemClass.name} needs at least one component dependency.`);
|
|
129
|
+
}
|
|
130
|
+
const entityClasses = getEntityClassesForComponentDependencies(componentDependencies);
|
|
131
|
+
if (entityClasses.length === 0) {
|
|
132
|
+
console.warn(`System '${systemClass.name}' has no matching entities.`);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
for (let entityClass of entityClasses) {
|
|
136
|
+
const systemClasses = putIfAbsent(this.entitySystemMap, entityClass, (): SystemClass[] => []);
|
|
137
|
+
systemClasses.push(systemClass);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
createSystems(entity: Object): SystemContext[] {
|
|
142
|
+
const systemResolverTuples = putIfAbsent(this.entitySystemResolverTuples, entity.constructor, (): SystemResolvers<SystemClass>[] => {
|
|
143
|
+
const systems = this.entitySystemMap.get(entity.constructor as Class);
|
|
144
|
+
if (!systems) {
|
|
145
|
+
return [];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const result: SystemResolvers<SystemClass>[] = [];
|
|
149
|
+
for (const systemClass of systems) {
|
|
150
|
+
result.push([systemClass, this.createResolverArray({type: systemClass, kind: 'system', entityClass: entity.constructor})]);
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
153
|
+
});
|
|
154
|
+
const systemInstances: SystemContext[] = [];
|
|
155
|
+
for (const [system, resolver] of systemResolverTuples) {
|
|
156
|
+
const args = new Array(resolver.length);
|
|
157
|
+
|
|
158
|
+
const systemContext = new SystemContext<InstanceType<SystemClass>>(entity);
|
|
159
|
+
|
|
160
|
+
for (let i = 0; i < args.length; i++) {
|
|
161
|
+
args[i] = resolver[i](systemContext);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// friend class :)
|
|
165
|
+
(systemContext as any).system = new system(...args); // TODO: new class(...args) is very slow in firefox :/
|
|
166
|
+
|
|
167
|
+
// let systemInstance = new (system.bind(system, args[0], args[1], args[2], args[3], args[4]));
|
|
168
|
+
|
|
169
|
+
systemInstances.push(systemContext);
|
|
170
|
+
}
|
|
171
|
+
return systemInstances;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
getSignal<T>(id: ID): Signal<T> {
|
|
175
|
+
return putIfAbsent(this.signalMap, id, () => new Signal<any>());
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
dispose() {
|
|
179
|
+
// TODO: implement dispose in Injector
|
|
180
|
+
for (const [key, signal] of this.signalMap) {
|
|
181
|
+
signal.detachAll();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
package/src/entity.ts
CHANGED
|
@@ -1,116 +1,123 @@
|
|
|
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
|
-
|
|
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
|
-
|
|
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
|
+
private _detached = false;
|
|
20
|
+
get detached(): boolean { return this._detached; }
|
|
21
|
+
|
|
22
|
+
constructor() {
|
|
23
|
+
this.id = idCounter++;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async onBeforeRemove?(): Promise<void>;
|
|
27
|
+
|
|
28
|
+
private setParent(parent: Entity | undefined) {
|
|
29
|
+
if (parent === this._parent) {
|
|
30
|
+
//TODO: i think this check can be removed
|
|
31
|
+
throw Error('same parent');
|
|
32
|
+
}
|
|
33
|
+
this._parent = parent;
|
|
34
|
+
this.parentChanged.dispatch();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get parent(): Entity | undefined {
|
|
38
|
+
return this._parent;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
getComponent<T extends Class>(componentClass: T): InstanceType<T> | undefined {
|
|
42
|
+
const componentMap = entityComponents.get(this.constructor as Class);
|
|
43
|
+
if (!componentMap) {
|
|
44
|
+
throw new Error(`No components in entity of type '${this.constructor.name}'.`);
|
|
45
|
+
}
|
|
46
|
+
const key = componentMap.get(componentClass as T);
|
|
47
|
+
return key && (<any>this)[key];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async add(entity: Entity) {
|
|
51
|
+
if (entity.parent === this) {
|
|
52
|
+
throw new Error('Entity is already a child');
|
|
53
|
+
}
|
|
54
|
+
if (entity === this) {
|
|
55
|
+
throw new Error('Could not add Entity as a child of itself.');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (entity.parent) {
|
|
59
|
+
entity.parent.children.delete(entity);
|
|
60
|
+
}
|
|
61
|
+
this.children.add(entity);
|
|
62
|
+
entity.setParent(this);
|
|
63
|
+
|
|
64
|
+
if (entity.gameEngine) {
|
|
65
|
+
if (this.gameEngine !== entity.gameEngine) {
|
|
66
|
+
throw new Error('cant add an active to a passive entity');
|
|
67
|
+
}
|
|
68
|
+
} else {
|
|
69
|
+
if (this.gameEngine) {
|
|
70
|
+
|
|
71
|
+
await this.gameEngine.add(entity);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// TODO: systems should be informed so they can do stuff (renderer system should add object 3d etc...)
|
|
76
|
+
|
|
77
|
+
if (this.gameEngine && this.gameEngine !== entity.gameEngine) {
|
|
78
|
+
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async remove(entity: Entity) {
|
|
83
|
+
if (!this.children.delete(entity)) {
|
|
84
|
+
throw new Error('Could not remove. Entity is not a child.');
|
|
85
|
+
}
|
|
86
|
+
entity.setParent(undefined);
|
|
87
|
+
if (this.gameEngine) {
|
|
88
|
+
return await this.gameEngine.remove(entity);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async detach() {
|
|
93
|
+
if (this._detached) {
|
|
94
|
+
throw new Error('entity already detached');
|
|
95
|
+
}
|
|
96
|
+
this._detached = true;
|
|
97
|
+
// TODO: mark for detach and prevent another call, because this.gameEngine is set to undefined until next frame
|
|
98
|
+
if (this.parent) {
|
|
99
|
+
await this.parent.remove(this);
|
|
100
|
+
} else {
|
|
101
|
+
if (!this.gameEngine) {
|
|
102
|
+
throw new Error('entity isn\'t attached to a parent or the game engine');
|
|
103
|
+
}
|
|
104
|
+
await this.gameEngine.remove(this);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
has(entity: Entity) {
|
|
109
|
+
return this.children.has(entity);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* this includes the entity itself
|
|
114
|
+
* @param target
|
|
115
|
+
*/
|
|
116
|
+
getAllEntities(target: Entity[] = []) {
|
|
117
|
+
target.push(this);
|
|
118
|
+
for (const child of this.children) {
|
|
119
|
+
child.getAllEntities(target);
|
|
120
|
+
}
|
|
121
|
+
return target;
|
|
122
|
+
}
|
|
123
|
+
}
|