-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
126 lines (108 loc) · 3.52 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
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
import { Engine } from "./engine.js";
const canvas = document.getElementById("myCanvas");
window.addEventListener("DOMContentLoaded", () => {
// initialize game
const engine = new Engine(60, canvas);
engine.start("loading-screen");
// audio files
let backgroundMusic = new Audio();
backgroundMusic.src =
"https://raw.githubusercontent.com/eric2523/survive-the-nights/main/sound/2020-03-22_-_A_Bit_Of_Hope_-_David_Fesliyan.mp3";
backgroundMusic.loop = true;
backgroundMusic.volume = 0.15;
const muteSound = document.getElementById("mute-sound");
// let playSound = false;
muteSound.addEventListener("click", () => {
if (engine.game !== null) {
if (backgroundMusic.paused) {
muteSound.children[0].textContent = "Mute";
engine.game.playSound = true;
backgroundMusic.play();
} else {
muteSound.children[0].textContent = "Unmute";
engine.game.playSound = false;
backgroundMusic.pause();
}
}
});
const startGame = document.getElementById("start-game");
const home = document.getElementById("home");
const pause = document.getElementById("pause");
const play = document.getElementById("play");
const restart = document.getElementById("restart");
const nextLevel = document.getElementById("next-level-btn");
const playAgain = document.getElementById("play-again-btn");
const loadingScreen = document.getElementsByClassName("loading-screen")[0];
const handlePause = () => {
if (engine.running) {
engine.running = false;
engine.stop();
backgroundMusic.pause();
}
}
const handlePlay = () => {
if (!engine.running) {
engine.running = true;
engine.run();
if (engine.game.playSound) {
backgroundMusic.play();
}
}
}
playAgain.addEventListener("click", () => {
playAgain.classList.add("hide");
let prevAudioFlag = engine.game.playSound
engine.restartGame();
engine.running = true;
engine.start();
engine.game.playSound = prevAudioFlag
});
nextLevel.addEventListener("click", () => {
nextLevel.classList.add("hide");
engine.game.createLevel(engine.game.level);
engine.run();
});
startGame.addEventListener("click", () => {
loadingScreen.classList.add("hide");
let prevAudioFlag = true;
if (engine.game) {
prevAudioFlag = engine.game.playSound;
}
engine.restartGame();
engine.running = true;
engine.start();
engine.game.playSound = prevAudioFlag;
if (engine.game.playSound) {
muteSound.children[0].textContent = "Mute";
backgroundMusic.play();
}
pause.addEventListener("click", handlePause);
play.addEventListener("click", handlePlay);
});
home.addEventListener("click", () => {
nextLevel.classList.add("hide");
playAgain.classList.add("hide");
loadingScreen.classList.remove("hide");
engine.stop();
engine.running = true;
engine.start("loading-screen");
play.removeEventListener("click", handlePlay)
pause.removeEventListener("click", handlePause)
});
restart.addEventListener("click", () => {
nextLevel.classList.add("hide");
playAgain.classList.add("hide");
loadingScreen.classList.add("hide");
engine.restartGame();
engine.running = true;
engine.start();
play.addEventListener("click", handlePlay)
pause.addEventListener("click", handlePause)
});
window.addEventListener("keydown", () => {
engine.controls.handleKeyDown();
});
window.addEventListener("keyup", () => {
engine.controls.handleKeyUp();
});
});