@remvst/game-navigation-core 1.5.0 → 1.6.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 (64) hide show
  1. package/lib/game/game-params.js +0 -1
  2. package/lib/game/game-plugin.d.ts +1 -1
  3. package/lib/game/game-plugin.js +4 -8
  4. package/lib/game/game.d.ts +5 -5
  5. package/lib/game/game.js +8 -9
  6. package/lib/index.d.ts +25 -19
  7. package/lib/index.js +9 -3
  8. package/lib/interaction/inputs.d.ts +6 -6
  9. package/lib/interaction/inputs.js +3 -4
  10. package/lib/navigation/resolver.d.ts +1 -1
  11. package/lib/navigation/screen-stack.d.ts +4 -4
  12. package/lib/navigation/screen-stack.js +1 -2
  13. package/lib/plugins/automation-game-plugin.js +7 -6
  14. package/lib/plugins/blur-pausing-game-plugin.js +3 -3
  15. package/lib/plugins/fullscreen-button-game-plugin.js +6 -6
  16. package/lib/plugins/metrics-game-plugin.d.ts +13 -0
  17. package/lib/plugins/metrics-game-plugin.js +47 -0
  18. package/lib/plugins/storage-game-plugin.js +1 -1
  19. package/lib/plugins/time-keys-game-plugin.js +1 -1
  20. package/lib/plugins/version-label-game-plugin.d.ts +2 -0
  21. package/lib/plugins/version-label-game-plugin.js +28 -17
  22. package/lib/storage/get-client-storage.js +3 -3
  23. package/lib/storage/in-memory-storage.js +1 -1
  24. package/lib/storage/local-json-store.js +0 -1
  25. package/lib/tracking/framerate-counts.d.ts +10 -0
  26. package/lib/tracking/framerate-counts.js +47 -0
  27. package/lib/tracking/properties-provider.d.ts +4 -0
  28. package/lib/tracking/properties-provider.js +2 -0
  29. package/lib/tracking/tracker.d.ts +7 -0
  30. package/lib/tracking/tracker.js +2 -0
  31. package/lib/tracking/tracking-screen-delegate.d.ts +14 -0
  32. package/lib/tracking/tracking-screen-delegate.js +38 -0
  33. package/lib/ui/screen.d.ts +4 -4
  34. package/lib/ui/screen.js +3 -3
  35. package/lib/util/math.js +0 -1
  36. package/lib/util/navigation-flow.d.ts +1 -0
  37. package/lib/util/navigation-flow.js +12 -0
  38. package/package.json +1 -1
  39. package/src/game/game-params.ts +1 -1
  40. package/src/game/game-plugin.ts +5 -14
  41. package/src/game/game.ts +23 -17
  42. package/src/index.ts +26 -19
  43. package/src/interaction/inputs.ts +43 -23
  44. package/src/interaction/screen-mouse-inputs.ts +7 -3
  45. package/src/navigation/resolver.ts +3 -6
  46. package/src/navigation/screen-stack.ts +15 -12
  47. package/src/plugins/automation-game-plugin.ts +8 -8
  48. package/src/plugins/blur-pausing-game-plugin.ts +3 -4
  49. package/src/plugins/fullscreen-button-game-plugin.ts +11 -7
  50. package/src/plugins/metrics-game-plugin.ts +65 -0
  51. package/src/plugins/storage-game-plugin.ts +1 -1
  52. package/src/plugins/time-keys-game-plugin.ts +7 -6
  53. package/src/plugins/version-label-game-plugin.ts +30 -18
  54. package/src/storage/get-client-storage.ts +4 -4
  55. package/src/storage/in-memory-storage.ts +1 -3
  56. package/src/storage/local-json-store.ts +10 -12
  57. package/src/tracking/framerate-counts.ts +50 -0
  58. package/src/tracking/properties-provider.ts +5 -0
  59. package/src/tracking/tracker.ts +6 -0
  60. package/src/tracking/tracking-screen-delegate.ts +48 -0
  61. package/src/ui/screen-delegate.ts +1 -1
  62. package/src/ui/screen.ts +29 -14
  63. package/src/util/math.ts +1 -1
  64. package/src/util/navigation-flow.ts +8 -0
