dacha 0.14.2 → 0.14.3
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/build/engine/consts.d.ts +2 -0
- package/build/engine/consts.js +2 -0
- package/build/engine/engine.js +6 -2
- package/build/engine/game-loop.d.ts +7 -1
- package/build/engine/game-loop.js +15 -7
- package/build/engine/scene/scene-provider.d.ts +2 -2
- package/build/engine/scene/scene-provider.js +1 -4
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/package.json +1 -1
package/build/engine/engine.js
CHANGED
|
@@ -30,7 +30,7 @@ export class Engine {
|
|
|
30
30
|
this.addWindowListeners();
|
|
31
31
|
return;
|
|
32
32
|
}
|
|
33
|
-
const { config: { templates, scenes, levels, loaders, startSceneId, startLoaderId, globalOptions, }, systems, components, resources = {}, } = this.options;
|
|
33
|
+
const { config: { templates, scenes, levels, loaders, startSceneId, startLoaderId, globalOptions: rawGlobalOptions, }, systems, components, resources = {}, } = this.options;
|
|
34
34
|
if (!startSceneId) {
|
|
35
35
|
throw new Error('Can\'t start the engine without starting scene. Please specify start scene id.');
|
|
36
36
|
}
|
|
@@ -49,6 +49,10 @@ export class Engine {
|
|
|
49
49
|
templateCollection.register(templates[i]);
|
|
50
50
|
}
|
|
51
51
|
const actorCreator = new ActorCreator(components, templateCollection);
|
|
52
|
+
const globalOptions = rawGlobalOptions.reduce((acc, option) => {
|
|
53
|
+
acc[option.name] = option.options;
|
|
54
|
+
return acc;
|
|
55
|
+
}, {});
|
|
52
56
|
this.sceneProvider = new SceneProvider({
|
|
53
57
|
scenes,
|
|
54
58
|
levels,
|
|
@@ -71,7 +75,7 @@ export class Engine {
|
|
|
71
75
|
}
|
|
72
76
|
this.gameLoop = new GameLoop(this.sceneProvider, [
|
|
73
77
|
new SceneController({ sceneProvider: this.sceneProvider }),
|
|
74
|
-
]);
|
|
78
|
+
], globalOptions.performance);
|
|
75
79
|
this.gameLoop.run();
|
|
76
80
|
this.addWindowListeners();
|
|
77
81
|
}
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import type { SceneProvider } from './scene';
|
|
2
2
|
import type { Controller } from './controllers';
|
|
3
|
+
export interface PerformanceSettings {
|
|
4
|
+
maxFPS: number;
|
|
5
|
+
fixedUpdateRate: number;
|
|
6
|
+
}
|
|
3
7
|
export declare class GameLoop {
|
|
4
8
|
private sceneProvider;
|
|
5
9
|
private controllers;
|
|
10
|
+
private msPerUpdate;
|
|
11
|
+
private msPerFixedUpdate;
|
|
6
12
|
private gameLoopId;
|
|
7
13
|
private previous;
|
|
8
14
|
private lag;
|
|
9
15
|
private bindedTick;
|
|
10
|
-
constructor(sceneProvider: SceneProvider, controllers:
|
|
16
|
+
constructor(sceneProvider: SceneProvider, controllers: Controller[], performance?: PerformanceSettings);
|
|
11
17
|
private tick;
|
|
12
18
|
run(): void;
|
|
13
19
|
stop(): void;
|
|
@@ -1,32 +1,41 @@
|
|
|
1
1
|
import { eventQueue } from './event-target';
|
|
2
|
-
const
|
|
2
|
+
const DEFAULT_FIXED_UPDATE_RATE = 50;
|
|
3
|
+
const DEFAULT_MAX_FPS = Infinity;
|
|
3
4
|
export class GameLoop {
|
|
4
5
|
sceneProvider;
|
|
5
6
|
controllers;
|
|
7
|
+
msPerUpdate;
|
|
8
|
+
msPerFixedUpdate;
|
|
6
9
|
gameLoopId;
|
|
7
10
|
previous;
|
|
8
11
|
lag;
|
|
9
12
|
bindedTick;
|
|
10
|
-
constructor(sceneProvider, controllers) {
|
|
13
|
+
constructor(sceneProvider, controllers, performance) {
|
|
11
14
|
this.sceneProvider = sceneProvider;
|
|
12
15
|
this.controllers = controllers;
|
|
16
|
+
this.msPerUpdate = 1000 / (performance?.maxFPS || DEFAULT_MAX_FPS);
|
|
17
|
+
this.msPerFixedUpdate = 1000 / (performance?.fixedUpdateRate || DEFAULT_FIXED_UPDATE_RATE);
|
|
13
18
|
this.gameLoopId = 0;
|
|
14
19
|
this.previous = 0;
|
|
15
20
|
this.lag = 0;
|
|
16
21
|
this.bindedTick = this.tick.bind(this);
|
|
17
22
|
}
|
|
18
23
|
tick() {
|
|
19
|
-
|
|
24
|
+
this.gameLoopId = requestAnimationFrame(this.bindedTick);
|
|
20
25
|
const current = performance.now();
|
|
21
26
|
const elapsed = current - this.previous;
|
|
27
|
+
if (elapsed < this.msPerUpdate) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
22
30
|
this.lag += elapsed;
|
|
31
|
+
eventQueue.update();
|
|
23
32
|
const currentScene = this.sceneProvider.getCurrentScene();
|
|
24
|
-
const fixedUpdateOptions = { deltaTime:
|
|
25
|
-
while (this.lag >=
|
|
33
|
+
const fixedUpdateOptions = { deltaTime: this.msPerFixedUpdate };
|
|
34
|
+
while (this.lag >= this.msPerFixedUpdate) {
|
|
26
35
|
currentScene?.systems.forEach((system) => {
|
|
27
36
|
system.fixedUpdate?.(fixedUpdateOptions);
|
|
28
37
|
});
|
|
29
|
-
this.lag -=
|
|
38
|
+
this.lag -= this.msPerFixedUpdate;
|
|
30
39
|
}
|
|
31
40
|
const options = { deltaTime: elapsed };
|
|
32
41
|
currentScene?.systems.forEach((system) => {
|
|
@@ -36,7 +45,6 @@ export class GameLoop {
|
|
|
36
45
|
controller.update();
|
|
37
46
|
});
|
|
38
47
|
this.previous = current;
|
|
39
|
-
this.gameLoopId = requestAnimationFrame(this.bindedTick);
|
|
40
48
|
}
|
|
41
49
|
run() {
|
|
42
50
|
this.previous = performance.now();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { SceneConfig, LevelConfig
|
|
1
|
+
import type { SceneConfig, LevelConfig } from '../types';
|
|
2
2
|
import type { ActorCreator } from '../actor';
|
|
3
3
|
import type { SystemConstructor } from '../system';
|
|
4
4
|
import type { TemplateCollection } from '../template';
|
|
@@ -9,7 +9,7 @@ interface SceneProviderOptions {
|
|
|
9
9
|
levels: Array<LevelConfig>;
|
|
10
10
|
systems: Array<SystemConstructor>;
|
|
11
11
|
resources: Record<string, unknown>;
|
|
12
|
-
globalOptions:
|
|
12
|
+
globalOptions: Record<string, unknown>;
|
|
13
13
|
actorCreator: ActorCreator;
|
|
14
14
|
templateCollection: TemplateCollection;
|
|
15
15
|
}
|
|
@@ -28,10 +28,7 @@ export class SceneProvider {
|
|
|
28
28
|
}, {});
|
|
29
29
|
this.systems = systems;
|
|
30
30
|
this.resources = resources;
|
|
31
|
-
this.globalOptions = globalOptions
|
|
32
|
-
acc[option.name] = option.options;
|
|
33
|
-
return acc;
|
|
34
|
-
}, {});
|
|
31
|
+
this.globalOptions = globalOptions;
|
|
35
32
|
this.loadedScene = void 0;
|
|
36
33
|
this.actorCreator = actorCreator;
|
|
37
34
|
this.templateCollection = templateCollection;
|
package/build/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { Engine } from './engine';
|
|
2
2
|
export { Component } from './engine/component';
|
|
3
3
|
export { VectorOps, MathOps, Vector2 } from './engine/math-lib';
|
|
4
|
+
export * from './engine/consts';
|
|
4
5
|
export * from './engine/types';
|
|
5
6
|
export { System } from './engine/system';
|
|
6
7
|
export type { SystemOptions, UpdateOptions, } from './engine/system';
|
package/build/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { Engine } from './engine';
|
|
2
2
|
export { Component } from './engine/component';
|
|
3
3
|
export { VectorOps, MathOps, Vector2 } from './engine/math-lib';
|
|
4
|
+
export * from './engine/consts';
|
|
4
5
|
export * from './engine/types';
|
|
5
6
|
export { System } from './engine/system';
|
|
6
7
|
export * as Animation from './contrib/components/animatable/types';
|