-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
172 lines (143 loc) · 4.12 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
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
// dom declaration
const startButton = document.getElementById("start");
const levelText = document.getElementById("level");
const modal = document.getElementById("modal_overlay");
const modalButton = document.getElementById("modal_button");
const scoreText = document.getElementById("score");
// arrayMain for referencing the random set of color
// arrayInGame for set of colors in every level always clear every level
const arrayBox = ["box_green", "box_yellow", "box_red", "box_blue"];
const arrayMain = [];
const arrayInGame = [];
let highestScore = 0;
let currentScore = 0;
function resetGame() {
arrayMain.length = arrayInGame.length = currentScore = 0;
levelText.innerText = "Level " + 1;
startButton.innerText = "Start";
disableBoxButton(true, true);
disbleStartButton(false);
}
//disble a button box logic or UI style
function disableBoxButton(isDisable, isDisabledUI) {
for (const element of document.getElementsByClassName("box")) {
element.disabled = isDisable;
if (isDisabledUI) element.classList.add("disable");
else element.classList.remove("disable");
}
}
function disbleStartButton(enabler) {
startButton.disabled = enabler;
if (enabler) startButton.classList.add("disable");
else startButton.classList.remove("disable");
}
// add glow effect
function activeBox(boxID) {
const box = document.getElementById(boxID);
box.classList.add("box_press");
playSound("coin");
setTimeout(() => {
box.classList.remove("box_press");
}, 400);
}
function addColorRandom() {
arrayMain.push(arrayBox[Math.round(Math.random() * 3)]);
console.log(arrayMain);
}
function playColorSet() {
startButton.innerText = "Wait";
addColorRandom();
//use for playing the next color set
let index = 0;
const glowEach = setInterval(() => {
activeBox(arrayMain[index++]);
if (index === arrayMain.length) {
startButton.innerText = "Guess";
disableBoxButton(false, false);
clearInterval(glowEach);
}
}, 600);
}
function checkColorSet() {
//lose
if (!isArrayStartsWith(arrayMain, arrayInGame)) loseLevel();
//win
else if (arrayInGame.length === arrayMain.length && arrayInGame.length !== 0) winLevel();
}
function loseLevel() {
playSound("lose");
showLostModal(true);
resetGame();
}
//reset ingame array and add one color randomly
function winLevel() {
arrayInGame.length = 0;
levelText.innerText = `Current: ${(arrayMain.length + 1).toString()}`;
startButton.innerText = "Start";
disableBoxButton(true);
playSound("win");
setTimeout(() => {
disableBoxButton(true, true);
disbleStartButton(false);
}, 1000);
currentScore++;
if (currentScore > highestScore || highestScore === 0) {
scoreText.innerText = `highest : ${++highestScore}`;
}
}
//utils
function playSound(soundName) {
switch (soundName) {
case "coin":
new Audio("https://pangilinanervin22.github.io/simon/res/coin_sound.wav").play();
break;
case "win":
new Audio("https://pangilinanervin22.github.io/simon/res/win.wav").play();
break;
case "lose":
new Audio("https://pangilinanervin22.github.io/simon/res/lose.wav").play();
break;
case "try":
new Audio("https://pangilinanervin22.github.io/simon/res/try.wav").play();
break;
case "start":
new Audio("https://pangilinanervin22.github.io/simon/res/start.wav").play();
break;
default:
}
}
function showLostModal(enabler) {
if (enabler) {
playSound("lose");
modal.classList.remove("hide");
} else {
modal.classList.add("hide");
}
}
function isArrayStartsWith(first, second) {
const firstArray = [...first];
const secondArray = [...second];
for (let index = 0; index < secondArray.length; index++)
if (firstArray[index] !== secondArray[index]) return false;
return true;
}
//function clicking all box
for (const item of document.getElementsByClassName("box"))
item.addEventListener("click", () => {
const id = item.getAttribute("id");
arrayInGame.push(id);
checkColorSet();
playSound("coin");
});
modalButton.addEventListener("click", () => {
playSound("try");
showLostModal(false);
});
startButton.addEventListener("click", () => {
disbleStartButton(true);
disableBoxButton(true, false);
playColorSet();
playSound("start");
});
// starting point
resetGame();