@remvst/game-navigation-core 1.9.0 → 1.10.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.
@@ -7,6 +7,7 @@ export declare abstract class GamePlugin {
7
7
  bind(game: Game): void;
8
8
  setup(): void;
9
9
  setupScreen(screen: Screen): void;
10
+ onCurrentScreenChanged(screen: Screen | null): void;
10
11
  cycle(elapsed: number): void;
11
12
  onCycleStart(): void;
12
13
  onCycleEnd(): void;
@@ -10,6 +10,7 @@ class GamePlugin {
10
10
  }
11
11
  setup() { }
12
12
  setupScreen(screen) { }
13
+ onCurrentScreenChanged(screen) { }
13
14
  cycle(elapsed) { }
14
15
  onCycleStart() { }
15
16
  onCycleEnd() { }
package/lib/game/game.js CHANGED
@@ -24,7 +24,13 @@ class Game {
24
24
  this.pluginMap.set(plugin.key, plugin);
25
25
  }
26
26
  for (const plugin of this.plugins) {
27
- plugin.setup();
27
+ try {
28
+ plugin.setup();
29
+ }
30
+ catch (err) {
31
+ console.error(`Failed to setup ${plugin.key} plugin: ${err.message}`, err);
32
+ throw err;
33
+ }
28
34
  }
29
35
  this.lastFrame = window.performance.now();
30
36
  this.scheduleFrame();
@@ -115,7 +121,11 @@ class Game {
115
121
  }
116
122
  }
117
123
  onCurrentScreenChanged() {
118
- this.container.style.cursor = this.screenStack.current()?.cursorType;
124
+ const screen = this.screenStack.current();
125
+ this.container.style.cursor = screen?.cursorType;
126
+ for (const plugin of this.plugins) {
127
+ plugin.onCurrentScreenChanged(screen);
128
+ }
119
129
  }
120
130
  }
121
131
  exports.Game = Game;
package/lib/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export * from "./game/game-params";
3
3
  export * from "./game/game-plugin";
4
4
  export * from "./interaction/inputs";
5
5
  export * from "./interaction/screen-mouse-inputs";
6
+ export * from "./navigation/back-navigation-listener";
6
7
  export * from "./navigation/resolver";
7
8
  export * from "./navigation/screen-stack";
8
9
  export * from "./ui/screen";
@@ -11,6 +12,7 @@ export * from "./plugins/automation-game-plugin";
11
12
  export * from "./plugins/blur-pausing-game-plugin";
12
13
  export * from "./plugins/fullscreen-button-game-plugin";
13
14
  export * from "./plugins/metrics-game-plugin";
15
+ export * from "./plugins/back-navigation-plugin";
14
16
  export * from "./plugins/storage-game-plugin";
15
17
  export * from "./plugins/time-keys-game-plugin";
16
18
  export * from "./plugins/version-label-game-plugin";
package/lib/index.js CHANGED
@@ -19,6 +19,7 @@ __exportStar(require("./game/game-params"), exports);
19
19
  __exportStar(require("./game/game-plugin"), exports);
20
20
  __exportStar(require("./interaction/inputs"), exports);
21
21
  __exportStar(require("./interaction/screen-mouse-inputs"), exports);
22
+ __exportStar(require("./navigation/back-navigation-listener"), exports);
22
23
  __exportStar(require("./navigation/resolver"), exports);
23
24
  __exportStar(require("./navigation/screen-stack"), exports);
24
25
  __exportStar(require("./ui/screen"), exports);
@@ -27,6 +28,7 @@ __exportStar(require("./plugins/automation-game-plugin"), exports);
27
28
  __exportStar(require("./plugins/blur-pausing-game-plugin"), exports);
28
29
  __exportStar(require("./plugins/fullscreen-button-game-plugin"), exports);
29
30
  __exportStar(require("./plugins/metrics-game-plugin"), exports);
31
+ __exportStar(require("./plugins/back-navigation-plugin"), exports);
30
32
  __exportStar(require("./plugins/storage-game-plugin"), exports);
31
33
  __exportStar(require("./plugins/time-keys-game-plugin"), exports);
32
34
  __exportStar(require("./plugins/version-label-game-plugin"), exports);