@@ -0,0 +1,65 @@
1
+ import { GamePlugin } from "../game/game-plugin";
2
+ import { UserCancelledError } from "../navigation/resolver";
3
+ import { FrameRateCounts } from "../tracking/framerate-counts";
4
+ import { Tracker } from "../tracking/tracker";
5
+ import { TrackingScreenDelegate } from "../tracking/tracking-screen-delegate";
6
+ import { Screen } from "../ui/screen";
7
+
8
+ export class MetricsGamePlugin extends GamePlugin {
9
+ static readonly key = "metrics";
10
+ readonly key = MetricsGamePlugin.key;
11
+
12
+ private framerateCounts: FrameRateCounts;
13
+
14
+ constructor(readonly tracker: Tracker) {
15
+ super();
16
+ }
17
+
18
+ setup(): void {
19
+ super.setup();
20
+
21
+ this.framerateCounts = new FrameRateCounts(
22
+ this.game.performanceRecorder,
23
+ );
24
+
25
+ window.addEventListener(
26
+ "error",
27
+ (event) => {
28
+ this.tracker.track("errorOccurred", {
29
+ errorType: "error",
30
+ message: event.message,
31
+ lineno: event.lineno,
32
+ });
33
+ return false;
34
+ },
35
+ false,
36
+ );
37
+ window.addEventListener(
38
+ "unhandledrejection",
39
+ (err) => {
40
+ if (err.reason instanceof UserCancelledError) {
41
+ return;
42
+ }
43
+
44
+ this.tracker.track("errorOccurred", {
45
+ errorType: "unhandledrejection",
46
+ message: err.reason.message,
47
+ });
48
+ return false;
49
+ },
50
+ false,
51
+ );
52
+
53
+ this.tracker.track("gameSetup", {});
54
+ }
55
+
56
+ cycle(elapsed: number): void {
57
+ super.cycle(elapsed);
58
+ this.framerateCounts.cycle(elapsed);
59
+ }
60
+
61
+ setupScreen(screen: Screen): void {
62
+ super.setupScreen(screen);
63
+ screen.delegate = new TrackingScreenDelegate(screen, this.tracker);
64
+ }
65
+ }
@@ -3,7 +3,7 @@ import { getClientStorage } from "../storage/get-client-storage";
3
3
  import { LocalJsonStore } from "../storage/local-json-store";
4
4
 
5
5
  export class StorageGamePlugin extends GamePlugin {
6
- static readonly key = 'storage';
6
+ static readonly key = "storage";
7
7
  readonly key = StorageGamePlugin.key;
8
8
  readonly localJsonStore = new LocalJsonStore(getClientStorage());
9
9
  }
@@ -2,17 +2,18 @@ import { Keyboard } from "@remvst/client-inputs";
2
2
  import { GamePlugin } from "../game/game-plugin";
3
3
 
