-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame.js
72 lines (64 loc) · 2.09 KB
/
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
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
import { MenuScene } from "./scenes/menu.js";
import { PauseScene } from "./scenes/pause.js";
import { GameOverScene } from "./scenes/gameover.js";
const fpsRatio = ms => { return Math.min(ms / (1000 / 60), 2) }
let then;
export class Game {
constructor(ctx) {
this.paused = false;
this.game_over = false;
then = Date.now();
this.ctx = ctx;
this.allowUserInput = true;
this.timer = [0.0, function(){}, this];
this.scene = new MenuScene(this);
this.prevScene = this.scene;
}
update(keyboard, mouse) {
let now = Date.now();
let delta = now - then;
let ratio = fpsRatio(delta);
if (this.timer[0] > 0.0) {
this.timer[0] -= ratio;
} else if (this.timer[0] < 0.0) {
this.timer[0] = 0.0;
this.timer[1](this.timer[2]);
}
if (!this.allowUserInput) {
mouse.leftClick = false;
keyboard.p = false;
}
this.scene.update(ratio, keyboard, mouse);
then = Date.now();
if (!this.game_over && keyboard.p) {this.paused = !this.paused}
if (this.paused && this.scene.isPausedScene !== true) {
this.changeScene(new PauseScene(this, this.ctx));
} else if (!this.paused && this.scene.isPausedScene) {
let scene = this.prevScene;
this.changeScene(scene);
}
}
draw(ctx, drawSprite) {
this.scene.draw(ctx, drawSprite);
}
setTimer(frames, func, self) {
this.timer = [frames, func, self]
}
changeScene(scene) {
let prevScene = this.scene;
this.prevScene = prevScene;
this.allowUserInput = false;
this.timer = [10.0, function(self) {self.allowUserInput=true}, this];
scene.isPausedScene = this.paused;
this.scene = scene;
}
darkenCanvas(ctx) {
ctx.fillStyle = "rgba(0, 0, 0, 0.5)";
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}
gameOver() {
if (!this.game_over) {
this.changeScene(new GameOverScene(this));
}
}
}