@@ -0,0 +1,3 @@
1
+ export interface BackNavigationListener {
2
+ onBackNavigation(): void;
3
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -11,6 +11,7 @@ export declare class ScreenStack {
11
11
  readonly screens: Screen[];
12
12
  private readonly queue;
13
13
  prepareScreen: (screen: Screen) => void;
14
+ onStackChanged: () => void;
14
15
  constructor(game: Game);
15
16
  findByType<T extends Screen>(screenType: new (...params: any) => T): T | null;
16
17
  current(): Screen;
@@ -15,6 +15,7 @@ class ScreenStack {
15
15
  this.screens = [];
16
16
  this.queue = new optimization_1.OperationQueue();
17
17
  this.prepareScreen = () => { };
18
+ this.onStackChanged = () => { };
18
19
  }
19
20
  findByType(screenType) {
20
21
  return (this.screens.find((scr) => scr instanceof screenType) || null);
@@ -29,6 +30,7 @@ class ScreenStack {
29
30
  if (inclusive && this.current() === screen) {
30
31
  this.pop();
31
32
  }
33
+ this.onStackChanged();
32
34
  }
33
35
  push(screen) {
34
36
  this.queue.execute(() => {
@@ -0,0 +1,12 @@
1
+ import { GamePlugin } from "../game/game-plugin";
2
+ import { Screen } from "../ui/screen";
3
+ export declare class BackNavigationPlugin extends GamePlugin {
4
+ readonly key = "back-navigation";
5
+ private pushedState;
6
+ setup(): void;
7
+ pushState(): void;
8
+ popState(): void;
9
+ private onPopState;
10
+ setupScreen(screen: Screen): void;
11
+ onCurrentScreenChanged(screen: Screen | null): void;
12
+ }
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BackNavigationPlugin = void 0;
4
+ const game_plugin_1 = require("../game/game-plugin");
5
+ class BackNavigationPlugin extends game_plugin_1.GamePlugin {
6
+ constructor() {
7
+ super(...arguments);
8
+ this.key = 'back-navigation';
9
+ this.pushedState = false;
10
+ }
11
+ setup() {
12
+ super.setup();
13
+ const { length } = history;
14
+ if (length > 0)
15
+ history.go(-length);
16
+ window.addEventListener('popstate', () => this.onPopState());
17
+ }
18
+ pushState() {
19
+ if (this.pushedState)
20
+ return;
21
+ this.pushedState = true;
22
+ window.history.pushState({}, '', '');
23
+ }
24
+ popState() {
25
+ if (!this.pushedState)
26
+ return;
27
+ this.pushedState = false;
28
+ window.history.back();
29
+ }
30
+ onPopState() {
31
+ this.pushedState = false;
32
+ const currentScreen = this.game.screenStack.current();
33
+ if (!currentScreen?.onBackNavigation)
34
+ return;
35
+ this.pushState();
36
+ currentScreen.onBackNavigation();
37
+ }
38
+ setupScreen(screen) {
39
+ super.setupScreen(screen);
40
+ }
41
+ onCurrentScreenChanged(screen) {
42
+ const handlesBackNavigation = screen?.onBackNavigation;
43
+ if (handlesBackNavigation) {
44
+ this.pushState();
45
+ }
46
+ else {
47
+ this.popState();
48
+ }
49
+ }
50
+ }
51
+ exports.BackNavigationPlugin = BackNavigationPlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remvst/game-navigation-core",
3
- "version": "1.9.0",
3
+ "version": "1.10.0",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -17,6 +17,7 @@ export abstract class GamePlugin {
17
17
  setup(): void {}
18
18
 
19
19
  setupScreen(screen: Screen): void {}
20
+ onCurrentScreenChanged(screen: Screen | null): void {}
20
21
 
21
22
  cycle(elapsed: number) {}
22
23
 
package/src/game/game.ts CHANGED
@@ -46,7 +46,15 @@ export abstract class Game<ParamsType extends GameParams = GameParams> {
46
46
  }
47
47
 
48
48
  for (const plugin of this.plugins) {
49
- plugin.setup();
49
+ try {
50
+ plugin.setup();
51
+ } catch (err) {
52
+ console.error(
53
+ `Failed to setup ${plugin.key} plugin: ${err.message}`,
54
+ err,
55
+ );
56
+ throw err;
57
+ }
50
58
  }
51
59
 
52
60
  this.lastFrame = window.performance.now();
@@ -157,6 +165,11 @@ export abstract class Game<ParamsType extends GameParams = GameParams> {
157
165
  }
158
166
 
159
167
  onCurrentScreenChanged() {
160
- this.container.style.cursor = this.screenStack.current()?.cursorType;
168
+ const screen = this.screenStack.current();
169
+ this.container.style.cursor = screen?.cursorType;
170
+
171
+ for (const plugin of this.plugins) {
172
+ plugin.onCurrentScreenChanged(screen);
173
+ }
161
174
  }
162
175
  }
package/src/index.ts CHANGED
@@ -5,6 +5,7 @@ export * from "./game/game-plugin";
5
5
  export * from "./interaction/inputs";
6
6
  export * from "./interaction/screen-mouse-inputs";
7
7
 
8
+ export * from "./navigation/back-navigation-listener";
8
9
  export * from "./navigation/resolver";
9
10
  export * from "./navigation/screen-stack";
10
11
 
@@ -15,6 +16,7 @@ export * from "./plugins/automation-game-plugin";
15
16
  export * from "./plugins/blur-pausing-game-plugin";
16
17
  export * from "./plugins/fullscreen-button-game-plugin";
17
18
  export * from "./plugins/metrics-game-plugin";
19
+ export * from "./plugins/back-navigation-plugin";
18
20
  export * from "./plugins/storage-game-plugin";
19
21
  export * from "./plugins/time-keys-game-plugin";
20
22
  export * from "./plugins/version-label-game-plugin";
@@ -0,0 +1,3 @@
1
+ export interface BackNavigationListener {
2
+ onBackNavigation(): void;
3
+ }
@@ -14,6 +14,8 @@ export class ScreenStack {
14
14
  private readonly queue = new OperationQueue();
15
15
  prepareScreen: (screen: Screen) => void = () => {};
16
16
 
17
+ onStackChanged: () => void = () => {};
18
+
17
19
  constructor(private readonly game: Game) {}
18
20
 
19
21
  findByType<T extends Screen>(
@@ -36,6 +38,8 @@ export class ScreenStack {
36
38
  if (inclusive && this.current() === screen) {
37
39
  this.pop();
38
40
  }
41
+
42
+ this.onStackChanged();
39
43
  }
40
44
 
41
45
  push(screen: Screen) {
@@ -0,0 +1,53 @@
1
+ import { GamePlugin } from "../game/game-plugin";
2
+ import { BackNavigationListener } from "../navigation/back-navigation-listener";
3
+ import { Screen } from "../ui/screen";
4
+
5
+ export class BackNavigationPlugin extends GamePlugin {
6
+ readonly key = 'back-navigation';
7
+
8
+ private pushedState = false;
9
+
10
+ setup(): void {
11
+ super.setup();
12
+
13
+ const { length} = history;
14
+ if (length > 0) history.go(-length);
15
+
16
+ window.addEventListener('popstate', () => this.onPopState());
17
+ }
18
+
19
+ pushState() {
20
+ if (this.pushedState) return;
21
+ this.pushedState = true;
22
+ window.history.pushState({}, '', '');
23
+ }
24
+
25
+ popState() {
26
+ if (!this.pushedState) return;
27
+ this.pushedState = false;
28
+ window.history.back();
29
+ }
30
+
31
+ private onPopState() {
32
+ this.pushedState = false;
33
+
34
+ const currentScreen = this.game.screenStack.current() as unknown as BackNavigationListener | null;
35
+ if (!currentScreen?.onBackNavigation) return;
36
+
37
+ this.pushState();
38
+ currentScreen.onBackNavigation();
39
+ }
40
+
41
+ setupScreen(screen: Screen) {
42
+ super.setupScreen(screen);
43
+ }
44
+
45
+ onCurrentScreenChanged(screen: Screen | null): void {
46
+ const handlesBackNavigation = (screen as unknown as BackNavigationListener)?.onBackNavigation;
47
+ if (handlesBackNavigation) {
48
+ this.pushState();
49
+ } else {
50
+ this.popState();
51
+ }
52
+ }
53
+ }