4
4
  export class TimeKeysGamePlugin extends GamePlugin {
5
-
6
- static readonly key = 'time-keys';
5
+ static readonly key = "time-keys";
7
6
  readonly key = TimeKeysGamePlugin.key;
8
7
 
9
8
  speedUpFactor: number;
10
9
  slowDownFactor: number;
11
10
 
12
- constructor(options: {
13
- speedUpFactor?: number,
14
- slowDownFactor?: number,
15
- } = {}) {
11
+ constructor(
12
+ options: {
13
+ speedUpFactor?: number;
14
+ slowDownFactor?: number;
15
+ } = {},
16
+ ) {
16
17
  super();
17
18
 
18
19
  this.speedUpFactor = options.speedUpFactor || 10;
@@ -1,14 +1,40 @@
1
1
  import { GamePlugin } from "../game/game-plugin";
2
2
 
3
3
  export class VersionLabelGamePlugin extends GamePlugin {
4
-
5
4
  static readonly key = "version-label";
6
5
  readonly key = VersionLabelGamePlugin.key;
7
6
 
7
+ readonly labelElement = (() => {
8
+ const label = document.createElement("div");
9
+ label.className = "version-label";
10
+ label.innerText = this.label;
11
+ return this.labelElement;
12
+ })();
13
+
14
+ readonly styleElement = (() => {
15
+ const styleElt = document.createElement("style");
16
+ styleElt.innerHTML = `
17
+ .version-label {
18
+ position: absolute;
19
+ z-index: 2;
20
+ margin: 10px;
21
+ color: #fff;
22
+ bottom: 0;
23
+ right: 0;
24
+ text-transform: uppercase;
25
+ text-shadow: 0px 2px 0px #000;
26
+ font-family: Courier;
27
+ font-size: 10px;
28
+ ${this.extraStyle}
29
+ }
30
+ `;
31
+ return styleElt;
32
+ })();
33
+
8
34
  constructor(
9
35
  private readonly container: HTMLElement,
10
36
  private readonly label: string,
11
- private readonly extraStyle: string = '',
37
+ private readonly extraStyle: string = "",
12
38
  ) {
13
39
  super();
14
40
  }
@@ -16,21 +42,7 @@ export class VersionLabelGamePlugin extends GamePlugin {
16
42
  setup(): void {
17
43
  super.setup();
18
44
 
19
- const button = document.createElement('div');
20
- button.innerText = this.label;
21
- button.style.cssText = `
22
- position: absolute;
23
- z-index: 2;
24
- margin: 10px;
25
- color: #fff;
26
- bottom: 0;
27
- right: 0;
28
- text-transform: uppercase;
29
- text-shadow: 0px 2px 0px #000;
30
- font-family: Courier;
31
- font-size: 10px;
32
- ${this.extraStyle}
33
- `;
34
- this.container.appendChild(button);
45
+ document.head.appendChild(this.styleElement);
46
+ this.container.appendChild(this.labelElement);
35
47
  }
36
48
  }
@@ -1,10 +1,10 @@
1
1
  import { InMemoryStorage } from "./in-memory-storage";
2
2
 
3
3
  export function getClientStorage(): Storage {
4
- const options: ([string, () => Storage])[] = [
5
- ['localStorage', () => window.localStorage],
6
- ['sessionStorage', () => window.sessionStorage],
7
- ['InMemoryStorage', () => new InMemoryStorage()],
4
+ const options: [string, () => Storage][] = [
5
+ ["localStorage", () => window.localStorage],
6
+ ["sessionStorage", () => window.sessionStorage],
7
+ ["InMemoryStorage", () => new InMemoryStorage()],
8
8
  ];
9
9
 
10
10
  for (const [label, getter] of options) {
@@ -1,6 +1,5 @@
1
1
  export class InMemoryStorage implements Storage {
2
-
3
- private readonly items = new Map<string, string>;
2
+ private readonly items = new Map<string, string>();
4
3
 
5
4
  get length(): number {
6
5
  return this.items.size;
@@ -25,5 +24,4 @@ export class InMemoryStorage implements Storage {
25
24
  setItem(key: string, value: string): void {
26
25
  this.items.set(key, value);
27
26
  }
28
-
29
27
  }
@@ -2,9 +2,7 @@ export class StoredItem<T> {
2
2
  constructor(
3
3
  private readonly localJsonStore: LocalJsonStore,
4
4
  readonly key: string,
5
- ) {
6
-
7
- }
5
+ ) {}
8
6
 
9
7
  get(): Promise<T | null> {
10
8
  return this.localJsonStore.getItem(this.key);
@@ -20,10 +18,7 @@ export class StoredItem<T> {
20
18
  }
21
19
 
22
20
  export class LocalJsonStore {
23
- constructor(
24
- private readonly localStorage: Storage,
25
- ) {
26
- }
21
+ constructor(private readonly localStorage: Storage) {}
27
22
 
28
23
  item<T>(key: string): StoredItem<T> {
29
24
  return new StoredItem(this, key);
@@ -33,16 +28,16 @@ export class LocalJsonStore {
33
28
  try {
34
29
  this.localStorage.setItem(key, JSON.stringify(value));
35
30
  } catch (err) {
36
- console.error(`setItem error: ${err.message}`, err)
31
+ console.error(`setItem error: ${err.message}`, err);
37
32
  throw err;
38
33
  }
39
34
  }
40
35
 
41
36
  async deleteItem(key: string): Promise<void> {
42
37
  try {
43
- this.localStorage.removeItem(key);
38
+ this.localStorage.removeItem(key);
44
39
  } catch (err) {
45
- console.error(`deleteItem error: ${err.message}`, err)
40
+ console.error(`deleteItem error: ${err.message}`, err);
46
41
  throw err;
47
42
  }
48
43
  }
@@ -53,8 +48,11 @@ export class LocalJsonStore {
53
48
  if (!stored) return null;
54
49
  return JSON.parse(stored);
55
50
  } catch (err) {
56
- console.error(`getItem error: ${err.message}. Falling back to null`, err)
51
+ console.error(
52
+ `getItem error: ${err.message}. Falling back to null`,
53
+ err,
54
+ );
57
55
  return null;
58
56
  }
59
57
  }
60
- };
58
+ }
@@ -0,0 +1,50 @@
1
+ import { PerformanceRecorder } from "@remvst/client-performance";
2
+ import { Properties } from "./tracker";
3
+
4
+ export class FrameRateCounts {
5
+ private readonly counts = {
6
+ above60: 0,
7
+ above45: 0,
8
+ above30: 0,
9
+ above15: 0,
10
+ above0: 0,
11
+ };
12
+
13
+ private age = 0;
14
+
15
+ constructor(private readonly performanceRecorder: PerformanceRecorder) {}
16
+
17
+ cycle(elapsed: number) {
18
+ this.age += elapsed;
19
+
20
+ if (this.age > 0.5) {
21
+ const previousFrame = this.performanceRecorder.frame(
22
+ this.performanceRecorder.frameCount - 1,
23
+ );
24
+ const currentFrame = this.performanceRecorder.frame(
25
+ this.performanceRecorder.frameCount,
26
+ );
27
+ const framerate =
28
+ 1000 /
29
+ (currentFrame.onStartTimes.get("FRAME") -
30
+ previousFrame.onStartTimes.get("FRAME"));
31
+ let key: keyof typeof this.counts;
32
+ if (framerate > 60) {
33
+ key = "above60";
34
+ } else if (framerate > 45) {
35
+ key = "above45";
36
+ } else if (framerate > 30) {
37
+ key = "above30";
38
+ } else if (framerate > 15) {
39
+ key = "above15";
40
+ } else {
41
+ key = "above0";
42
+ }
43
+ this.counts[key]++;
44
+ }
45
+ }
46
+
47
+ toJson(): Properties {
48
+ return this.counts;
49
+ }
50
+ }
@@ -0,0 +1,5 @@
1
+ import { Properties } from "./tracker";
2
+
3
+ export interface PropertiesProvider {
4
+ readonly properties: Properties;
5
+ }
@@ -0,0 +1,6 @@
1
+ export type PropertyValue = string | number | boolean;
2
+ export type Properties = { [key: string]: PropertyValue | PropertyValue[] };
3
+
4
+ export interface Tracker {
5
+ track(eventName: string, properties: Properties): void;
6
+ }
@@ -0,0 +1,48 @@
1
+ import { Screen } from "../ui/screen";
2
+ import { ScreenDelegate } from "../ui/screen-delegate";
3
+ import { PropertiesProvider } from "./properties-provider";
4
+ import { Properties, Tracker } from "./tracker";
5
+
6
+ export class TrackingScreenDelegate implements ScreenDelegate {
7
+ constructor(
8
+ private readonly screen: Screen,
9
+ private readonly tracker: Tracker,
10
+ ) {}
11
+
12
+ private properties(extraProperties: Properties) {
13
+ const merged: Properties = {
14
+ "screen.id": this.screen.id,
15
+ "screen.age": this.screen.age,
16
+ };
17
+
18
+ Object.assign(merged, extraProperties);
19
+
20
+ const screenProperties = (this.screen as any as PropertiesProvider)
21
+ .properties;
22
+ if (screenProperties) {
23
+ Object.assign(merged, screenProperties);
24
+ }
25
+
26
+ return merged;
27
+ }
28
+
29
+ private track(eventName: string, extraProperties: Properties = {}) {
30
+ this.tracker.track(eventName, this.properties(extraProperties));
31
+ }
32
+
33
+ onSetup(): void {
34
+ this.track("screenSetup");
35
+ }
36
+
37
+ onForeground(): void {
38
+ this.track("screenForeground");
39
+ }
40
+
41
+ onBackground(): void {
42
+ this.track("screenBackground");
43
+ }
44
+
45
+ onDestroy(): void {
46
+ this.track("screenDestroy");
47
+ }
48
+ }
@@ -3,4 +3,4 @@ export interface ScreenDelegate {
3
3
  onForeground(): void;
4
4
  onBackground(): void;
5
5
  onDestroy(): void;
6
- }
6
+ }
package/src/ui/screen.ts CHANGED
@@ -1,12 +1,11 @@
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';
1
+ import { InterpolationPool } from "@remvst/animate.js";
2
+ import { Game } from "../game/game";
3
+ import { ScreenInputs } from "../interaction/inputs";
4
+ import { ScreenDelegate } from "./screen-delegate";
5
5
 
6
6
  export abstract class Screen {
7
-
8
7
  game: Game;
9
- cursorType: string = 'default';
8
+ cursorType: string = "default";
10
9
 
11
10
  inputs: ScreenInputs;
12
11
  age: number = 0;
@@ -42,9 +41,25 @@ export abstract class Screen {
42
41
  this.inputs = new ScreenInputs(this);
43
42
  this.inputs.keyboard.onKeyDown = (keyCode) => this.keyDown(keyCode);
44
43
  this.inputs.keyboard.onKeyUp = (keyCode) => this.keyUp(keyCode);
45
- this.inputs.mouse.onMouseDown = (button) => this.mouseDown(button, this.inputs.mouse.position.x, this.inputs.mouse.position.y);
46
- this.inputs.mouse.onMouseUp = (button) => this.mouseUp(button, this.inputs.mouse.position.x, this.inputs.mouse.position.y);
47
- this.inputs.mouse.onMouseMove = (movementX, movementY) => this.mouseMove(this.inputs.mouse.position.x, this.inputs.mouse.position.y, movementX, movementY);
44
+ this.inputs.mouse.onMouseDown = (button) =>
45
+ this.mouseDown(
46
+ button,
47
+ this.inputs.mouse.position.x,
48
+ this.inputs.mouse.position.y,
49
+ );
50
+ this.inputs.mouse.onMouseUp = (button) =>
51
+ this.mouseUp(
52
+ button,
53
+ this.inputs.mouse.position.x,
54
+ this.inputs.mouse.position.y,
55
+ );
56
+ this.inputs.mouse.onMouseMove = (movementX, movementY) =>
57
+ this.mouseMove(
58
+ this.inputs.mouse.position.x,
59
+ this.inputs.mouse.position.y,
60
+ movementX,
61
+ movementY,
62
+ );
48
63
  this.inputs.mouse.onWheel = (x, y, z) => this.wheel(x, y, z);
49
64
  this.inputs.setup();
50
65
 
@@ -144,9 +159,9 @@ export abstract class Screen {
144
159
  }
145
160
  }
146
161
 
147
- addDebugValues(values: {[key: string]: any}) {
148
- values['screen.id'] = this.id;
149
- values['screen.interpolations'] = this.interpolationPool.size;
162
+ addDebugValues(values: { [key: string]: any }) {
163
+ values["screen.id"] = this.id;
164
+ values["screen.interpolations"] = this.interpolationPool.size;
150
165
  }
151
166
 
152
167
  get isForeground() {
@@ -166,7 +181,7 @@ export abstract class Screen {
166
181
  }
167
182
 
168
183
  get isCycling() {
169
- for (let i = this.game.screenStack.screens.length - 1 ; i >= 0 ; i--) {
184
+ for (let i = this.game.screenStack.screens.length - 1; i >= 0; i--) {
170
185
  const screen = this.game.screenStack.screens[i];
171
186
  if (screen === this) return true;
172
187
  if (screen.absorbCycle) return false;
@@ -175,7 +190,7 @@ export abstract class Screen {
175
190
  }
176
191
 
177
192
  get isTakingInputs() {
178
- for (let i = this.game.screenStack.screens.length - 1 ; i >= 0 ; i--) {
193
+ for (let i = this.game.screenStack.screens.length - 1; i >= 0; i--) {
179
194
  const screen = this.game.screenStack.screens[i];
180
195
  if (screen === this) return true;
181
196
  if (screen.absorbInputs) return false;
package/src/util/math.ts CHANGED
@@ -2,4 +2,4 @@ export function between(a: number, b: number, c: number) {
2
2
  if (b < a) return a;
3
3
  if (b > c) return c;
4
4
  return b;
5
- };
5
+ }
@@ -0,0 +1,8 @@
1
+ import { UserCancelledError } from "../navigation/resolver";
2
+
3
+ export function navigationFlow<T>(flow: Promise<T>) {
4
+ flow.catch((err) => {
5
+ if (err instanceof UserCancelledError) return;
6
+ console.error(err);
7
+ });
8
+ }