@remvst/game-navigation-core 1.0.0 → 1.0.1

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.
@@ -1,11 +1,12 @@
1
1
  import { Game } from "./game";
2
2
  import { Screen } from '../ui/screen';
3
3
  export declare abstract class GamePlugin {
4
+ abstract get key(): string;
4
5
  protected game: Game;
5
6
  get timeFactor(): number;
6
7
  bind(game: Game): void;
8
+ setup(): void;
7
9
  setupScreen(screen: Screen): void;
8
- abstract get key(): string;
9
- abstract setup(): void;
10
- abstract render(): void;
10
+ cycle(elapsed: number): void;
11
+ render(): void;
11
12
  }
@@ -8,7 +8,13 @@ class GamePlugin {
8
8
  bind(game) {
9
9
  this.game = game;
10
10
  }
11
+ setup() {
12
+ }
11
13
  setupScreen(screen) {
12
14
  }
15
+ cycle(elapsed) {
16
+ }
17
+ render() {
18
+ }
13
19
  }
14
20
  exports.GamePlugin = GamePlugin;
package/lib/game/game.js CHANGED
@@ -77,6 +77,10 @@ class Game {
77
77
  for (const plugin of this.plugins) {
78
78
  adjusted *= plugin.timeFactor;
79
79
  }
80
+ const screen = this.screenStack.current();
81
+ if (screen) {
82
+ adjusted *= this.screenStack.current().timeFactor;
83
+ }
80
84
  this.age += elapsed;
81
85
  const screens = this.screenStack.screens.slice();
82
86
  for (let i = screens.length - 1; i >= 0; i--) {
@@ -87,6 +91,9 @@ class Game {
87
91
  break;
88
92
  }
89
93
  }
94
+ for (const plugin of this.plugins) {
95
+ plugin.cycle(elapsed);
96
+ }
90
97
  }
