mani-game-engine 1.0.0-pre.10 → 1.0.0-pre.100

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2020 Jan Mankopf
3
+ Copyright (c) 2022 Jan Mankopf
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/lib/clock.d.ts CHANGED
@@ -6,8 +6,14 @@ declare const defaultOptions: {
6
6
  maxFrameTime: number;
7
7
  timeScale: number;
8
8
  };
9
- export declare type GameClockOptions = Partial<typeof defaultOptions>;
9
+ export type Timeout = {
10
+ recall?: number;
11
+ callback: Function;
12
+ callTime: number;
13
+ };
14
+ export type GameClockOptions = Partial<typeof defaultOptions>;
10
15
  export interface Clock {
16
+ readonly gameTime: number;
11
17
  onPrepare: Signal<unknown>;
12
18
  onEarlyUpdate: Signal<OnEarlyUpdateParams>;
13
19
  onUpdate: Signal<OnUpdateParams>;
@@ -15,26 +21,44 @@ export interface Clock {
15
21
  timeScale: number;
16
22
  start(): this;
17
23
  stop(): this;
24
+ dispose(): void;
25
+ processFrame(time: number): void;
26
+ readonly fixedDeltaTime: number;
27
+ setTimeout(callback: Function, delay: number): number;
28
+ setInterval(callback: Function, delay: number): number;
29
+ clearTimeout(timeoutId: number): boolean;
30
+ fixedTween(duration: number, from: number, to: number, onUpdate: (t: number) => void): Promise<void>;
31
+ tween(duration: number, from: number, to: number, onUpdate: (t: number) => void): Promise<void>;
18
32
  }
19
33
  export declare class GameClock implements Clock {
34
+ get gameTime(): number;
35
+ get animationTime(): number;
36
+ private _animationTime;
37
+ private nextTimeoutId;
38
+ private timeouts;
20
39
  private isRunning;
21
- private requestId;
22
40
  private currentTime;
23
41
  private accumulator;
24
- private gameTime;
42
+ private _gameTime;
25
43
  private readonly _fixedDeltaTime;
26
- private update;
27
- private maxFrameTime;
44
+ processFrame(time: number): void;
45
+ private readonly maxFrameTime;
28
46
  readonly onPrepare: Signal<unknown>;
29
47
  readonly onEarlyUpdate: Signal<OnEarlyUpdateParams>;
30
48
  readonly onUpdate: Signal<OnUpdateParams>;
31
49
  readonly onFixedUpdate: Signal<OnFixedUpdateParams>;
32
50
  constructor(options?: GameClockOptions);
51
+ dispose(): void;
33
52
  get fixedDeltaTime(): number;
34
53
  private _timeScale;
35
54
  get timeScale(): number;
36
55
  set timeScale(value: number);
37
56
  start(): this;
38
57
  stop(): this;
58
+ setTimeout(callback: Function, delay: number): number;
59
+ setInterval(callback: Function, delay: number): number;
60
+ fixedTween(duration: number, from: number, to: number, onUpdate: (t: number) => void): Promise<void>;
61
+ tween(duration: number, from: number, to: number, onUpdate: (t: number) => void): Promise<void>;
62
+ clearTimeout(timeoutId: number): boolean;
39
63
  }
40
64
  export {};
package/lib/clock.js CHANGED
@@ -1,4 +1,7 @@
1
- import { Signal } from 'mani-signal';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GameClock = void 0;
4
+ const mani_signal_1 = require("mani-signal");
2
5
  const MIN_TIME_SCALE = 0.00001;
3
6
  const defaultOptions = {
4
7
  autoStart: true,
@@ -6,47 +9,60 @@ const defaultOptions = {
6
9
  maxFrameTime: 0.25,
7
10
  timeScale: 1,
8
11
  };
9
- export class GameClock {
12
+ class GameClock {
13
+ get gameTime() {
14
+ return this._gameTime;
15
+ }
16
+ get animationTime() {
17
+ return this._animationTime;
18
+ }
19
+ processFrame(time) {
20
+ if (!this.isRunning) {
21
+ return;
22
+ }
23
+ const newTime = time * 0.001;
24
+ const rawDelta = newTime - this.currentTime;
25
+ let clampedDelta = rawDelta;
26
+ if (clampedDelta > this.maxFrameTime) {
27
+ clampedDelta = this.maxFrameTime;
28
+ }
29
+ const frameTime = clampedDelta * this.timeScale;
30
+ this.currentTime = newTime;
31
+ this.accumulator += frameTime;
32
+ while (this.accumulator >= this._fixedDeltaTime) {
33
+ this.onEarlyUpdate.dispatch({ time: this._gameTime, deltaTime: this._fixedDeltaTime });
34
+ this.onPrepare.dispatch();
35
+ for (const [id, timeOut] of this.timeouts) {
36
+ if (this._gameTime >= timeOut.callTime) {
37
+ timeOut.callback();
38
+ if (timeOut.recall) {
39
+ timeOut.callTime += timeOut.recall;
40
+ }
41
+ else {
42
+ this.timeouts.delete(id);
43
+ }
44
+ }
45
+ }
46
+ this.onFixedUpdate.dispatch({ time: this._gameTime, deltaTime: this._fixedDeltaTime });
47
+ this.accumulator -= this._fixedDeltaTime;
48
+ this._gameTime += this._fixedDeltaTime;
49
+ }
50
+ const alpha = this.accumulator / this._fixedDeltaTime;
51
+ this._animationTime = this._gameTime + alpha * this._fixedDeltaTime;
52
+ this.onUpdate.dispatch({ time: this._animationTime, deltaTime: frameTime, alpha: alpha });
53
+ }
10
54
  constructor(options) {
55
+ this._animationTime = 0;
56
+ this.nextTimeoutId = 0;
57
+ this.timeouts = new Map();
11
58
  this.isRunning = false;
12
- this.requestId = 0;
13
59
  this.currentTime = 0;
14
60
  this.accumulator = 0;
15
- this.gameTime = 0;
16
- this.update = (time) => {
17
- const newTime = time * 0.001;
18
- if (!this.isRunning) {
19
- this.currentTime = newTime;
20
- this.isRunning = true;
21
- }
22
- let frameTime = (newTime - this.currentTime) * this.timeScale;
23
- if (frameTime > this.maxFrameTime) {
24
- frameTime = this.maxFrameTime;
25
- }
26
- this.currentTime = newTime;
27
- this.accumulator += frameTime;
28
- let isMultipleFixedUpdate = false;
29
- this.onPrepare.dispatch();
30
- this.onEarlyUpdate.dispatch({ time: this.gameTime, deltaTime: frameTime });
31
- while (this.accumulator >= this._fixedDeltaTime) {
32
- if (isMultipleFixedUpdate) {
33
- this.onPrepare.dispatch();
34
- }
35
- this.onFixedUpdate.dispatch({ time: this.gameTime, deltaTime: this._fixedDeltaTime });
36
- isMultipleFixedUpdate = true;
37
- this.accumulator -= this._fixedDeltaTime;
38
- this.gameTime += this._fixedDeltaTime;
39
- }
40
- const alpha = this.accumulator / this._fixedDeltaTime;
41
- this.onUpdate.dispatch({ time: this.gameTime + alpha * this._fixedDeltaTime, deltaTime: frameTime, alpha: alpha });
42
- if (this.isRunning) {
43
- this.requestId = requestAnimationFrame(this.update);
44
- }
45
- };
46
- this.onPrepare = new Signal();
47
- this.onEarlyUpdate = new Signal();
48
- this.onUpdate = new Signal();
49
- this.onFixedUpdate = new Signal();
61
+ this._gameTime = 0;
62
+ this.onPrepare = new mani_signal_1.Signal();
63
+ this.onEarlyUpdate = new mani_signal_1.Signal();
64
+ this.onUpdate = new mani_signal_1.Signal();
65
+ this.onFixedUpdate = new mani_signal_1.Signal();
50
66
  this._timeScale = 1;
51
67
  const opts = Object.assign(Object.assign({}, defaultOptions), options);
52
68
  this._fixedDeltaTime = opts.fixedDeltaTime;
@@ -54,18 +70,28 @@ export class GameClock {
54
70
  this.timeScale = opts.timeScale;
55
71
  opts.autoStart && this.start();
56
72
  }
73
+ dispose() {
74
+ this.stop();
75
+ this.timeouts.clear();
76
+ this.onPrepare.detachAll();
77
+ this.onEarlyUpdate.detachAll();
78
+ this.onUpdate.detachAll();
79
+ this.onFixedUpdate.detachAll();
80
+ }
57
81
  get fixedDeltaTime() { return this._fixedDeltaTime; }
58
82
  get timeScale() {
59
83
  return this._timeScale;
60
84
  }
61
85
  set timeScale(value) {
62
- this._timeScale = Math.max(value, MIN_TIME_SCALE);
86
+ this._timeScale = value === 0 ? 0 : Math.max(value, MIN_TIME_SCALE);
63
87
  }
64
88
  start() {
65
89
  if (this.isRunning) {
66
90
  return this;
67
91
  }
68
- this.requestId = requestAnimationFrame(this.update);
92
+ this.isRunning = true;
93
+ this.currentTime = performance.now() * 0.001;
94
+ this.accumulator = 0;
69
95
  return this;
70
96
  }
71
97
  stop() {
@@ -73,8 +99,49 @@ export class GameClock {
73
99
  return this;
74
100
  }
75
101
  this.isRunning = false;
76
- cancelAnimationFrame(this.requestId);
77
102
  return this;
78
103
  }
104
+ setTimeout(callback, delay) {
105
+ const timeOut = { callback, callTime: this._gameTime + delay };
106
+ this.nextTimeoutId++;
107
+ this.timeouts.set(this.nextTimeoutId, timeOut);
108
+ return this.nextTimeoutId;
109
+ }
110
+ setInterval(callback, delay) {
111
+ const timeOut = { callback, callTime: this._gameTime + delay, recall: delay };
112
+ this.nextTimeoutId++;
113
+ this.timeouts.set(this.nextTimeoutId, timeOut);
114
+ return this.nextTimeoutId;
115
+ }
116
+ async fixedTween(duration, from, to, onUpdate) {
117
+ return new Promise((resolve) => {
118
+ const startTime = this.gameTime;
119
+ const binding = this.onFixedUpdate.add(({ time }) => {
120
+ const t = Math.min(1, (this.gameTime - startTime) / duration);
121
+ onUpdate(from + (to - from) * t);
122
+ if (t >= 1) {
123
+ binding.detach();
124
+ resolve();
125
+ }
126
+ });
127
+ });
128
+ }
129
+ async tween(duration, from, to, onUpdate) {
130
+ return new Promise((resolve) => {
131
+ const startTime = this._animationTime;
132
+ const binding = this.onUpdate.add(({ time }) => {
133
+ const t = Math.min(1, (this._animationTime - startTime) / duration);
134
+ onUpdate(from + (to - from) * t);
135
+ if (t >= 1) {
136
+ binding.detach();
137
+ resolve();
138
+ }
139
+ });
140
+ });
141
+ }
142
+ clearTimeout(timeoutId) {
143
+ return this.timeouts.delete(timeoutId);
144
+ }
79
145
  }
146
+ exports.GameClock = GameClock;
80
147
  //# sourceMappingURL=clock.js.map
package/lib/clock.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"clock.js","sourceRoot":"","sources":["../src/clock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AAGnC,MAAM,cAAc,GAAG,OAAO,CAAC;AAI/B,MAAM,cAAc,GAAG;IACnB,SAAS,EAAE,IAAI;IACf,cAAc,EAAE,CAAC,GAAG,EAAE;IACtB,YAAY,EAAE,IAAI;IAClB,SAAS,EAAE,CAAC;CACf,CAAC;AAeF,MAAM,OAAO,SAAS;IAkDlB,YAAY,OAA0B;QA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AAC,GAAG,CAAC,CAAC,CAAC;gBACjC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;oBACX,OAAO,CAAC,MAAM,EAAE,CAAC;oBACjB,OAAO,EAAE,CAAC;gBACZ,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAED,YAAY,CAAC,SAAiB;QAC5B,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACzC,CAAC;CACF;AApKD,8BAoKC"}
@@ -1,25 +1,45 @@
1
- import { Class, ID, Injector, ResolverFunction } from 'mani-injector';
2
1
  import { Signal } from 'mani-signal';
2
+ import { Class, Dependency, ID, Injector, ResolverFunction } from './injector';
3
3
  import { SystemContext } from './systemContext';
4
- declare type ComponentClass = Class;
5
- declare type EntityClass = Class;
6
- declare type SystemResolvers<T extends Class> = [T, ResolverFunction[]];
7
- export declare const entityComponents: Map<EntityClass, Map<ComponentClass, string>>;
8
- export declare const GetComponent: (dependantType: Class<any>, _propertyKey: string | symbol, index: number) => any;
9
- export declare const GetEntity: (dependantType: Class<any>, _propertyKey: string | symbol, index: number) => any;
10
- export declare const GetContext: (dependantType: Class<any>, _propertyKey: string | symbol, index: number) => any;
11
- export declare const GetSignal: (id: ID) => (dependantType: Class<any>, _propertyKey: string | symbol, index: number) => any;
4
+ import { Entity } from './entity';
5
+ type ComponentClass = Class;
6
+ type EntityClass = Class;
7
+ type ComponentDependency = Dependency & {
8
+ kind: 'component';
9
+ index: number;
10
+ type: Class;
11
+ };
12
+ type DynamicComponentDependency = Dependency & {
13
+ kind: 'dynamic';
14
+ index: number;
15
+ type: Class;
16
+ };
17
+ export declare const GetComponent: (dependantType: Class<any>, _propertyKey: any, index: number) => any;
18
+ export declare const GetDynamicComponent: (dependantType: Class<any>, _propertyKey: any, index: number) => any;
19
+ export declare const GetEntity: (dependantType: Class<any>, _propertyKey: any, index: number) => any;
20
+ export declare const GetContext: (dependantType: Class<any>, _propertyKey: any, index: number) => any;
21
+ export declare const GetSignal: (id: ID) => (dependantType: Class<any>, _propertyKey: any, index: number) => any;
22
+ export declare const GetEntitySignal: (id: ID) => (dependantType: Class<any>, _propertyKey: any, index: number) => any;
12
23
  export declare const EntityComponent: (target: object, propertyKey: string) => any;
13
24
  export declare const signalHandlers: Map<Object, [string, ID][]>;
14
25
  export declare const staticSignalHandlers: Map<Object, [string, ID][]>;
15
26
  export declare const OnSignal: (id: ID) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
16
27
  export declare class EcsInjector<SystemClass extends Class = Class> extends Injector {
28
+ static warnNoMatchingEntities: boolean;
29
+ static entityComponents: Map<EntityClass, Map<ComponentClass, string>>;
30
+ protected componentDependencyMap: Map<Class, ComponentDependency[]>;
31
+ protected dynamicComponentDependencyMap: Map<Class, DynamicComponentDependency[]>;
17
32
  protected entitySystemMap: Map<Class, SystemClass[]>;
18
- protected entitySystemResolverTuples: Map<Class, SystemResolvers<SystemClass>[]>;
19
- private signalMap;
33
+ protected dynamicComponentSystemMap: Map<Class, SystemClass[]>;
34
+ protected entitySystemResolverTuples: Map<Class, Map<SystemClass, ResolverFunction[]>>;
35
+ protected registeredClasses: Set<SystemClass>;
36
+ private readonly signalMap;
20
37
  constructor(parent?: EcsInjector<SystemClass>);
21
38
  registerSystem<T extends SystemClass>(systemClass: T): void;
22
- createSystems(entity: Object): SystemContext[];
39
+ createSystemsForDynamicComponents(dynamicComponentClass: Class, entity: Entity): Set<SystemContext<import("./types").System>> | undefined;
40
+ createSystemsForEntity(entity: Entity): Set<SystemContext>;
41
+ private createSystems;
42
+ private getSystemResolverTuples;
23
43
  getSignal<T>(id: ID): Signal<T>;
24
44
  dispose(): void;
25
45
  }
@@ -1,41 +1,58 @@
1
- import { createDependencyAnnotation, Injector, putIfAbsent } from 'mani-injector';
2
- import { Signal } from 'mani-signal';
3
- import { SystemContext } from './systemContext';
4
- export const entityComponents = new Map();
5
- export const GetComponent = createDependencyAnnotation((type, index) => ({ kind: 'component', type, index }));
6
- export const GetEntity = createDependencyAnnotation((_type, index) => ({ kind: 'entity', index }));
7
- export const GetContext = createDependencyAnnotation((_type, index) => ({ kind: 'context', index }));
8
- export const GetSignal = (id) => createDependencyAnnotation((_type, index) => ({ kind: 'signal', index, id }));
9
- export const EntityComponent = (target, propertyKey) => {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EcsInjector = exports.OnSignal = exports.staticSignalHandlers = exports.signalHandlers = exports.EntityComponent = exports.GetEntitySignal = exports.GetSignal = exports.GetContext = exports.GetEntity = exports.GetDynamicComponent = exports.GetComponent = void 0;
4
+ const mani_signal_1 = require("mani-signal");
5
+ const injector_1 = require("./injector");
6
+ const systemContext_1 = require("./systemContext");
7
+ exports.GetComponent = (0, injector_1.createDependencyAnnotation)((type, index) => ({ kind: 'component', type, index }));
8
+ exports.GetDynamicComponent = (0, injector_1.createDependencyAnnotation)((type, index) => ({
9
+ kind: 'dynamic',
10
+ type,
11
+ index,
12
+ }));
13
+ exports.GetEntity = (0, injector_1.createDependencyAnnotation)((_type, index) => ({ kind: 'entity', index }));
14
+ exports.GetContext = (0, injector_1.createDependencyAnnotation)((_type, index) => ({ kind: 'context', index }));
15
+ const GetSignal = (id) => (0, injector_1.createDependencyAnnotation)((_type, index) => ({ kind: 'signal', index, id }));
16
+ exports.GetSignal = GetSignal;
17
+ const GetEntitySignal = (id) => (0, injector_1.createDependencyAnnotation)((_type, index) => ({
18
+ kind: 'entitySignal',
19
+ index,
20
+ id,
21
+ }));
22
+ exports.GetEntitySignal = GetEntitySignal;
23
+ const EntityComponent = (target, propertyKey) => {
10
24
  const entityClass = target.constructor;
11
25
  const componentClass = Reflect.getMetadata('design:type', target, propertyKey);
12
26
  if (componentClass === Object) {
13
27
  throw new Error(`Object component type not allowed. Forgot to specify type of ${entityClass.name}.${propertyKey}?`);
14
28
  }
15
- const componentSet = putIfAbsent(entityComponents, entityClass, () => new Map());
29
+ const componentSet = (0, injector_1.putIfAbsent)(EcsInjector.entityComponents, entityClass, () => new Map());
16
30
  componentSet.set(componentClass, propertyKey);
17
31
  };
18
- export const signalHandlers = new Map();
19
- export const staticSignalHandlers = new Map();
20
- export const OnSignal = (id) => (target, propertyKey, descriptor) => {
32
+ exports.EntityComponent = EntityComponent;
33
+ exports.signalHandlers = new Map();
34
+ exports.staticSignalHandlers = new Map();
35
+ const OnSignal = (id) => (target, propertyKey, descriptor) => {
21
36
  if (target instanceof Function) {
22
- let mappingList = putIfAbsent(staticSignalHandlers, target, () => []);
37
+ let mappingList = (0, injector_1.putIfAbsent)(exports.staticSignalHandlers, target, () => []);
23
38
  mappingList.push([propertyKey, id]);
24
39
  }
25
40
  else {
26
- let mappingList = putIfAbsent(signalHandlers, target.constructor, () => []);
41
+ let mappingList = (0, injector_1.putIfAbsent)(exports.signalHandlers, target.constructor, () => []);
27
42
  mappingList.push([propertyKey, id]);
28
43
  }
29
44
  };
30
- const getComponentDependencies = (system) => {
31
- const dependencies = Injector.dependencyMap.get(system);
32
- return dependencies
33
- ? dependencies.filter((dependency) => dependency.kind === 'component')
34
- : [];
45
+ exports.OnSignal = OnSignal;
46
+ const extractDependenciesWithType = (type) => {
47
+ const result = new Map();
48
+ injector_1.Injector.dependencyMap.forEach((dependency, system) => {
49
+ (0, injector_1.putIfAbsent)(result, system, () => [...dependency.filter((dependency) => dependency.kind === type)]);
50
+ });
51
+ return result;
35
52
  };
36
53
  const getEntityClassesForComponentDependencies = (componentTypes) => {
37
54
  const result = [];
38
- for (const [entityClass, componentMap] of entityComponents) {
55
+ for (const [entityClass, componentMap] of EcsInjector.entityComponents) {
39
56
  let allDependenciesMet = true;
40
57
  for (const componentType of componentTypes) {
41
58
  if (!componentMap.has(componentType)) {
@@ -56,80 +73,144 @@ const componentResolver = (context, dependency) => {
56
73
  throw new Error(`Could not resolve Component ${type.name}. @GetComponent only allowed in system scope.`);
57
74
  }
58
75
  const entityClass = context.entityClass;
59
- const key = entityComponents.get(entityClass).get(dependency.type);
76
+ const key = EcsInjector.entityComponents.get(entityClass).get(dependency.type);
60
77
  return (context) => context.entity[key];
61
78
  };
79
+ const dynamicComponentResolver = (context, dependency) => {
80
+ const kind = context.kind;
81
+ const type = context.type;
82
+ if (kind !== 'system') {
83
+ throw new Error(`Could not resolve Component ${type.name}. @GetComponent only allowed in system scope.`);
84
+ }
85
+ return (context) => context.entity.getDynamicComponent(dependency.type);
86
+ };
62
87
  const entityResolver = ({ type, kind }, _dependency) => {
63
88
  if (kind !== 'system') {
64
89
  throw new Error(`Could not resolve Entity in ${type.name}. @GetEntity only allowed in system scope.`);
65
90
  }
66
91
  return (context) => context.entity;
67
92
  };
93
+ const entitySignalResolver = ({ type, kind }, dependency) => {
94
+ if (kind !== 'system') {
95
+ throw new Error(`Could not resolve Entity in ${type.name}. @GetEntity only allowed in system scope.`);
96
+ }
97
+ return (context) => context.entity.getSignal(dependency.id);
98
+ };
68
99
  const contextResolver = ({ type, kind }, _dependency) => {
69
100
  if (kind !== 'system') {
70
101
  throw new Error(`Could not resolve Context in ${type.name}. @GetContext only allowed in system scope.`);
71
102
  }
72
103
  return (context) => context;
73
104
  };
74
- export class EcsInjector extends Injector {
105
+ class EcsInjector extends injector_1.Injector {
75
106
  constructor(parent) {
76
107
  super(parent);
108
+ this.componentDependencyMap = extractDependenciesWithType('component');
109
+ this.dynamicComponentDependencyMap = extractDependenciesWithType('dynamic');
110
+ this.registeredClasses = new Set();
77
111
  this.signalMap = parent ? parent.signalMap : new Map();
78
- this.entitySystemMap = parent ? parent.entitySystemMap : new Map();
79
- this.entitySystemResolverTuples = parent ? parent.entitySystemResolverTuples : new Map();
112
+ this.entitySystemMap = new Map();
113
+ this.dynamicComponentSystemMap = new Map();
114
+ this.entitySystemResolverTuples = new Map();
80
115
  this.map(EcsInjector).toValue(this);
81
116
  if (!parent) {
82
117
  this.addExtensionResolver('entity', entityResolver);
83
118
  this.addExtensionResolver('component', componentResolver);
119
+ this.addExtensionResolver('dynamic', dynamicComponentResolver);
84
120
  this.addExtensionResolver('context', contextResolver);
85
121
  const signalResolver = ({ kind }, dependency) => {
86
- const signal = putIfAbsent(this.signalMap, dependency.id, () => new Signal());
122
+ const signal = (0, injector_1.putIfAbsent)(this.signalMap, dependency.id, () => new mani_signal_1.Signal());
87
123
  return (entity) => signal;
88
124
  };
89
125
  this.addExtensionResolver('signal', signalResolver);
126
+ this.addExtensionResolver('entitySignal', entitySignalResolver);
90
127
  }
91
128
  }
92
129
  registerSystem(systemClass) {
93
- const componentDependencies = getComponentDependencies(systemClass).map(dependency => dependency.type);
130
+ var _a;
131
+ if (this.registeredClasses.has(systemClass)) {
132
+ throw new Error(`System ${systemClass.name} already registered.`);
133
+ }
134
+ this.registeredClasses.add(systemClass);
135
+ const componentDependencies = ((_a = this.componentDependencyMap.get(systemClass)) === null || _a === void 0 ? void 0 : _a.map(it => it.type)) || [];
136
+ const dynamicComponentDependencies = this.dynamicComponentDependencyMap.get(systemClass) || [];
137
+ for (const dynamicComponentDependency of dynamicComponentDependencies) {
138
+ const systemClassesForDynamicComponent = (0, injector_1.putIfAbsent)(this.dynamicComponentSystemMap, dynamicComponentDependency.type, () => []);
139
+ systemClassesForDynamicComponent.push(systemClass);
140
+ }
94
141
  if (componentDependencies.length === 0) {
95
142
  throw new Error(`${systemClass.name} needs at least one component dependency.`);
96
143
  }
97
144
  const entityClasses = getEntityClassesForComponentDependencies(componentDependencies);
98
- if (entityClasses.length === 0) {
145
+ if (entityClasses.length === 0 && EcsInjector.warnNoMatchingEntities) {
99
146
  console.warn(`System '${systemClass.name}' has no matching entities.`);
100
147
  return;
101
148
  }
102
149
  for (let entityClass of entityClasses) {
103
- const systemClasses = putIfAbsent(this.entitySystemMap, entityClass, () => []);
150
+ const systemClasses = (0, injector_1.putIfAbsent)(this.entitySystemMap, entityClass, () => []);
104
151
  systemClasses.push(systemClass);
105
152
  }
106
153
  }
107
- createSystems(entity) {
108
- const systemResolverTuples = putIfAbsent(this.entitySystemResolverTuples, entity.constructor, () => {
109
- const systems = this.entitySystemMap.get(entity.constructor);
110
- if (!systems) {
111
- return [];
112
- }
113
- const result = [];
114
- for (const systemClass of systems) {
115
- result.push([systemClass, this.createResolverArray({ type: systemClass, kind: 'system', entityClass: entity.constructor })]);
154
+ createSystemsForDynamicComponents(dynamicComponentClass, entity) {
155
+ const potentialSystems = this.getSystemResolverTuples(entity);
156
+ const systems = this.dynamicComponentSystemMap.get(dynamicComponentClass);
157
+ if (!systems) {
158
+ return undefined;
159
+ }
160
+ const finalSystemResolverEntries = new Map();
161
+ for (const system of systems) {
162
+ const resolverTuples = potentialSystems.get(system);
163
+ if (resolverTuples) {
164
+ finalSystemResolverEntries.set(system, resolverTuples);
116
165
  }
117
- return result;
118
- });
119
- const systemInstances = [];
120
- for (const [system, resolver] of systemResolverTuples) {
166
+ }
167
+ return this.createSystems(finalSystemResolverEntries, entity);
168
+ }
169
+ createSystemsForEntity(entity) {
170
+ const systemResolverTuples = this.getSystemResolverTuples(entity);
171
+ return this.createSystems(systemResolverTuples, entity);
172
+ }
173
+ createSystems(systemResolverMap, entity) {
174
+ const systemInstances = new Set();
175
+ for (const [system, resolver] of systemResolverMap) {
121
176
  const args = new Array(resolver.length);
122
- const systemContext = new SystemContext(entity);
177
+ const dynamicDependencies = this.dynamicComponentDependencyMap.get(system) || [];
178
+ const hasUnfulfilledDynamicDependencies = dynamicDependencies.some((dependency, index) => {
179
+ return !entity.hasDynamicComponentClass(dependency.type);
180
+ });
181
+ if (hasUnfulfilledDynamicDependencies) {
182
+ continue;
183
+ }
184
+ const systemContext = new systemContext_1.SystemContext(entity);
123
185
  for (let i = 0; i < args.length; i++) {
124
186
  args[i] = resolver[i](systemContext);
125
187
  }
126
- systemContext.system = new system(...args);
127
- systemInstances.push(systemContext);
188
+ const newSystem = new system(...args);
189
+ const entitySignals = systemContext_1.entitySignalHandlers.get(system);
190
+ if (entitySignals === null || entitySignals === void 0 ? void 0 : entitySignals.length) {
191
+ for (const [propertyKey, id] of entitySignals) {
192
+ entity.getSignal(id).add(newSystem[propertyKey], { context: newSystem });
193
+ }
194
+ }
195
+ systemContext.system = newSystem;
196
+ systemInstances.add(systemContext);
128
197
  }
129
198
  return systemInstances;
130
199
  }
200
+ getSystemResolverTuples(entity) {
201
+ return (0, injector_1.putIfAbsent)(this.entitySystemResolverTuples, entity.constructor, () => {
202
+ const systems = this.entitySystemMap.get(entity.constructor);
203
+ if (!systems)
204
+ return [];
205
+ const result = new Map();
206
+ for (const systemClass of systems) {
207
+ result.set(systemClass, this.createResolverArray({ type: systemClass, kind: 'system', entityClass: entity.constructor }));
208
+ }
209
+ return result;
210
+ });
211
+ }
131
212
  getSignal(id) {
132
- return putIfAbsent(this.signalMap, id, () => new Signal());
213
+ return (0, injector_1.putIfAbsent)(this.signalMap, id, () => new mani_signal_1.Signal());
133
214
  }
134
215
  dispose() {
135
216
  for (const [key, signal] of this.signalMap) {
@@ -137,4 +218,7 @@ export class EcsInjector extends Injector {
137
218
  }
138
219
  }
139
220
  }
221
+ exports.EcsInjector = EcsInjector;
222
+ EcsInjector.warnNoMatchingEntities = true;
223
+ EcsInjector.entityComponents = new Map();
140
224
  //# sourceMappingURL=ecsInjector.js.map