mani-game-engine 1.0.0-pre.12 → 1.0.0-pre.13

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,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2020 Jan Mankopf
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Jan Mankopf
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,69 +1,69 @@
1
-
2
- #### Use entity component system
3
- ```typescript
4
- import {EntityComponent, GetComponent, GetEntity, Inject, Injector} from 'mani-injector';
5
-
6
- class Service {}
7
-
8
- class FooComponent {}
9
- class BarComponent {}
10
-
11
- class FirstEntity {
12
- @EntityComponent
13
- foo: FooComponent = new FooComponent();
14
- }
15
-
16
- class SecondEntity {
17
- @EntityComponent
18
- foo: FooComponent = new FooComponent();
19
- @EntityComponent
20
- bar: BarComponent = new BarComponent();
21
- }
22
-
23
- // this system is created for every entity that has a FooComponent
24
- class FooSystem {
25
- constructor(
26
- // get a reference to the component when the system is created for a specific entity
27
- @GetComponent readonly foo: FooComponent,
28
- // you can get the entity object
29
- @GetEntity readonly entity:Object
30
- ) {}
31
- }
32
-
33
- // this system is created for every entity that has a BarComponent
34
- class BarSystem {
35
- constructor(
36
- @GetComponent readonly bar: BarComponent,
37
- ) {}
38
- }
39
-
40
- // this system is created for every entity that has a FooComponent and a BarComponent
41
- class FooBarSystem {
42
- constructor(
43
- @GetComponent readonly foo: FooComponent,
44
- @GetComponent readonly bar: BarComponent,
45
- // you can inject everything that is mapped in the injector
46
- @Inject readonly service: Service
47
- ) {}
48
- }
49
-
50
-
51
- const injector = new Injector();
52
-
53
- injector.registerSystem(FooSystem);
54
- injector.registerSystem(BarSystem);
55
- injector.registerSystem(FooBarSystem);
56
- injector.map(Service).toSingleton();
57
-
58
- const entity1 = new FirstEntity();
59
- const entity2 = new SecondEntity();
60
-
61
- // array with one instance of FooSystem with a reference to the component
62
- console.log(injector.createSystems(entity1));
63
-
64
- // array with 3 systems
65
- // one instance of FooSystem
66
- // one instance of BarSystem
67
- // one instance of FooBarSystem
68
- console.log(injector.createSystems(entity2));
69
- ```
1
+
2
+ #### Use entity component system
3
+ ```typescript
4
+ import {EntityComponent, GetComponent, GetEntity, Inject, Injector} from 'mani-injector';
5
+
6
+ class Service {}
7
+
8
+ class FooComponent {}
9
+ class BarComponent {}
10
+
11
+ class FirstEntity {
12
+ @EntityComponent
13
+ foo: FooComponent = new FooComponent();
14
+ }
15
+
16
+ class SecondEntity {
17
+ @EntityComponent
18
+ foo: FooComponent = new FooComponent();
19
+ @EntityComponent
20
+ bar: BarComponent = new BarComponent();
21
+ }
22
+
23
+ // this system is created for every entity that has a FooComponent
24
+ class FooSystem {
25
+ constructor(
26
+ // get a reference to the component when the system is created for a specific entity
27
+ @GetComponent readonly foo: FooComponent,
28
+ // you can get the entity object
29
+ @GetEntity readonly entity:Object
30
+ ) {}
31
+ }
32
+
33
+ // this system is created for every entity that has a BarComponent
34
+ class BarSystem {
35
+ constructor(
36
+ @GetComponent readonly bar: BarComponent,
37
+ ) {}
38
+ }
39
+
40
+ // this system is created for every entity that has a FooComponent and a BarComponent
41
+ class FooBarSystem {
42
+ constructor(
43
+ @GetComponent readonly foo: FooComponent,
44
+ @GetComponent readonly bar: BarComponent,
45
+ // you can inject everything that is mapped in the injector
46
+ @Inject readonly service: Service
47
+ ) {}
48
+ }
49
+
50
+
51
+ const injector = new Injector();
52
+
53
+ injector.registerSystem(FooSystem);
54
+ injector.registerSystem(BarSystem);
55
+ injector.registerSystem(FooBarSystem);
56
+ injector.map(Service).toSingleton();
57
+
58
+ const entity1 = new FirstEntity();
59
+ const entity2 = new SecondEntity();
60
+
61
+ // array with one instance of FooSystem with a reference to the component
62
+ console.log(injector.createSystems(entity1));
63
+
64
+ // array with 3 systems
65
+ // one instance of FooSystem
66
+ // one instance of BarSystem
67
+ // one instance of FooBarSystem
68
+ console.log(injector.createSystems(entity2));
69
+ ```
package/lib/clock.d.ts CHANGED
@@ -1,40 +1,40 @@
1
- import { Signal } from 'mani-signal';
2
- import { OnEarlyUpdateParams, OnFixedUpdateParams, OnUpdateParams } from './types';
3
- declare const defaultOptions: {
4
- autoStart: boolean;
5
- fixedDeltaTime: number;
6
- maxFrameTime: number;
7
- timeScale: number;
8
- };
9
- export declare type GameClockOptions = Partial<typeof defaultOptions>;
10
- export interface Clock {
11
- onPrepare: Signal<unknown>;
12
- onEarlyUpdate: Signal<OnEarlyUpdateParams>;
13
- onUpdate: Signal<OnUpdateParams>;
14
- onFixedUpdate: Signal<OnFixedUpdateParams>;
15
- timeScale: number;
16
- start(): this;
17
- stop(): this;
18
- }
19
- export declare class GameClock implements Clock {
20
- private isRunning;
21
- private requestId;
22
- private currentTime;
23
- private accumulator;
24
- private gameTime;
25
- private readonly _fixedDeltaTime;
26
- private update;
27
- private maxFrameTime;
28
- readonly onPrepare: Signal<unknown>;
29
- readonly onEarlyUpdate: Signal<OnEarlyUpdateParams>;
30
- readonly onUpdate: Signal<OnUpdateParams>;
31
- readonly onFixedUpdate: Signal<OnFixedUpdateParams>;
32
- constructor(options?: GameClockOptions);
33
- get fixedDeltaTime(): number;
34
- private _timeScale;
35
- get timeScale(): number;
36
- set timeScale(value: number);
37
- start(): this;
38
- stop(): this;
39
- }
40
- export {};
1
+ import { Signal } from 'mani-signal';
2
+ import { OnEarlyUpdateParams, OnFixedUpdateParams, OnUpdateParams } from './types';
3
+ declare const defaultOptions: {
4
+ autoStart: boolean;
5
+ fixedDeltaTime: number;
6
+ maxFrameTime: number;
7
+ timeScale: number;
8
+ };
9
+ export declare type GameClockOptions = Partial<typeof defaultOptions>;
10
+ export interface Clock {
11
+ onPrepare: Signal<unknown>;
12
+ onEarlyUpdate: Signal<OnEarlyUpdateParams>;
13
+ onUpdate: Signal<OnUpdateParams>;
14
+ onFixedUpdate: Signal<OnFixedUpdateParams>;
15
+ timeScale: number;
16
+ start(): this;
17
+ stop(): this;
18
+ }
19
+ export declare class GameClock implements Clock {
20
+ private isRunning;
21
+ private requestId;
22
+ private currentTime;
23
+ private accumulator;
24
+ private gameTime;
25
+ private readonly _fixedDeltaTime;
26
+ private update;
27
+ private readonly maxFrameTime;
28
+ readonly onPrepare: Signal<unknown>;
29
+ readonly onEarlyUpdate: Signal<OnEarlyUpdateParams>;
30
+ readonly onUpdate: Signal<OnUpdateParams>;
31
+ readonly onFixedUpdate: Signal<OnFixedUpdateParams>;
32
+ constructor(options?: GameClockOptions);
33
+ get fixedDeltaTime(): number;
34
+ private _timeScale;
35
+ get timeScale(): number;
36
+ set timeScale(value: number);
37
+ start(): this;
38
+ stop(): this;
39
+ }
40
+ export {};
package/lib/clock.js CHANGED
@@ -1,80 +1,80 @@
1
- import { Signal } from 'mani-signal';
2
- const MIN_TIME_SCALE = 0.00001;
3
- const defaultOptions = {
4
- autoStart: true,
5
- fixedDeltaTime: 1 / 60,
6
- maxFrameTime: 0.25,
7
- timeScale: 1,
8
- };
9
- export class GameClock {
10
- constructor(options) {
11
- this.isRunning = false;
12
- this.requestId = 0;
13
- this.currentTime = 0;
14
- 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();
50
- this._timeScale = 1;
51
- const opts = Object.assign(Object.assign({}, defaultOptions), options);
52
- this._fixedDeltaTime = opts.fixedDeltaTime;
53
- this.maxFrameTime = opts.maxFrameTime;
54
- this.timeScale = opts.timeScale;
55
- opts.autoStart && this.start();
56
- }
57
- get fixedDeltaTime() { return this._fixedDeltaTime; }
58
- get timeScale() {
59
- return this._timeScale;
60
- }
61
- set timeScale(value) {
62
- this._timeScale = Math.max(value, MIN_TIME_SCALE);
63
- }
64
- start() {
65
- if (this.isRunning) {
66
- return this;
67
- }
68
- this.requestId = requestAnimationFrame(this.update);
69
- return this;
70
- }
71
- stop() {
72
- if (!this.isRunning) {
73
- return this;
74
- }
75
- this.isRunning = false;
76
- cancelAnimationFrame(this.requestId);
77
- return this;
78
- }
79
- }
1
+ import { Signal } from 'mani-signal';
2
+ const MIN_TIME_SCALE = 0.00001;
3
+ const defaultOptions = {
4
+ autoStart: true,
5
+ fixedDeltaTime: 1 / 60,
6
+ maxFrameTime: 0.25,
7
+ timeScale: 1,
8
+ };
9
+ export class GameClock {
10
+ constructor(options) {
11
+ this.isRunning = false;
12
+ this.requestId = 0;
13
+ this.currentTime = 0;
14
+ 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();
50
+ this._timeScale = 1;
51
+ const opts = Object.assign(Object.assign({}, defaultOptions), options);
52
+ this._fixedDeltaTime = opts.fixedDeltaTime;
53
+ this.maxFrameTime = opts.maxFrameTime;
54
+ this.timeScale = opts.timeScale;
55
+ opts.autoStart && this.start();
56
+ }
57
+ get fixedDeltaTime() { return this._fixedDeltaTime; }
58
+ get timeScale() {
59
+ return this._timeScale;
60
+ }
61
+ set timeScale(value) {
62
+ this._timeScale = Math.max(value, MIN_TIME_SCALE);
63
+ }
64
+ start() {
65
+ if (this.isRunning) {
66
+ return this;
67
+ }
68
+ this.requestId = requestAnimationFrame(this.update);
69
+ return this;
70
+ }
71
+ stop() {
72
+ if (!this.isRunning) {
73
+ return this;
74
+ }
75
+ this.isRunning = false;
76
+ cancelAnimationFrame(this.requestId);
77
+ return this;
78
+ }
79
+ }
80
80
  //# sourceMappingURL=clock.js.map
