@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.
- package/lib/game/game-params.d.ts +6 -0
- package/lib/game/game-params.js +3 -0
- package/lib/game/game-plugin.d.ts +11 -0
- package/lib/game/game-plugin.js +14 -0
- package/lib/game/game.d.ts +32 -0
- package/lib/game/game.js +113 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.js +29 -0
- package/lib/interaction/inputs.d.ts +28 -0
- package/lib/interaction/inputs.js +103 -0
- package/lib/interaction/screen-mouse-inputs.d.ts +9 -0
- package/lib/interaction/screen-mouse-inputs.js +20 -0
- package/lib/navigation/resolver.d.ts +15 -0
- package/lib/navigation/resolver.js +35 -0
- package/lib/navigation/screen-stack.d.ts +22 -0
- package/lib/navigation/screen-stack.js +93 -0
- package/lib/plugins/automation-game-plugin.d.ts +14 -0
- package/lib/plugins/automation-game-plugin.js +34 -0
- package/lib/plugins/blur-pausing-game-plugin.d.ts +9 -0
- package/lib/plugins/blur-pausing-game-plugin.js +28 -0
- package/lib/plugins/time-keys-game-plugin.d.ts +8 -0
- package/lib/plugins/time-keys-game-plugin.js +26 -0
- package/lib/ui/screen-delegate.d.ts +6 -0
- package/lib/ui/screen-delegate.js +2 -0
- package/lib/ui/screen.d.ts +38 -0
- package/lib/ui/screen.js +130 -0
- package/lib/util/math.d.ts +1 -0
- package/lib/util/math.js +12 -0
- package/package.json +21 -0
- package/src/game/game-params.ts +6 -0
- package/src/game/game-plugin.ts +23 -0
- package/src/game/game.ts +153 -0
- package/src/index.ts +18 -0
- package/src/interaction/inputs.ts +118 -0
- package/src/interaction/screen-mouse-inputs.ts +22 -0
- package/src/navigation/resolver.ts +49 -0
- package/src/navigation/screen-stack.ts +112 -0
- package/src/plugins/automation-game-plugin.ts +40 -0
- package/src/plugins/blur-pausing-game-plugin.ts +26 -0
- package/src/plugins/time-keys-game-plugin.ts +24 -0
- package/src/ui/screen-delegate.ts +6 -0
- package/src/ui/screen.ts +167 -0
- package/src/util/math.ts +5 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { OperationQueue } from '@remvst/optimization';
|
|
2
|
+
import { ResolverHolder, Resolver } from './resolver';
|
|
3
|
+
import { Game } from '../game/game';
|
|
4
|
+
import { Screen } from '../ui/screen';
|
|
5
|
+
|
|
6
|
+
export enum NavigationType {
|
|
7
|
+
RESET,
|
|
8
|
+
PUSH,
|
|
9
|
+
REPLACE_EXISTING,
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class ScreenStack {
|
|
13
|
+
|
|
14
|
+
readonly screens: Screen[] = [];
|
|
15
|
+
private readonly queue = new OperationQueue();
|
|
16
|
+
prepareScreen: (screen: Screen) => void = () => {};
|
|
17
|
+
|
|
18
|
+
constructor(private readonly game: Game) {
|
|
19
|
+
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
findByType<T extends Screen>(screenType: (new (...params: any) => T)): T | null {
|
|
23
|
+
return (this.screens.find((scr) => scr instanceof screenType) as T) || null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
current() {
|
|
27
|
+
return this.screens[this.screens.length - 1] || null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
popTo(screen: Screen, inclusive: boolean = false) {
|
|
31
|
+
while (this.current() && this.current() !== screen) {
|
|
32
|
+
this.pop();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (inclusive && this.current() === screen) {
|
|
36
|
+
this.pop();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
push(screen: Screen) {
|
|
41
|
+
this.queue.execute(() => {
|
|
42
|
+
const previousScreen = this.current();
|
|
43
|
+
|
|
44
|
+
screen.bind(this.game);
|
|
45
|
+
screen.setup();
|
|
46
|
+
|
|
47
|
+
this.screens.push(screen);
|
|
48
|
+
|
|
49
|
+
if (previousScreen) {
|
|
50
|
+
previousScreen.background();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
screen.foreground();
|
|
54
|
+
|
|
55
|
+
screen.cycle(0);
|
|
56
|
+
screen.postCycle(0);
|
|
57
|
+
|
|
58
|
+
this.game.onCurrentScreenChanged();
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
pop() {
|
|
63
|
+
this.queue.execute(() => {
|
|
64
|
+
const screen = this.screens.pop();
|
|
65
|
+
if (screen) {
|
|
66
|
+
screen.destroy();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const newScreen = this.screens[this.screens.length - 1];
|
|
70
|
+
if (newScreen) {
|
|
71
|
+
newScreen.foreground();
|
|
72
|
+
this.game.onCurrentScreenChanged();
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
reset(screen: Screen) {
|
|
78
|
+
this.queue.execute(() => {
|
|
79
|
+
while (this.screens.length > 0) {
|
|
80
|
+
// Not calling pop() in case the child class needs to do something in it
|
|
81
|
+
const popped = this.screens.pop();
|
|
82
|
+
popped.destroy();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
this.push(screen);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
resolvableScreen<T>(
|
|
90
|
+
screen: Screen & ResolverHolder<T>,
|
|
91
|
+
navigationType: NavigationType = NavigationType.PUSH,
|
|
92
|
+
dismissWhenResolved: boolean = true,
|
|
93
|
+
): Promise<T> {
|
|
94
|
+
return new Promise((resolve, reject) => {
|
|
95
|
+
screen.resolver = new Resolver(screen, resolve, reject);
|
|
96
|
+
screen.resolver.dismissWhenResolved = dismissWhenResolved;
|
|
97
|
+
switch (navigationType) {
|
|
98
|
+
case NavigationType.PUSH:
|
|
99
|
+
this.push(screen);
|
|
100
|
+
break;
|
|
101
|
+
case NavigationType.RESET:
|
|
102
|
+
this.reset(screen);
|
|
103
|
+
break;
|
|
104
|
+
case NavigationType.REPLACE_EXISTING:
|
|
105
|
+
const existing = this.findByType(screen.constructor as new (...params: any) => Screen) as Screen;
|
|
106
|
+
if (existing) this.popTo(existing, true);
|
|
107
|
+
this.push(screen);
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { GamePlugin } from "../game/game-plugin";
|
|
2
|
+
|
|
3
|
+
export class AutomationGamePlugin extends GamePlugin {
|
|
4
|
+
|
|
5
|
+
static readonly key = 'automation';
|
|
6
|
+
readonly key = AutomationGamePlugin.key;
|
|
7
|
+
|
|
8
|
+
// Debug
|
|
9
|
+
protected automationLabel: HTMLDivElement;
|
|
10
|
+
|
|
11
|
+
get timeFactor(): number {
|
|
12
|
+
return this.automationTimeFactor;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
constructor(private readonly automationTimeFactor = 5) {
|
|
16
|
+
super();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
setup(): void {
|
|
20
|
+
this.automationLabel = document.createElement('div');
|
|
21
|
+
this.automationLabel.id = 'automation-label';
|
|
22
|
+
document.body.appendChild(this.automationLabel);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
render(): void {
|
|
26
|
+
const automation = {};
|
|
27
|
+
this.getAutomationValues(automation);
|
|
28
|
+
this.automationLabel.innerText = JSON.stringify(automation);
|
|
29
|
+
|
|
30
|
+
// Disable all CSS animations for automation
|
|
31
|
+
const styleElement = document.createElement('style');
|
|
32
|
+
styleElement.setAttribute('id','style-tag');
|
|
33
|
+
styleElement.innerHTML = '*,:after,:before,.animate-in{transition:none!important;animation:none!important;}';
|
|
34
|
+
document.head.appendChild(styleElement);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
getAutomationValues(out: {[key: string]: any}) {
|
|
38
|
+
this.game.getDebugValues(out);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { GamePlugin } from "../game/game-plugin";
|
|
2
|
+
|
|
3
|
+
export class BlurPausingGamePlugin extends GamePlugin {
|
|
4
|
+
|
|
5
|
+
static readonly key = 'blur-pausing-game';
|
|
6
|
+
readonly key = BlurPausingGamePlugin.key;
|
|
7
|
+
|
|
8
|
+
hasFocus = true;
|
|
9
|
+
|
|
10
|
+
setup(): void {
|
|
11
|
+
window.addEventListener('blur', () => this.windowHasFocus(false), false);
|
|
12
|
+
window.addEventListener('focus', () => this.windowHasFocus(true), false);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
render(): void {
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
windowHasFocus(hasFocus: boolean) {
|
|
19
|
+
this.hasFocus = hasFocus;
|
|
20
|
+
if (hasFocus) {
|
|
21
|
+
this.game.scheduleFrame();
|
|
22
|
+
} else {
|
|
23
|
+
this.game.stopLoop();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Keyboard } from "@remvst/client-inputs";
|
|
2
|
+
import { GamePlugin } from "../game/game-plugin";
|
|
3
|
+
|
|
4
|
+
export class TimeKeysGamePlugin extends GamePlugin {
|
|
5
|
+
|
|
6
|
+
static readonly key = 'time-keys';
|
|
7
|
+
readonly key = TimeKeysGamePlugin.key;
|
|
8
|
+
|
|
9
|
+
get timeFactor(): number {
|
|
10
|
+
if (this.game.inputs.keyboard.isDown(Keyboard.F)) {
|
|
11
|
+
return 500;
|
|
12
|
+
} else if (this.game.inputs.keyboard.isDown(Keyboard.G)) {
|
|
13
|
+
return 0.1;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return 1;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
setup(): void {
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
render(): void {
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/ui/screen.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
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
|
+
|
|
6
|
+
export abstract class Screen {
|
|
7
|
+
|
|
8
|
+
game: Game;
|
|
9
|
+
cursorType: string = 'default';
|
|
10
|
+
|
|
11
|
+
inputs: ScreenInputs;
|
|
12
|
+
age: number = 0;
|
|
13
|
+
frameCount: number = 0;
|
|
14
|
+
interpolationPool: InterpolationPool;
|
|
15
|
+
delegate: ScreenDelegate | null = null;
|
|
16
|
+
|
|
17
|
+
private readonly subscreens: Screen[] = [];
|
|
18
|
+
|
|
19
|
+
constructor() {
|
|
20
|
+
this.game = null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
addSubscreen(screen: Screen) {
|
|
24
|
+
this.subscreens.push(screen);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
bind(game: Game) {
|
|
28
|
+
this.game = game;
|
|
29
|
+
|
|
30
|
+
for (const plugin of this.game.plugins) {
|
|
31
|
+
plugin.setupScreen(this);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
for (const sub of this.subscreens) {
|
|
35
|
+
sub.bind(game);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
abstract get id(): string;
|
|
40
|
+
|
|
41
|
+
setup() {
|
|
42
|
+
this.inputs = new ScreenInputs(this);
|
|
43
|
+
this.inputs.keyboard.onKeyDown = (keyCode) => this.keyDown(keyCode);
|
|
44
|
+
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);
|
|
48
|
+
this.inputs.mouse.onWheel = (x, y, z) => this.wheel(x, y, z);
|
|
49
|
+
this.inputs.setup();
|
|
50
|
+
|
|
51
|
+
this.interpolationPool = new InterpolationPool();
|
|
52
|
+
this.delegate?.onSetup();
|
|
53
|
+
|
|
54
|
+
for (const sub of this.subscreens) {
|
|
55
|
+
sub.setup();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
destroy() {
|
|
60
|
+
this.delegate?.onDestroy();
|
|
61
|
+
|
|
62
|
+
for (const sub of this.subscreens) {
|
|
63
|
+
sub.destroy();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
foreground() {
|
|
68
|
+
this.delegate?.onForeground();
|
|
69
|
+
|
|
70
|
+
for (const sub of this.subscreens) {
|
|
71
|
+
sub.foreground();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
background() {
|
|
76
|
+
const newScreen = this.game.screenStack.current();
|
|
77
|
+
if (newScreen.absorbInputs) {
|
|
78
|
+
this.inputs.reset();
|
|
79
|
+
}
|
|
80
|
+
this.delegate?.onBackground();
|
|
81
|
+
|
|
82
|
+
for (const sub of this.subscreens) {
|
|
83
|
+
sub.background();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
cycle(elapsed: number) {
|
|
88
|
+
this.age += elapsed;
|
|
89
|
+
this.frameCount++;
|
|
90
|
+
|
|
91
|
+
for (const sub of this.subscreens) {
|
|
92
|
+
sub.inputs.import(this.inputs);
|
|
93
|
+
sub.cycle(elapsed);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
postCycle(elapsed: number) {
|
|
98
|
+
if (this.interpolationPool) {
|
|
99
|
+
this.interpolationPool.cycle(elapsed);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
for (const sub of this.subscreens) {
|
|
103
|
+
sub.postCycle(elapsed);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
keyDown(keyCode: number) {
|
|
108
|
+
for (const subscreen of this.subscreens) {
|
|
109
|
+
subscreen.keyDown(keyCode);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
keyUp(keyCode: number) {
|
|
114
|
+
for (const subscreen of this.subscreens) {
|
|
115
|
+
subscreen.keyUp(keyCode);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
protected skip() {
|
|
120
|
+
this.interpolationPool.skip();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
mouseDown(button: number, x: number, y: number) {
|
|
124
|
+
for (const subscreen of this.subscreens) {
|
|
125
|
+
subscreen.mouseDown(button, x, y);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
mouseMove(x: number, y: number, movementX: number, movementY: number) {
|
|
130
|
+
for (const subscreen of this.subscreens) {
|
|
131
|
+
subscreen.mouseMove(x, y, movementX, movementY);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
mouseUp(button: number, x: number, y: number) {
|
|
136
|
+
for (const subscreen of this.subscreens) {
|
|
137
|
+
subscreen.mouseUp(button, x, y);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
wheel(deltaX: number, deltaY: number, deltaZ: number) {
|
|
142
|
+
for (const subscreen of this.subscreens) {
|
|
143
|
+
subscreen.wheel(deltaX, deltaY, deltaZ);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
addDebugValues(values: {[key: string]: any}) {
|
|
148
|
+
values['screen.id'] = this.id;
|
|
149
|
+
values['screen.interpolations'] = this.interpolationPool.size;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
get isForeground() {
|
|
153
|
+
return this.game && this.game.screenStack.current() === this;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
get absorbInputs() {
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
get absorbCycle() {
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
get timeFactor() {
|
|
165
|
+
return 1;
|
|
166
|
+
}
|
|
167
|
+
}
|
package/src/util/math.ts
ADDED
package/tsconfig.json
ADDED