-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
60 lines (53 loc) · 1.69 KB
/
main.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
class GameManager {
constructor(canvas) {
this.canvas = canvas;
this.renderer = new Renderer(this.canvas);
this.themeManager = new ThemeManager();
}
startLevel(levelNumber) {
this.canvas.classList.toggle('game-stopped');
this.themeManager.startMusic();
this.game = new GameSession(
this.renderer,
this.gameSuccessHandler.bind(this),
this.gameFailureHandler.bind(this),
levelNumber
);
this.resizeCanvas();
this.mainLoop();
}
gameSuccessHandler() {
let victoryMenu = document.getElementById('victory-menu');
victoryMenu.style.removeProperty('display');
this.canvas = document.getElementById('the-game');
this.canvas.classList.toggle('game-stopped');
this.themeManager.stopMusic();
this.game.tearDown();
}
gameFailureHandler() {
let deathMenu = document.getElementById('death-menu');
deathMenu.style.removeProperty('display');
this.canvas = document.getElementById('the-game');
this.canvas.classList.toggle('game-stopped');
this.themeManager.stopMusic();
this.game.tearDown();
}
resizeCanvas() {
const devicePixelRatio = window.devicePixelRatio || 1;
this.canvas.width = window.innerWidth * devicePixelRatio;
this.canvas.height = window.innerHeight * devicePixelRatio;
this.renderer.resizeViewport();
}
mainLoop() {
if (this.game.isRunning) {
this.game.loop();
requestAnimationFrame(this.mainLoop.bind(this));
}
}
}
let g;
window.addEventListener('resize', () => g.resizeCanvas());
window.onload = function () {
localStorage.setItem('isTouch', ("ontouchstart" in document.documentElement) | 0);
g = new GameManager(document.getElementById('the-game'));
};