@remvst/game-navigation-core 1.0.0

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.
Files changed (44) hide show
  1. package/lib/game/game-params.d.ts +6 -0
  2. package/lib/game/game-params.js +3 -0
  3. package/lib/game/game-plugin.d.ts +11 -0
  4. package/lib/game/game-plugin.js +14 -0
  5. package/lib/game/game.d.ts +32 -0
  6. package/lib/game/game.js +113 -0
  7. package/lib/index.d.ts +13 -0
  8. package/lib/index.js +29 -0
  9. package/lib/interaction/inputs.d.ts +28 -0
  10. package/lib/interaction/inputs.js +103 -0
  11. package/lib/interaction/screen-mouse-inputs.d.ts +9 -0
  12. package/lib/interaction/screen-mouse-inputs.js +20 -0
  13. package/lib/navigation/resolver.d.ts +15 -0
  14. package/lib/navigation/resolver.js +35 -0
  15. package/lib/navigation/screen-stack.d.ts +22 -0
  16. package/lib/navigation/screen-stack.js +93 -0
  17. package/lib/plugins/automation-game-plugin.d.ts +14 -0
  18. package/lib/plugins/automation-game-plugin.js +34 -0
  19. package/lib/plugins/blur-pausing-game-plugin.d.ts +9 -0
  20. package/lib/plugins/blur-pausing-game-plugin.js +28 -0
  21. package/lib/plugins/time-keys-game-plugin.d.ts +8 -0
  22. package/lib/plugins/time-keys-game-plugin.js +26 -0
  23. package/lib/ui/screen-delegate.d.ts +6 -0
  24. package/lib/ui/screen-delegate.js +2 -0
  25. package/lib/ui/screen.d.ts +38 -0
  26. package/lib/ui/screen.js +130 -0
  27. package/lib/util/math.d.ts +1 -0
  28. package/lib/util/math.js +12 -0
  29. package/package.json +21 -0
  30. package/src/game/game-params.ts +6 -0
  31. package/src/game/game-plugin.ts +23 -0
  32. package/src/game/game.ts +153 -0
  33. package/src/index.ts +18 -0
  34. package/src/interaction/inputs.ts +118 -0
  35. package/src/interaction/screen-mouse-inputs.ts +22 -0
  36. package/src/navigation/resolver.ts +49 -0
  37. package/src/navigation/screen-stack.ts +112 -0
  38. package/src/plugins/automation-game-plugin.ts +40 -0
  39. package/src/plugins/blur-pausing-game-plugin.ts +26 -0
  40. package/src/plugins/time-keys-game-plugin.ts +24 -0
  41. package/src/ui/screen-delegate.ts +6 -0
  42. package/src/ui/screen.ts +167 -0
  43. package/src/util/math.ts +5 -0
  44. package/tsconfig.json +13 -0
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TimeKeysGamePlugin = void 0;
4
+ const client_inputs_1 = require("@remvst/client-inputs");
5
+ const game_plugin_1 = require("../game/game-plugin");
6
+ class TimeKeysGamePlugin extends game_plugin_1.GamePlugin {
7
+ constructor() {
8
+ super(...arguments);
9
+ this.key = TimeKeysGamePlugin.key;
10
+ }
11
+ get timeFactor() {
12
+ if (this.game.inputs.keyboard.isDown(client_inputs_1.Keyboard.F)) {
13
+ return 500;
14
+ }
15
+ else if (this.game.inputs.keyboard.isDown(client_inputs_1.Keyboard.G)) {
16
+ return 0.1;
17
+ }
18
+ return 1;
19
+ }
20
+ setup() {
21
+ }
22
+ render() {
23
+ }
24
+ }
25
+ exports.TimeKeysGamePlugin = TimeKeysGamePlugin;
26
+ TimeKeysGamePlugin.key = 'time-keys';
@@ -0,0 +1,6 @@
1
+ export interface ScreenDelegate {
2
+ onSetup(): void;
3
+ onForeground(): void;
4
+ onBackground(): void;
5
+ onDestroy(): void;
6
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,38 @@
1
+ import { InterpolationPool } from '@remvst/animate.js';
2
+ import { Game } from '../game/game';
3
+ import { ScreenDelegate } from './screen-delegate';
4
+ import { ScreenInputs } from '../interaction/inputs';
5
+ export declare abstract class Screen {
6
+ game: Game;
7
+ cursorType: string;
8
+ inputs: ScreenInputs;
9
+ age: number;
10
+ frameCount: number;
11
+ interpolationPool: InterpolationPool;
12
+ delegate: ScreenDelegate | null;
13
+ private readonly subscreens;
14
+ constructor();
15
+ addSubscreen(screen: Screen): void;
16
+ bind(game: Game): void;
17
+ abstract get id(): string;
18
+ setup(): void;
19
+ destroy(): void;
20
+ foreground(): void;
21
+ background(): void;
22
+ cycle(elapsed: number): void;
23
+ postCycle(elapsed: number): void;
24
+ keyDown(keyCode: number): void;
25
+ keyUp(keyCode: number): void;
26
+ protected skip(): void;
27
+ mouseDown(button: number, x: number, y: number): void;
28
+ mouseMove(x: number, y: number, movementX: number, movementY: number): void;
29
+ mouseUp(button: number, x: number, y: number): void;
30
+ wheel(deltaX: number, deltaY: number, deltaZ: number): void;
31
+ addDebugValues(values: {
32
+ [key: string]: any;
33
+ }): void;
34
+ get isForeground(): boolean;
35
+ get absorbInputs(): boolean;
36
+ get absorbCycle(): boolean;
37
+ get timeFactor(): number;
38
+ }
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Screen = void 0;
4
+ const animate_js_1 = require("@remvst/animate.js");
5
+ const inputs_1 = require("../interaction/inputs");
6
+ class Screen {
7
+ constructor() {
8
+ this.cursorType = 'default';
9
+ this.age = 0;
10
+ this.frameCount = 0;
11
+ this.delegate = null;
12
+ this.subscreens = [];
13
+ this.game = null;
14
+ }
15
+ addSubscreen(screen) {
16
+ this.subscreens.push(screen);
17
+ }
18
+ bind(game) {
19
+ this.game = game;
20
+ for (const plugin of this.game.plugins) {
21
+ plugin.setupScreen(this);
22
+ }
23
+ for (const sub of this.subscreens) {
24
+ sub.bind(game);
25
+ }
26
+ }
27
+ setup() {
28
+ this.inputs = new inputs_1.ScreenInputs(this);
29
+ this.inputs.keyboard.onKeyDown = (keyCode) => this.keyDown(keyCode);
30
+ this.inputs.keyboard.onKeyUp = (keyCode) => this.keyUp(keyCode);
31
+ this.inputs.mouse.onMouseDown = (button) => this.mouseDown(button, this.inputs.mouse.position.x, this.inputs.mouse.position.y);
32
+ this.inputs.mouse.onMouseUp = (button) => this.mouseUp(button, this.inputs.mouse.position.x, this.inputs.mouse.position.y);
33
+ this.inputs.mouse.onMouseMove = (movementX, movementY) => this.mouseMove(this.inputs.mouse.position.x, this.inputs.mouse.position.y, movementX, movementY);
34
+ this.inputs.mouse.onWheel = (x, y, z) => this.wheel(x, y, z);
35
+ this.inputs.setup();
36
+ this.interpolationPool = new animate_js_1.InterpolationPool();
37
+ this.delegate?.onSetup();
38
+ for (const sub of this.subscreens) {
39
+ sub.setup();
40
+ }
41
+ }
42
+ destroy() {
43
+ this.delegate?.onDestroy();
44
+ for (const sub of this.subscreens) {
45
+ sub.destroy();
46
+ }
47
+ }
48
+ foreground() {
49
+ this.delegate?.onForeground();
50
+ for (const sub of this.subscreens) {
51
+ sub.foreground();
52
+ }
53
+ }
54
+ background() {
55
+ const newScreen = this.game.screenStack.current();
56
+ if (newScreen.absorbInputs) {
57
+ this.inputs.reset();
58
+ }
59
+ this.delegate?.onBackground();
60
+ for (const sub of this.subscreens) {
61
+ sub.background();
62
+ }
63
+ }
64
+ cycle(elapsed) {
65
+ this.age += elapsed;
66
+ this.frameCount++;
67
+ for (const sub of this.subscreens) {
68
+ sub.inputs.import(this.inputs);
69
+ sub.cycle(elapsed);
70
+ }
71
+ }
72
+ postCycle(elapsed) {
73
+ if (this.interpolationPool) {
74
+ this.interpolationPool.cycle(elapsed);
75
+ }
76
+ for (const sub of this.subscreens) {
77
+ sub.postCycle(elapsed);
78
+ }
79
+ }
80
+ keyDown(keyCode) {
81
+ for (const subscreen of this.subscreens) {
82
+ subscreen.keyDown(keyCode);
83
+ }
84
+ }
85
+ keyUp(keyCode) {
86
+ for (const subscreen of this.subscreens) {
87
+ subscreen.keyUp(keyCode);
88
+ }
89
+ }
90
+ skip() {
91
+ this.interpolationPool.skip();
92
+ }
93
+ mouseDown(button, x, y) {
94
+ for (const subscreen of this.subscreens) {
95
+ subscreen.mouseDown(button, x, y);
96
+ }
97
+ }
98
+ mouseMove(x, y, movementX, movementY) {
99
+ for (const subscreen of this.subscreens) {
100
+ subscreen.mouseMove(x, y, movementX, movementY);
101
+ }
102
+ }
103
+ mouseUp(button, x, y) {
104
+ for (const subscreen of this.subscreens) {
105
+ subscreen.mouseUp(button, x, y);
106
+ }
107
+ }
108
+ wheel(deltaX, deltaY, deltaZ) {
109
+ for (const subscreen of this.subscreens) {
110
+ subscreen.wheel(deltaX, deltaY, deltaZ);
111
+ }
112
+ }
113
+ addDebugValues(values) {
114
+ values['screen.id'] = this.id;
115
+ values['screen.interpolations'] = this.interpolationPool.size;
116
+ }
117
+ get isForeground() {
118
+ return this.game && this.game.screenStack.current() === this;
119
+ }
120
+ get absorbInputs() {
121
+ return true;
122
+ }
123
+ get absorbCycle() {
124
+ return true;
125
+ }
126
+ get timeFactor() {
127
+ return 1;
128
+ }
129
+ }
130
+ exports.Screen = Screen;
@@ -0,0 +1 @@
1
+ export declare function between(a: number, b: number, c: number): number;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.between = void 0;
4
+ function between(a, b, c) {
5
+ if (b < a)
6
+ return a;
7
+ if (b > c)
8
+ return c;
9
+ return b;
10
+ }
11
+ exports.between = between;
12
+ ;
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@remvst/game-navigation-core",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "lib/index.js",
6
+ "scripts": {
7
+ "build": "rm -rf lib && tsc",
8
+ "prepublishOnly": "npm i && npm run build"
9
+ },
10
+ "author": "Rémi Vansteelandt",
11
+ "license": "UNLICENSED",
12
+ "dependencies": {
13
+ "@remvst/animate.js": "^2.0.0",
14
+ "@remvst/client-inputs": "^1.0.4",
15
+ "@remvst/client-performance": "^1.0.0",
16
+ "@remvst/optimization": "^1.0.1"
17
+ },
18
+ "devDependencies": {
19
+ "typescript": "^5.2.2"
20
+ }
21
+ }
@@ -0,0 +1,6 @@
1
+ export interface GameParams {
2
+ width: number;
3
+ height: number;
4
+ maxFrameInterval?: number;
5
+ fps?: number;
6
+ };
@@ -0,0 +1,23 @@
1
+ import { Game } from "./game";
2
+ import { Screen } from '../ui/screen';
3
+
4
+ export abstract class GamePlugin {
5
+
6
+ protected game: Game;
7
+
8
+ get timeFactor(): number {
9
+ return 1;
10
+ }
11
+
12
+ bind(game: Game) {
13
+ this.game = game;
14
+ }
15
+
16
+ setupScreen(screen: Screen): void {
17
+
18
+ }
19
+
20
+ abstract get key(): string;
21
+ abstract setup(): void;
22
+ abstract render(): void;
23
+ }
@@ -0,0 +1,153 @@
1
+ import { GameInputs } from '../interaction/inputs';
2
+ import { GameParams } from './game-params';
3
+ import { GamePlugin } from './game-plugin';
4
+ import { Screen } from '../ui/screen';
5
+ import { ScreenStack } from '../navigation/screen-stack';
6
+ import { PerformanceRecorder } from '@remvst/client-performance';
7
+
8
+ export abstract class Game<ParamsType extends GameParams = GameParams> {
9
+
10
+ private readonly pluginMap = new Map<string, GamePlugin>();
11
+
12
+ // Inputs
13
+ inputs: GameInputs;
14
+
15
+ // Screens
16
+ screenStack: ScreenStack;
17
+
18
+ // Time
19
+ age = 0;
20
+ private lastFrame: number;
21
+
22
+ // Cycle loop
23
+ private animationFrameId: number;
24
+ private runningLoop = false;
25
+
26
+ // Performance
27
+ readonly performanceRecorder = new PerformanceRecorder({
28
+ 'recordCount': Math.floor(window.innerWidth / 3),
29
+ });
30
+
31
+ constructor(readonly params: ParamsType) {
32
+ this.parseURL();
33
+ }
34
+
35
+ abstract get plugins(): GamePlugin[];
36
+
37
+ abstract get container(): HTMLElement;
38
+
39
+ plugin<T extends GamePlugin>(keyProvider: (new (...params: any) => T) & {key: string}): T {
40
+ return this.pluginMap.get(keyProvider.key) as T;
41
+ }
42
+
43
+ setup() {
44
+ for (const plugin of this.plugins) {
45
+ plugin.bind(this);
46
+ plugin.setup();
47
+ this.pluginMap.set(plugin.key, plugin);
48
+ }
49
+
50
+ this.inputs = new GameInputs(this);
51
+ this.inputs.setup();
52
+
53
+ this.screenStack = new ScreenStack(this);
54
+
55
+ this.lastFrame = window.performance.now();
56
+ this.scheduleFrame();
57
+ }
58
+
59
+ scheduleFrame() {
60
+ if (this.animationFrameId) {
61
+ return;
62
+ }
63
+
64
+ const callback = () => {
65
+ this.animationFrameId = null;
66
+ this.frame();
67
+ };
68
+
69
+ this.runningLoop = true;
70
+
71
+ if (!this.params.fps) {
72
+ this.animationFrameId = window.requestAnimationFrame(callback);
73
+ } else {
74
+ setTimeout(callback, 1000 / this.params.fps);
75
+ }
76
+ }
77
+
78
+ stopLoop() {
79
+ this.runningLoop = false;
80
+ window.cancelAnimationFrame(this.animationFrameId);
81
+ this.animationFrameId = null;
82
+ }
83
+
84
+ frame() {
85
+ this.performanceRecorder.roll();
86
+ this.performanceRecorder.onStart('FRAME');
87
+
88
+ const now = window.performance.now();
89
+ const elapsedMs = now - this.lastFrame;
90
+ const elapsed = Math.min(this.params.maxFrameInterval || 1, elapsedMs / 1000);
91
+
92
+ this.lastFrame = now;
93
+
94
+ this.cycle(elapsed);
95
+
96
+ this.performanceRecorder.onStart('RENDER');
97
+ for (const plugin of this.plugins) {
98
+ plugin.render();
99
+ }
100
+ this.performanceRecorder.onEnd('RENDER');
101
+
102
+ this.performanceRecorder.onEnd('FRAME');
103
+
104
+ if (this.runningLoop) {
105
+ this.scheduleFrame();
106
+ }
107
+ }
108
+
109
+ cycle(elapsed: number) {
110
+ // Safety in case we resume the game after a while
111
+ let adjusted = Math.min(elapsed, this.params.maxFrameInterval || 1);
112
+
113
+ for (const plugin of this.plugins) {
114
+ adjusted *= plugin.timeFactor;
115
+ }
116
+
117
+ this.age += elapsed;
118
+
119
+ const screens = this.screenStack.screens.slice();
120
+ for (let i = screens.length - 1; i >= 0; i--) {
121
+ const screen = screens[i];
122
+ screen.cycle(adjusted);
123
+ screen.postCycle(adjusted);
124
+
125
+ if (screen.absorbCycle) {
126
+ break;
127
+ }
128
+ }
129
+ }
130
+
131
+ getDebugValues(out: {[key: string]: any}) {
132
+ this.screenStack.current()?.addDebugValues(out);
133
+ }
134
+
135
+ private parseURL() {
136
+ const params = new URLSearchParams((document.location.search || '?').substr(1));
137
+ for (const [param, originalValue] of params.entries()) {
138
+ let value = originalValue as any;
139
+ if (value === 'true' || value === 'false') {
140
+ value = JSON.parse(value);
141
+ } else if (!Number.isNaN(value)) {
142
+ value = parseFloat(value) || value;
143
+ }
144
+
145
+ (this.params as any)[param] = value;
146
+ }
147
+ }
148
+
149
+ onCurrentScreenChanged() {
150
+ (window as any).S = this.screenStack.current();
151
+ this.container.style.cursor = this.screenStack.current()?.cursorType;
152
+ }
153
+ };
package/src/index.ts ADDED
@@ -0,0 +1,18 @@
1
+ export * from './game/game-params';
2
+ export * from './game/game-plugin';
3
+ export * from './game/game';
4
+
5
+ export * from './interaction/inputs';
6
+ export * from './interaction/screen-mouse-inputs';
7
+
8
+ export * from './navigation/resolver';
9
+ export * from './navigation/screen-stack';
10
+
11
+ export * from './ui/screen-delegate';
12
+ export * from './ui/screen';
13
+
14
+ export * from './plugins/automation-game-plugin';
15
+ export * from './plugins/blur-pausing-game-plugin';
16
+ export * from './plugins/time-keys-game-plugin';
17
+
18
+ export * from './util/math';
@@ -0,0 +1,118 @@
1
+ import { between } from '../util/math';
2
+ import { ScreenMouseInputs } from './screen-mouse-inputs';
3
+ import { DOMGamepadInputs, DOMKeyboardInputs, DOMMouseInputs, Inputs } from '@remvst/client-inputs';
4
+ import GamepadInputs from '@remvst/client-inputs/lib/gamepad/gamepad-inputs';
5
+ import KeyboardInputs from '@remvst/client-inputs/lib/keyboard/keyboard-inputs';
6
+ import { Screen } from '../ui/screen';
7
+ import { Game } from '../game/game';
8
+
9
+ export class ScreenInputs implements Inputs {
10
+ readonly keyboard = new KeyboardInputs();
11
+ readonly mouse = new ScreenMouseInputs(this.screen.game.params);
12
+ readonly gamepad = new GamepadInputs();
13
+
14
+ constructor(readonly screen: Screen) {
15
+
16
+ }
17
+
18
+ setup() {
19
+ this.keyboard.setup();
20
+ this.mouse.setup();
21
+ this.gamepad.setup();
22
+ }
23
+
24
+ reset() {
25
+ this.keyboard.reset();
26
+ this.mouse.reset();
27
+ this.gamepad.reset();
28
+ }
29
+
30
+ import(other: Inputs) {
31
+ this.keyboard.import(other.keyboard);
32
+ this.mouse.import(other.mouse);
33
+ this.gamepad.import(other.gamepad);
34
+ }
35
+ }
36
+
37
+ export class GameInputs implements Inputs {
38
+ readonly keyboard = new DOMKeyboardInputs();
39
+ readonly mouse = new DOMMouseInputs(this.game.container, this.game.params);
40
+ readonly gamepad = new DOMGamepadInputs();
41
+
42
+ constructor(private readonly game: Game) {
43
+ this.keyboard.onKeyDown = (keyCode: number) => {
44
+ this.makeGamepadInactive();
45
+ this.loopScreens((inputs) => inputs.keyboard.setDown(keyCode, true));
46
+ };
47
+ this.keyboard.onKeyUp = (keyCode: number) => {
48
+ this.makeGamepadInactive();
49
+ this.loopScreens((inputs) => inputs.keyboard.setDown(keyCode, false));
50
+ };
51
+
52
+ this.mouse.onMouseDown = (button) => {
53
+ this.makeGamepadInactive();
54
+ this.loopScreens((inputs) => inputs.mouse.setButtonDown(button, true));
55
+ };
56
+ this.mouse.onMouseUp = (button) => {
57
+ this.makeGamepadInactive();
58
+ this.loopScreens((inputs) => inputs.mouse.setButtonDown(button, false));
59
+ };
60
+ this.mouse.onWheel = (x, y, z) => {
61
+ this.makeGamepadInactive();
62
+ this.loopScreens((inputs) => inputs.mouse.onWheel(x, y, z));
63
+ };
64
+
65
+ this.mouse.onMouseMove = (movementX, movementY) => {
66
+ this.makeGamepadInactive();
67
+
68
+ const x = between(0, this.mouse.position.x, this.game.params.width);
69
+ const y = between(0, this.mouse.position.y, this.game.params.height);
70
+
71
+ this.loopScreens((inputs) => inputs.mouse.setMousePosition(x, y, movementX, movementY));
72
+ };
73
+
74
+ this.gamepad.onButtonDown = (button) => this.loopScreens((inputs) => inputs.gamepad.setDown(button, true));
75
+ this.gamepad.onButtonUp = (button) => this.loopScreens((inputs) => inputs.gamepad.setDown(button, false));
76
+ this.gamepad.onAxisUpdated = (axis, value) => this.loopScreens((inputs) => inputs.gamepad.setAxis(axis, value));
77
+ }
78
+
79
+ setup() {
80
+ this.keyboard.setup();
81
+ this.mouse.setup();
82
+ this.gamepad.setup();
83
+
84
+ window.addEventListener('blur', () => this.reset(), false);
85
+ }
86
+
87
+ reset() {
88
+ this.keyboard.reset();
89
+ this.mouse.reset();
90
+ this.gamepad.reset();
91
+
92
+ for (const screen of this.game.screenStack.screens) {
93
+ screen.inputs.reset();
94
+ }
95
+ }
96
+
97
+ private makeGamepadInactive() {
98
+ this.gamepad.isActive = false;
99
+ for (const screen of this.game.screenStack.screens) {
100
+ screen.inputs.gamepad.isActive = false;
101
+ }
102
+ }
103
+
104
+ private loopScreens(func: (inputs: Inputs) => void) {
105
+ for (let i = this.game.screenStack.screens.length - 1 ; i >= 0 ; i--) {
106
+ const screen = this.game.screenStack.screens[i];
107
+ func(screen.inputs);
108
+
109
+ if (screen.absorbInputs) {
110
+ break;
111
+ }
112
+ }
113
+ }
114
+
115
+ preventDefault() {
116
+ this.keyboard.preventDefault();
117
+ }
118
+ };
@@ -0,0 +1,22 @@
1
+ import MouseInputs from "@remvst/client-inputs/lib/mouse/mouse-inputs";
2
+ import { between } from "../util/math";
3
+
4
+ export class ScreenMouseInputs extends MouseInputs {
5
+
6
+ constructor(private readonly size: {width: number, height: number}) {
7
+ super();
8
+ }
9
+
10
+ setMousePosition(x: number, y: number, movementX: number, movementY: number): void {
11
+ if (document.pointerLockElement) {
12
+ super.setMousePosition(
13
+ between(0, this.position.x + movementX, this.size.width),
14
+ between(0, this.position.y + movementY, this.size.height),
15
+ movementX,
16
+ movementY,
17
+ );
18
+ } else {
19
+ super.setMousePosition(x, y, movementX, movementY);
20
+ }
21
+ }
22
+ }
@@ -0,0 +1,49 @@
1
+ import { Screen } from '../ui/screen';
2
+
3
+ export class UserCancelledError extends Error {
4
+
5
+ }
6
+
7
+ export class Resolver<T> {
8
+
9
+ private readonly screen: Screen;
10
+ readonly resolve: (value: T) => void;
11
+ readonly reject: (error: Error) => void;
12
+
13
+ dismissWhenResolved = true;
14
+ private resolved = false;
15
+
16
+ constructor(
17
+ screen: Screen,
18
+ resolve: (value: T) => void,
19
+ reject: (error: Error) => void
20
+ ) {
21
+ this.screen = screen;
22
+ this.resolve = (value) => {
23
+ if (this.resolved) return;
24
+ this.resolved = true;
25
+
26
+ if (this.dismissWhenResolved) {
27
+ this.dismiss();
28
+ }
29
+ resolve(value);
30
+ };
31
+ this.reject = (err) => {
32
+ if (this.resolved) return;
33
+ this.resolved = true;
34
+
35
+ if (this.dismissWhenResolved) {
36
+ this.dismiss();
37
+ }
38
+ reject(err);
39
+ };
40
+ }
41
+
42
+ private dismiss() {
43
+ this.screen.game.screenStack.pop();
44
+ }
45
+ }
46
+
47
+ export interface ResolverHolder<T> {
48
+ resolver: Resolver<T>;
49
+ }