-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.ts
192 lines (161 loc) · 6.54 KB
/
game.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import * as PIXI from "pixi.js";
import { logger } from "./logger";
import IGame from "../interfaces/IGame";
import IGameState from "../interfaces/IGameState";
import IKeyboard from "../interfaces/IKeyboard";
import IScene from "../interfaces/IScene";
import IActor from "../interfaces/IActor";
import Timer from "./timer";
import LoadingScene from "../scenes/LoadingScene";
import PauseScene from "../scenes/PauseScene";
import GameOverScene from "../scenes/GameOverScene";
import Scene from "../scenes/Scene";
import MenuScene from "../scenes/MenuScene";
export const KEYBOARD_DISABLE_FRAMES = 30.0;
export function getInitialGameState(): IGameState {
return {
flags: {
gameOver: false,
paused: false,
doGameOver: true,
doPause: true,
},
functions: {
tick: playTick,
},
};
}
function getDimensionPercent(element: HTMLCanvasElement, dimension: "width" | "height", percentage: number): number {
percentage = Math.min(Math.max(percentage, 0), 100);
return element[dimension] * (percentage / 100);
}
export default class Game implements IGame {
_app: PIXI.Application;
renderer: PIXI.AbstractRenderer;
stage: PIXI.Container;
width: (percentage: number) => number;
height: (percentage: number) => number;
scene: IScene;
prevScene: IScene;
sprites: {[key: string]: () => IActor} = {};
timers: Timer[] = [];
_prevTick = playTick;
state = getInitialGameState();
static entryScene: typeof Scene = MenuScene;
static gameOverScene: typeof Scene = GameOverScene;
static pauseScene: typeof Scene = PauseScene;
static loadingScene: typeof Scene = LoadingScene;
constructor(app: PIXI.Application, sprites: {[key: string]: (game: IGame) => IActor}, keyboard: IKeyboard) {
this._app = app;
this.renderer = app.renderer;
this.stage = app.stage;
this.width = (percentage: number) => getDimensionPercent(app.view, "width", percentage);
this.height = (percentage: number) => getDimensionPercent(app.view, "height", percentage);
// Resize scenes when the renderer is resized
(app.renderer as any).on("resize", (width: number, height: number) => {
logger.info(`Renderer resized to ${width}x${height}`);
this.scene.resize();
});
// Make sprite builders by giving this Game instance to all the functions
for (let sprite of Object.keys(sprites)) {
this.sprites[sprite] = (): IActor => sprites[sprite](this);
}
// Create scenes
logger.info(`Game created with dimensions ${this.width(100)}x${this.height(100)}`);
this.scene = new Game.entryScene(this);
this.prevScene = new Game.loadingScene(this);
this.scene.mount(this.stage);
app.ticker.add((delta) => this.state.functions.tick(this, delta, keyboard));
};
changeScene(scene: IScene) {
/* Assign current `scene` to `prevScene` & change current `scene`
* This method mounts the new scene to `game.stage` but does not unmount anything
* The new scene usually unmounts the prevScene in its beforeMount funcs
*/
this.prevScene = this.scene;
this.scene = scene;
if (scene.mounted !== null) {
logger.info("Tried to remount a scene that is already mounted.");
return
}
try {
scene.mount(this.stage);
} catch (e) {
if (e instanceof TypeError) {
logger.error("Failed to add an undefined Actor to the container. The scene cannot mount.");
return;
}
logger.error(`${(e as Error).name}: ${(e as Error).message}`);
throw e;
}
}
gameOver(keyboard: IKeyboard | undefined = undefined) {
if (this.state.flags.gameOver || !this.state.flags.doGameOver) return;
this.state.flags.gameOver = true;
keyboard?.disable(KEYBOARD_DISABLE_FRAMES);
this.state.functions.tick = gameOverTick;
this.changeScene(new Game.gameOverScene(this));
}
pause(keyboard: IKeyboard | undefined = undefined) {
if (this.state.flags.gameOver || !this.state.flags.doPause) return;
this.state.flags.paused = !this.state.flags.paused;
if (!this.state.flags.paused) {
this.state.functions.tick = this._prevTick;
keyboard?.disable(KEYBOARD_DISABLE_FRAMES);
this.scene.unmount(this.stage);
this.changeScene(this.prevScene);
return;
}
keyboard?.disable(KEYBOARD_DISABLE_FRAMES);
this._prevTick = Object.assign(this.state.functions.tick);
this.state.functions.tick = pauseTick;
this.changeScene(new Game.pauseScene(this));
}
reset() {
logger.debug("RESET GAME: Unmounting scene and prevScene");
this.scene.mounted && this.scene.unmount(this.scene.mounted);
this.prevScene.mounted && this.prevScene.unmount(this.prevScene.mounted);
logger.debug("RESET GAME: Stopping all timers");
this.timers = [];
logger.debug("RESET GAME: Resetting state object to defaults");
this.state = getInitialGameState();
this.scene = new Game.entryScene(this);
this.prevScene = new Game.loadingScene(this);
logger.verbose("RESET GAME: Created entryscene; mounting root container");
logger.verbose("RESET GAME: The prevScene is currently LoadingScene");
this.scene.mount(this.stage);
}
startTimer(f: () => void, ms: number, name?: string) {
return this.timers.push(new Timer(f, ms, name)) - 1;
}
stopTimer(i: number) {
logger.debug(`Stopping the "${this.timers[i].name}" Timer early`);
this.timers[i] = new Timer();
}
}
export function playTick(game: IGame, delta: number, keyboard: IKeyboard) {
logger.spam(`Delta: ${delta}`);
delta = Math.min(delta, 2.0);
// Tick game timers
let timersActive = false;
for (let i = 0; i < game.timers.length; i++) {
if (game.timers[i].called) continue;
timersActive = true;
game.timers[i].tick(delta);
}
if (!timersActive) {
game.timers = [];
}
// Tick keyboard and scene
keyboard.tick(delta);
game.scene.tick(delta, keyboard);
if (keyboard.p) game.pause(keyboard);
}
export function pauseTick(game: IGame, delta: number, keyboard: IKeyboard) {
keyboard.tick(delta);
if (keyboard.p) {
game.pause(keyboard);
}
}
export function gameOverTick(game: IGame, delta: number, keyboard: IKeyboard) {
}