-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
42 lines (41 loc) · 972 Bytes
/
game.js
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
import PlayerSelectionScreen from "./player_selection_screen.js";
export default class Game {
constructor(canvas) {
canvas.style.border = "1px solid black";
this.settings = {};
this.ctx = canvas.getContext("2d");
this.changeScreen(PlayerSelectionScreen);
}
changeScreen(screen, opts) {
this.nextScreen = {
screen,
opts,
};
}
get width() {
return this.ctx.canvas.width;
}
get height() {
return this.ctx.canvas.height;
}
clear() {
// make sure we switch screens in the beginning of the frame
if (this.nextScreen) {
const { screen: Screen, opts } = this.nextScreen;
this.currentScreen = new Screen(this, opts);
this.nextScreen = null;
}
this.ctx.clearRect(0, 0, this.width, this.height);
}
update() {
this.currentScreen.update(this);
}
render() {
this.currentScreen.render(this);
}
nextFrame() {
this.clear();
this.update();
this.render();
}
}