@@ -1,26 +1,26 @@
1
- import { Class, ID, Injector, ResolverFunction } from 'mani-injector';
2
- import { Signal } from 'mani-signal';
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;
12
- export declare const EntityComponent: (target: object, propertyKey: string) => any;
13
- export declare const signalHandlers: Map<Object, [string, ID][]>;
14
- export declare const staticSignalHandlers: Map<Object, [string, ID][]>;
15
- export declare const OnSignal: (id: ID) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
16
- export declare class EcsInjector<SystemClass extends Class = Class> extends Injector {
17
- protected entitySystemMap: Map<Class, SystemClass[]>;
18
- protected entitySystemResolverTuples: Map<Class, SystemResolvers<SystemClass>[]>;
19
- private signalMap;
20
- constructor(parent?: EcsInjector<SystemClass>);
21
- registerSystem<T extends SystemClass>(systemClass: T): void;
22
- createSystems(entity: Object): SystemContext[];
23
- getSignal<T>(id: ID): Signal<T>;
24
- dispose(): void;
25
- }
26
- export {};
1
+ import { Class, ID, Injector, ResolverFunction } from 'mani-injector';
2
+ import { Signal } from 'mani-signal';
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;
12
+ export declare const EntityComponent: (target: object, propertyKey: string) => any;
13
+ export declare const signalHandlers: Map<Object, [string, ID][]>;
14
+ export declare const staticSignalHandlers: Map<Object, [string, ID][]>;
15
+ export declare const OnSignal: (id: ID) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
16
+ export declare class EcsInjector<SystemClass extends Class = Class> extends Injector {
17
+ protected entitySystemMap: Map<Class, SystemClass[]>;
18
+ protected entitySystemResolverTuples: Map<Class, SystemResolvers<SystemClass>[]>;
19
+ private signalMap;
20
+ constructor(parent?: EcsInjector<SystemClass>);
21
+ registerSystem<T extends SystemClass>(systemClass: T): void;
22
+ createSystems(entity: Object): SystemContext[];
23
+ getSignal<T>(id: ID): Signal<T>;
24
+ dispose(): void;
25
+ }
26
+ export {};