91
98
  getDebugValues(out) {
92
99
  this.screenStack.current()?.addDebugValues(out);
@@ -12,11 +12,13 @@ class AutomationGamePlugin extends game_plugin_1.GamePlugin {
12
12
  this.key = AutomationGamePlugin.key;
13
13
  }
14
14
  setup() {
15
+ super.setup();
15
16
  this.automationLabel = document.createElement('div');
16
17
  this.automationLabel.id = 'automation-label';
17
18
  document.body.appendChild(this.automationLabel);
18
19
  }
19
20
  render() {
21
+ super.render();
20
22
  const automation = {};
21
23
  this.getAutomationValues(automation);
22
24
  this.automationLabel.innerText = JSON.stringify(automation);
@@ -2,8 +2,6 @@ import { GamePlugin } from "../game/game-plugin";
2
2
  export declare class BlurPausingGamePlugin extends GamePlugin {
3
3
  static readonly key = "blur-pausing-game";
4
4
  readonly key = "blur-pausing-game";
5
- hasFocus: boolean;
6
5
  setup(): void;
7
- render(): void;
8
- windowHasFocus(hasFocus: boolean): void;
6
+ private focusChanged;
9
7
  }
@@ -6,17 +6,14 @@ class BlurPausingGamePlugin extends game_plugin_1.GamePlugin {
6
6
  constructor() {
7
7
  super(...arguments);
8
8
  this.key = BlurPausingGamePlugin.key;
9
- this.hasFocus = true;
10
9
  }
11
10
  setup() {
12
- window.addEventListener('blur', () => this.windowHasFocus(false), false);
13
- window.addEventListener('focus', () => this.windowHasFocus(true), false);
11
+ super.setup();
12
+ window.addEventListener('blur', () => this.focusChanged(), false);
13
+ window.addEventListener('focus', () => this.focusChanged(), false);
14
14
  }
15
- render() {
16
- }
17
- windowHasFocus(hasFocus) {
18
- this.hasFocus = hasFocus;
19
- if (hasFocus) {
15
+ focusChanged() {
16
+ if (document.hasFocus()) {
20
17
  this.game.scheduleFrame();
21
18
  }
22
19
  else {
@@ -3,6 +3,4 @@ export declare class TimeKeysGamePlugin extends GamePlugin {
3
3
  static readonly key = "time-keys";
4
4
  readonly key = "time-keys";
5
5
  get timeFactor(): number;
6
- setup(): void;
7
- render(): void;
8
6
  }
@@ -17,10 +17,6 @@ class TimeKeysGamePlugin extends game_plugin_1.GamePlugin {
17
17
  }
18
18
  return 1;
19
19
  }
20
- setup() {
21
- }
22
- render() {
23
- }
24
20
  }
25
21
  exports.TimeKeysGamePlugin = TimeKeysGamePlugin;
26
22
  TimeKeysGamePlugin.key = 'time-keys';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remvst/game-navigation-core",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -3,6 +3,8 @@ import { Screen } from '../ui/screen';
3
3
 
4
4
  export abstract class GamePlugin {
5
5
 
6
+ abstract get key(): string;
7
+
6
8
  protected game: Game;
7
9
 
8
10
  get timeFactor(): number {
@@ -13,11 +15,19 @@ export abstract class GamePlugin {
13
15
  this.game = game;
14
16
  }
15
17
 
18
+ setup(): void {
19
+
20
+ }
21
+
16
22
  setupScreen(screen: Screen): void {
17
23
 
18
24
  }
19
25
 
20
- abstract get key(): string;
21
- abstract setup(): void;
22
- abstract render(): void;
26
+ cycle(elapsed: number) {
27
+
28
+ }
29
+
30
+ render() {
31
+
32
+ }
23
33
  }
package/src/game/game.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { GameInputs } from '../interaction/inputs';
2
2
  import { GameParams } from './game-params';
3
3
  import { GamePlugin } from './game-plugin';
4
- import { Screen } from '../ui/screen';
5
4
  import { ScreenStack } from '../navigation/screen-stack';
6
5
  import { PerformanceRecorder } from '@remvst/client-performance';
7
6
 
@@ -114,6 +113,11 @@ export abstract class Game<ParamsType extends GameParams = GameParams> {
114
113
  adjusted *= plugin.timeFactor;
115
114
  }
116
115
 
116
+ const screen = this.screenStack.current();
117
+ if (screen) {
118
+ adjusted *= this.screenStack.current().timeFactor;
119
+ }
120
+
117
121
  this.age += elapsed;
118
122
 
119
123
  const screens = this.screenStack.screens.slice();
@@ -126,6 +130,10 @@ export abstract class Game<ParamsType extends GameParams = GameParams> {
126
130
  break;
127
131
  }
128
132
  }
133
+
134
+ for (const plugin of this.plugins) {
135
+ plugin.cycle(elapsed);
136
+ }
129
137
  }
130
138
 
131
139
  getDebugValues(out: {[key: string]: any}) {
@@ -17,12 +17,16 @@ export class AutomationGamePlugin extends GamePlugin {
17
17
  }
18
18
 
19
19
  setup(): void {
20
+ super.setup();
21
+
20
22
  this.automationLabel = document.createElement('div');
21
23
  this.automationLabel.id = 'automation-label';
22
24
  document.body.appendChild(this.automationLabel);
23
25
  }
24
26
 
25
27
  render(): void {
28
+ super.render();
29
+
26
30
  const automation = {};
27
31
  this.getAutomationValues(automation);
28
32
  this.automationLabel.innerText = JSON.stringify(automation);
@@ -5,19 +5,15 @@ export class BlurPausingGamePlugin extends GamePlugin {
5
5
  static readonly key = 'blur-pausing-game';
6
6
  readonly key = BlurPausingGamePlugin.key;
7
7
 
8
- hasFocus = true;
9
-
10
8
  setup(): void {
11
- window.addEventListener('blur', () => this.windowHasFocus(false), false);
12
- window.addEventListener('focus', () => this.windowHasFocus(true), false);
13
- }
9
+ super.setup();
14
10
 
15
- render(): void {
11
+ window.addEventListener('blur', () => this.focusChanged(), false);
12
+ window.addEventListener('focus', () => this.focusChanged(), false);
16
13
  }
17
14
 
18
- windowHasFocus(hasFocus: boolean) {
19
- this.hasFocus = hasFocus;
20
- if (hasFocus) {
15
+ private focusChanged() {
16
+ if (document.hasFocus()) {
21
17
  this.game.scheduleFrame();
22
18
  } else {
23
19
  this.game.stopLoop();
@@ -15,10 +15,4 @@ export class TimeKeysGamePlugin extends GamePlugin {
15
15
 
16
16
  return 1;
17
17
  }
18
-
19
- setup(): void {
20
- }
21
-
22
- render(): void {
23
- }
24
18
  }