@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,6 @@
1
+ export interface GameParams {
2
+ width: number;
3
+ height: number;
4
+ maxFrameInterval?: number;
5
+ fps?: number;
6
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ ;
@@ -0,0 +1,11 @@
1
+ import { Game } from "./game";
2
+ import { Screen } from '../ui/screen';
3
+ export declare abstract class GamePlugin {
4
+ protected game: Game;
5
+ get timeFactor(): number;
6
+ bind(game: Game): void;
7
+ setupScreen(screen: Screen): void;
8
+ abstract get key(): string;
9
+ abstract setup(): void;
10
+ abstract render(): void;
11
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GamePlugin = void 0;
4
+ class GamePlugin {
5
+ get timeFactor() {
6
+ return 1;
7
+ }
8
+ bind(game) {
9
+ this.game = game;
10
+ }
11
+ setupScreen(screen) {
12
+ }
13
+ }
14
+ exports.GamePlugin = GamePlugin;
@@ -0,0 +1,32 @@
1
+ import { GameInputs } from '../interaction/inputs';
2
+ import { GameParams } from './game-params';
3
+ import { GamePlugin } from './game-plugin';
4
+ import { ScreenStack } from '../navigation/screen-stack';
5
+ import { PerformanceRecorder } from '@remvst/client-performance';
6
+ export declare abstract class Game<ParamsType extends GameParams = GameParams> {
7
+ readonly params: ParamsType;
8
+ private readonly pluginMap;
9
+ inputs: GameInputs;
10
+ screenStack: ScreenStack;
11
+ age: number;
12
+ private lastFrame;
13
+ private animationFrameId;
14
+ private runningLoop;
15
+ readonly performanceRecorder: PerformanceRecorder;
16
+ constructor(params: ParamsType);
17
+ abstract get plugins(): GamePlugin[];
18
+ abstract get container(): HTMLElement;
19
+ plugin<T extends GamePlugin>(keyProvider: (new (...params: any) => T) & {
20
+ key: string;
21
+ }): T;
22
+ setup(): void;
23
+ scheduleFrame(): void;
24
+ stopLoop(): void;
25
+ frame(): void;
26
+ cycle(elapsed: number): void;
27
+ getDebugValues(out: {
28
+ [key: string]: any;
29
+ }): void;
30
+ private parseURL;
31
+ onCurrentScreenChanged(): void;
32
+ }
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Game = void 0;
4
+ const inputs_1 = require("../interaction/inputs");
5
+ const screen_stack_1 = require("../navigation/screen-stack");
6
+ const client_performance_1 = require("@remvst/client-performance");
7
+ class Game {
8
+ constructor(params) {
9
+ this.params = params;
10
+ this.pluginMap = new Map();
11
+ // Time
12
+ this.age = 0;
13
+ this.runningLoop = false;
14
+ // Performance
15
+ this.performanceRecorder = new client_performance_1.PerformanceRecorder({
16
+ 'recordCount': Math.floor(window.innerWidth / 3),
17
+ });
18
+ this.parseURL();
19
+ }
20
+ plugin(keyProvider) {
21
+ return this.pluginMap.get(keyProvider.key);
22
+ }
23
+ setup() {
24
+ for (const plugin of this.plugins) {
25
+ plugin.bind(this);
26
+ plugin.setup();
27
+ this.pluginMap.set(plugin.key, plugin);
28
+ }
29
+ this.inputs = new inputs_1.GameInputs(this);
30
+ this.inputs.setup();
31
+ this.screenStack = new screen_stack_1.ScreenStack(this);
32
+ this.lastFrame = window.performance.now();
33
+ this.scheduleFrame();
34
+ }
35
+ scheduleFrame() {
36
+ if (this.animationFrameId) {
37
+ return;
38
+ }
39
+ const callback = () => {
40
+ this.animationFrameId = null;
41
+ this.frame();
42
+ };
43
+ this.runningLoop = true;
44
+ if (!this.params.fps) {
45
+ this.animationFrameId = window.requestAnimationFrame(callback);
46
+ }
47
+ else {
48
+ setTimeout(callback, 1000 / this.params.fps);
49
+ }
50
+ }
51
+ stopLoop() {
52
+ this.runningLoop = false;
53
+ window.cancelAnimationFrame(this.animationFrameId);
54
+ this.animationFrameId = null;
55
+ }
56
+ frame() {
57
+ this.performanceRecorder.roll();
58
+ this.performanceRecorder.onStart('FRAME');
59
+ const now = window.performance.now();
60
+ const elapsedMs = now - this.lastFrame;
61
+ const elapsed = Math.min(this.params.maxFrameInterval || 1, elapsedMs / 1000);
62
+ this.lastFrame = now;
63
+ this.cycle(elapsed);
64
+ this.performanceRecorder.onStart('RENDER');
65
+ for (const plugin of this.plugins) {
66
+ plugin.render();
67
+ }
68
+ this.performanceRecorder.onEnd('RENDER');
69
+ this.performanceRecorder.onEnd('FRAME');
70
+ if (this.runningLoop) {
71
+ this.scheduleFrame();
72
+ }
73
+ }
74
+ cycle(elapsed) {
75
+ // Safety in case we resume the game after a while
76
+ let adjusted = Math.min(elapsed, this.params.maxFrameInterval || 1);
77
+ for (const plugin of this.plugins) {
78
+ adjusted *= plugin.timeFactor;
79
+ }
80
+ this.age += elapsed;
81
+ const screens = this.screenStack.screens.slice();
82
+ for (let i = screens.length - 1; i >= 0; i--) {
83
+ const screen = screens[i];
84
+ screen.cycle(adjusted);
85
+ screen.postCycle(adjusted);
86
+ if (screen.absorbCycle) {
87
+ break;
88
+ }
89
+ }
90
+ }
91
+ getDebugValues(out) {
92
+ this.screenStack.current()?.addDebugValues(out);
93
+ }
94
+ parseURL() {
95
+ const params = new URLSearchParams((document.location.search || '?').substr(1));
96
+ for (const [param, originalValue] of params.entries()) {
97
+ let value = originalValue;
98
+ if (value === 'true' || value === 'false') {
99
+ value = JSON.parse(value);
100
+ }
101
+ else if (!Number.isNaN(value)) {
102
+ value = parseFloat(value) || value;
103
+ }
104
+ this.params[param] = value;
105
+ }
106
+ }
107
+ onCurrentScreenChanged() {
108
+ window.S = this.screenStack.current();
109
+ this.container.style.cursor = this.screenStack.current()?.cursorType;
110
+ }
111
+ }
112
+ exports.Game = Game;
113
+ ;
package/lib/index.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ export * from './game/game-params';
2
+ export * from './game/game-plugin';
3
+ export * from './game/game';
4
+ export * from './interaction/inputs';
5
+ export * from './interaction/screen-mouse-inputs';
6
+ export * from './navigation/resolver';
7
+ export * from './navigation/screen-stack';
8
+ export * from './ui/screen-delegate';
9
+ export * from './ui/screen';
10
+ export * from './plugins/automation-game-plugin';
11
+ export * from './plugins/blur-pausing-game-plugin';
12
+ export * from './plugins/time-keys-game-plugin';
13
+ export * from './util/math';
package/lib/index.js ADDED
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./game/game-params"), exports);
18
+ __exportStar(require("./game/game-plugin"), exports);
19
+ __exportStar(require("./game/game"), exports);
20
+ __exportStar(require("./interaction/inputs"), exports);
21
+ __exportStar(require("./interaction/screen-mouse-inputs"), exports);
22
+ __exportStar(require("./navigation/resolver"), exports);
23
+ __exportStar(require("./navigation/screen-stack"), exports);
24
+ __exportStar(require("./ui/screen-delegate"), exports);
25
+ __exportStar(require("./ui/screen"), exports);
26
+ __exportStar(require("./plugins/automation-game-plugin"), exports);
27
+ __exportStar(require("./plugins/blur-pausing-game-plugin"), exports);
28
+ __exportStar(require("./plugins/time-keys-game-plugin"), exports);
29
+ __exportStar(require("./util/math"), exports);
@@ -0,0 +1,28 @@
1
+ import { ScreenMouseInputs } from './screen-mouse-inputs';
2
+ import { DOMGamepadInputs, DOMKeyboardInputs, DOMMouseInputs, Inputs } from '@remvst/client-inputs';
3
+ import GamepadInputs from '@remvst/client-inputs/lib/gamepad/gamepad-inputs';
4
+ import KeyboardInputs from '@remvst/client-inputs/lib/keyboard/keyboard-inputs';
5
+ import { Screen } from '../ui/screen';
6
+ import { Game } from '../game/game';
7
+ export declare class ScreenInputs implements Inputs {
8
+ readonly screen: Screen;
9
+ readonly keyboard: KeyboardInputs;
10
+ readonly mouse: ScreenMouseInputs;
11
+ readonly gamepad: GamepadInputs;
12
+ constructor(screen: Screen);
13
+ setup(): void;
14
+ reset(): void;
15
+ import(other: Inputs): void;
16
+ }
17
+ export declare class GameInputs implements Inputs {
18
+ private readonly game;
19
+ readonly keyboard: DOMKeyboardInputs;
20
+ readonly mouse: DOMMouseInputs;
21
+ readonly gamepad: DOMGamepadInputs;
22
+ constructor(game: Game);
23
+ setup(): void;
24
+ reset(): void;
25
+ private makeGamepadInactive;
26
+ private loopScreens;
27
+ preventDefault(): void;
28
+ }
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GameInputs = exports.ScreenInputs = void 0;
4
+ const math_1 = require("../util/math");
5
+ const screen_mouse_inputs_1 = require("./screen-mouse-inputs");
6
+ const client_inputs_1 = require("@remvst/client-inputs");
7
+ const gamepad_inputs_1 = require("@remvst/client-inputs/lib/gamepad/gamepad-inputs");
8
+ const keyboard_inputs_1 = require("@remvst/client-inputs/lib/keyboard/keyboard-inputs");
9
+ class ScreenInputs {
10
+ constructor(screen) {
11
+ this.screen = screen;
12
+ this.keyboard = new keyboard_inputs_1.default();
13
+ this.mouse = new screen_mouse_inputs_1.ScreenMouseInputs(this.screen.game.params);
14
+ this.gamepad = new gamepad_inputs_1.default();
15
+ }
16
+ setup() {
17
+ this.keyboard.setup();
18
+ this.mouse.setup();
19
+ this.gamepad.setup();
20
+ }
21
+ reset() {
22
+ this.keyboard.reset();
23
+ this.mouse.reset();
24
+ this.gamepad.reset();
25
+ }
26
+ import(other) {
27
+ this.keyboard.import(other.keyboard);
28
+ this.mouse.import(other.mouse);
29
+ this.gamepad.import(other.gamepad);
30
+ }
31
+ }
32
+ exports.ScreenInputs = ScreenInputs;
33
+ class GameInputs {
34
+ constructor(game) {
35
+ this.game = game;
36
+ this.keyboard = new client_inputs_1.DOMKeyboardInputs();
37
+ this.mouse = new client_inputs_1.DOMMouseInputs(this.game.container, this.game.params);
38
+ this.gamepad = new client_inputs_1.DOMGamepadInputs();
39
+ this.keyboard.onKeyDown = (keyCode) => {
40
+ this.makeGamepadInactive();
41
+ this.loopScreens((inputs) => inputs.keyboard.setDown(keyCode, true));
42
+ };
43
+ this.keyboard.onKeyUp = (keyCode) => {
44
+ this.makeGamepadInactive();
45
+ this.loopScreens((inputs) => inputs.keyboard.setDown(keyCode, false));
46
+ };
47
+ this.mouse.onMouseDown = (button) => {
48
+ this.makeGamepadInactive();
49
+ this.loopScreens((inputs) => inputs.mouse.setButtonDown(button, true));
50
+ };
51
+ this.mouse.onMouseUp = (button) => {
52
+ this.makeGamepadInactive();
53
+ this.loopScreens((inputs) => inputs.mouse.setButtonDown(button, false));
54
+ };
55
+ this.mouse.onWheel = (x, y, z) => {
56
+ this.makeGamepadInactive();
57
+ this.loopScreens((inputs) => inputs.mouse.onWheel(x, y, z));
58
+ };
59
+ this.mouse.onMouseMove = (movementX, movementY) => {
60
+ this.makeGamepadInactive();
61
+ const x = (0, math_1.between)(0, this.mouse.position.x, this.game.params.width);
62
+ const y = (0, math_1.between)(0, this.mouse.position.y, this.game.params.height);
63
+ this.loopScreens((inputs) => inputs.mouse.setMousePosition(x, y, movementX, movementY));
64
+ };
65
+ this.gamepad.onButtonDown = (button) => this.loopScreens((inputs) => inputs.gamepad.setDown(button, true));
66
+ this.gamepad.onButtonUp = (button) => this.loopScreens((inputs) => inputs.gamepad.setDown(button, false));
67
+ this.gamepad.onAxisUpdated = (axis, value) => this.loopScreens((inputs) => inputs.gamepad.setAxis(axis, value));
68
+ }
69
+ setup() {
70
+ this.keyboard.setup();
71
+ this.mouse.setup();
72
+ this.gamepad.setup();
73
+ window.addEventListener('blur', () => this.reset(), false);
74
+ }
75
+ reset() {
76
+ this.keyboard.reset();
77
+ this.mouse.reset();
78
+ this.gamepad.reset();
79
+ for (const screen of this.game.screenStack.screens) {
80
+ screen.inputs.reset();
81
+ }
82
+ }
83
+ makeGamepadInactive() {
84
+ this.gamepad.isActive = false;
85
+ for (const screen of this.game.screenStack.screens) {
86
+ screen.inputs.gamepad.isActive = false;
87
+ }
88
+ }
89
+ loopScreens(func) {
90
+ for (let i = this.game.screenStack.screens.length - 1; i >= 0; i--) {
91
+ const screen = this.game.screenStack.screens[i];
92
+ func(screen.inputs);
93
+ if (screen.absorbInputs) {
94
+ break;
95
+ }
96
+ }
97
+ }
98
+ preventDefault() {
99
+ this.keyboard.preventDefault();
100
+ }
101
+ }
102
+ exports.GameInputs = GameInputs;
103
+ ;
@@ -0,0 +1,9 @@
1
+ import MouseInputs from "@remvst/client-inputs/lib/mouse/mouse-inputs";
2
+ export declare class ScreenMouseInputs extends MouseInputs {
3
+ private readonly size;
4
+ constructor(size: {
5
+ width: number;
6
+ height: number;
7
+ });
8
+ setMousePosition(x: number, y: number, movementX: number, movementY: number): void;
9
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ScreenMouseInputs = void 0;
4
+ const mouse_inputs_1 = require("@remvst/client-inputs/lib/mouse/mouse-inputs");
5
+ const math_1 = require("../util/math");
6
+ class ScreenMouseInputs extends mouse_inputs_1.default {
7
+ constructor(size) {
8
+ super();
9
+ this.size = size;
10
+ }
11
+ setMousePosition(x, y, movementX, movementY) {
12
+ if (document.pointerLockElement) {
13
+ super.setMousePosition((0, math_1.between)(0, this.position.x + movementX, this.size.width), (0, math_1.between)(0, this.position.y + movementY, this.size.height), movementX, movementY);
14
+ }
15
+ else {
16
+ super.setMousePosition(x, y, movementX, movementY);
17
+ }
18
+ }
19
+ }
20
+ exports.ScreenMouseInputs = ScreenMouseInputs;
@@ -0,0 +1,15 @@
1
+ import { Screen } from '../ui/screen';
2
+ export declare class UserCancelledError extends Error {
3
+ }
4
+ export declare class Resolver<T> {
5
+ private readonly screen;
6
+ readonly resolve: (value: T) => void;
7
+ readonly reject: (error: Error) => void;
8
+ dismissWhenResolved: boolean;
9
+ private resolved;
10
+ constructor(screen: Screen, resolve: (value: T) => void, reject: (error: Error) => void);
11
+ private dismiss;
12
+ }
13
+ export interface ResolverHolder<T> {
14
+ resolver: Resolver<T>;
15
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Resolver = exports.UserCancelledError = void 0;
4
+ class UserCancelledError extends Error {
5
+ }
6
+ exports.UserCancelledError = UserCancelledError;
7
+ class Resolver {
8
+ constructor(screen, resolve, reject) {
9
+ this.dismissWhenResolved = true;
10
+ this.resolved = false;
11
+ this.screen = screen;
12
+ this.resolve = (value) => {
13
+ if (this.resolved)
14
+ return;
15
+ this.resolved = true;
16
+ if (this.dismissWhenResolved) {
17
+ this.dismiss();
18
+ }
19
+ resolve(value);
20
+ };
21
+ this.reject = (err) => {
22
+ if (this.resolved)
23
+ return;
24
+ this.resolved = true;
25
+ if (this.dismissWhenResolved) {
26
+ this.dismiss();
27
+ }
28
+ reject(err);
29
+ };
30
+ }
31
+ dismiss() {
32
+ this.screen.game.screenStack.pop();
33
+ }
34
+ }
35
+ exports.Resolver = Resolver;
@@ -0,0 +1,22 @@
1
+ import { ResolverHolder } from './resolver';
2
+ import { Game } from '../game/game';
3
+ import { Screen } from '../ui/screen';
4
+ export declare enum NavigationType {
5
+ RESET = 0,
6
+ PUSH = 1,
7
+ REPLACE_EXISTING = 2
8
+ }
9
+ export declare class ScreenStack {
10
+ private readonly game;
11
+ readonly screens: Screen[];
12
+ private readonly queue;
13
+ prepareScreen: (screen: Screen) => void;
14
+ constructor(game: Game);
15
+ findByType<T extends Screen>(screenType: (new (...params: any) => T)): T | null;
16
+ current(): Screen;
17
+ popTo(screen: Screen, inclusive?: boolean): void;
18
+ push(screen: Screen): void;
19
+ pop(): void;
20
+ reset(screen: Screen): void;
21
+ resolvableScreen<T>(screen: Screen & ResolverHolder<T>, navigationType?: NavigationType, dismissWhenResolved?: boolean): Promise<T>;
22
+ }
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ScreenStack = exports.NavigationType = void 0;
4
+ const optimization_1 = require("@remvst/optimization");
5
+ const resolver_1 = require("./resolver");
6
+ var NavigationType;
7
+ (function (NavigationType) {
8
+ NavigationType[NavigationType["RESET"] = 0] = "RESET";
9
+ NavigationType[NavigationType["PUSH"] = 1] = "PUSH";
10
+ NavigationType[NavigationType["REPLACE_EXISTING"] = 2] = "REPLACE_EXISTING";
11
+ })(NavigationType || (exports.NavigationType = NavigationType = {}));
12
+ class ScreenStack {
13
+ constructor(game) {
14
+ this.game = game;
15
+ this.screens = [];
16
+ this.queue = new optimization_1.OperationQueue();
17
+ this.prepareScreen = () => { };
18
+ }
19
+ findByType(screenType) {
20
+ return this.screens.find((scr) => scr instanceof screenType) || null;
21
+ }
22
+ current() {
23
+ return this.screens[this.screens.length - 1] || null;
24
+ }
25
+ popTo(screen, inclusive = false) {
26
+ while (this.current() && this.current() !== screen) {
27
+ this.pop();
28
+ }
29
+ if (inclusive && this.current() === screen) {
30
+ this.pop();
31
+ }
32
+ }
33
+ push(screen) {
34
+ this.queue.execute(() => {
35
+ const previousScreen = this.current();
36
+ screen.bind(this.game);
37
+ screen.setup();
38
+ this.screens.push(screen);
39
+ if (previousScreen) {
40
+ previousScreen.background();
41
+ }
42
+ screen.foreground();
43
+ screen.cycle(0);
44
+ screen.postCycle(0);
45
+ this.game.onCurrentScreenChanged();
46
+ });
47
+ }
48
+ pop() {
49
+ this.queue.execute(() => {
50
+ const screen = this.screens.pop();
51
+ if (screen) {
52
+ screen.destroy();
53
+ }
54
+ const newScreen = this.screens[this.screens.length - 1];
55
+ if (newScreen) {
56
+ newScreen.foreground();
57
+ this.game.onCurrentScreenChanged();
58
+ }
59
+ });
60
+ }
61
+ reset(screen) {
62
+ this.queue.execute(() => {
63
+ while (this.screens.length > 0) {
64
+ // Not calling pop() in case the child class needs to do something in it
65
+ const popped = this.screens.pop();
66
+ popped.destroy();
67
+ }
68
+ this.push(screen);
69
+ });
70
+ }
71
+ resolvableScreen(screen, navigationType = NavigationType.PUSH, dismissWhenResolved = true) {
72
+ return new Promise((resolve, reject) => {
73
+ screen.resolver = new resolver_1.Resolver(screen, resolve, reject);
74
+ screen.resolver.dismissWhenResolved = dismissWhenResolved;
75
+ switch (navigationType) {
76
+ case NavigationType.PUSH:
77
+ this.push(screen);
78
+ break;
79
+ case NavigationType.RESET:
80
+ this.reset(screen);
81
+ break;
82
+ case NavigationType.REPLACE_EXISTING:
83
+ const existing = this.findByType(screen.constructor);
84
+ if (existing)
85
+ this.popTo(existing, true);
86
+ this.push(screen);
87
+ break;
88
+ }
89
+ });
90
+ }
91
+ }
92
+ exports.ScreenStack = ScreenStack;
93
+ ;
@@ -0,0 +1,14 @@
1
+ import { GamePlugin } from "../game/game-plugin";
2
+ export declare class AutomationGamePlugin extends GamePlugin {
3
+ private readonly automationTimeFactor;
4
+ static readonly key = "automation";
5
+ readonly key = "automation";
6
+ protected automationLabel: HTMLDivElement;
7
+ get timeFactor(): number;
8
+ constructor(automationTimeFactor?: number);
9
+ setup(): void;
10
+ render(): void;
11
+ getAutomationValues(out: {
12
+ [key: string]: any;
13
+ }): void;
14
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AutomationGamePlugin = void 0;
4
+ const game_plugin_1 = require("../game/game-plugin");
5
+ class AutomationGamePlugin extends game_plugin_1.GamePlugin {
6
+ get timeFactor() {
7
+ return this.automationTimeFactor;
8
+ }
9
+ constructor(automationTimeFactor = 5) {
10
+ super();
11
+ this.automationTimeFactor = automationTimeFactor;
12
+ this.key = AutomationGamePlugin.key;
13
+ }
14
+ setup() {
15
+ this.automationLabel = document.createElement('div');
16
+ this.automationLabel.id = 'automation-label';
17
+ document.body.appendChild(this.automationLabel);
18
+ }
19
+ render() {
20
+ const automation = {};
21
+ this.getAutomationValues(automation);
22
+ this.automationLabel.innerText = JSON.stringify(automation);
23
+ // Disable all CSS animations for automation
24
+ const styleElement = document.createElement('style');
25
+ styleElement.setAttribute('id', 'style-tag');
26
+ styleElement.innerHTML = '*,:after,:before,.animate-in{transition:none!important;animation:none!important;}';
27
+ document.head.appendChild(styleElement);
28
+ }
29
+ getAutomationValues(out) {
30
+ this.game.getDebugValues(out);
31
+ }
32
+ }
33
+ exports.AutomationGamePlugin = AutomationGamePlugin;
34
+ AutomationGamePlugin.key = 'automation';
@@ -0,0 +1,9 @@
1
+ import { GamePlugin } from "../game/game-plugin";
2
+ export declare class BlurPausingGamePlugin extends GamePlugin {
3
+ static readonly key = "blur-pausing-game";
4
+ readonly key = "blur-pausing-game";
5
+ hasFocus: boolean;
6
+ setup(): void;
7
+ render(): void;
8
+ windowHasFocus(hasFocus: boolean): void;
9
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BlurPausingGamePlugin = void 0;
4
+ const game_plugin_1 = require("../game/game-plugin");
5
+ class BlurPausingGamePlugin extends game_plugin_1.GamePlugin {
6
+ constructor() {
7
+ super(...arguments);
8
+ this.key = BlurPausingGamePlugin.key;
9
+ this.hasFocus = true;
10
+ }
11
+ setup() {
12
+ window.addEventListener('blur', () => this.windowHasFocus(false), false);
13
+ window.addEventListener('focus', () => this.windowHasFocus(true), false);
14
+ }
15
+ render() {
16
+ }
17
+ windowHasFocus(hasFocus) {
18
+ this.hasFocus = hasFocus;
19
+ if (hasFocus) {
20
+ this.game.scheduleFrame();
21
+ }
22
+ else {
23
+ this.game.stopLoop();
24
+ }
25
+ }
26
+ }
27
+ exports.BlurPausingGamePlugin = BlurPausingGamePlugin;
28
+ BlurPausingGamePlugin.key = 'blur-pausing-game';
@@ -0,0 +1,8 @@
1
+ import { GamePlugin } from "../game/game-plugin";
2
+ export declare class TimeKeysGamePlugin extends GamePlugin {
3
+ static readonly key = "time-keys";
4
+ readonly key = "time-keys";
5
+ get timeFactor(): number;
6
+ setup(): void;
7
+ render(): void;
8
+ }