-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
362 lines (336 loc) · 9.33 KB
/
app.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//VARS
//audio
let woah = document.querySelector('#woah');
let yeah = document.querySelector('#yeah');
let hot = document.querySelector('#hot');
let funkyClip = document.querySelector('#funky');
let disco = document.querySelector('#disco');
let ow = document.querySelector('#ow');
let recordScratch = document.querySelector('#recordScratch');
let doit = document.querySelector('#doit');
let dance = document.querySelector('#dance');
let comp1 = document.querySelector('#one');
let comp2 = document.querySelector('#two');
let comp3 = document.querySelector('#three');
let comp4 = document.querySelector('#four');
let comp5 = document.querySelector('#five');
let speedUp = document.querySelector('#speedUp');
let funkySong = document.querySelector('#funkySong');
funkySong.loop = true;
funkySong.volume = .5;
//display round number
let round = document.querySelector('#round');
let roundNumber = 0;
round.innerText = `Round: ${roundNumber}`;
//display high score
let highScore = document.querySelector('#highScore');
let highScoreNumber = 0;
highScore.innerText = `High Score: ${highScoreNumber}`;
//store pattern and player input
let funkyArray = [];
let playerArray = [];
let playerTurn = false;
//compares funkyArray and playerArray for every input
let n = 0;
//Tests player rhythm with Date.now()
let go = 0;
let input = 0;
//timeout variable to implement double-time --future update
let fiveHundo = 500;
let fourHundo = 400;
let sixHundo = 600;
//pop-up defeat and win notifications
const reset = document.querySelector('.reset');
const nextRound = document.querySelector('.nextRound');
//player cue ('NOW!')
const playerCue = document.querySelector('.playerCue');
//start button
const start = document.querySelector('.start');
//for background style
let bgOne = document.querySelectorAll('.one');
let bgTwo = document.querySelectorAll('.two');
let bgThree = document.querySelectorAll('.three');
let bgFour = document.querySelectorAll('.four');
//enable player input if player turn is true
window.addEventListener('keydown', playerInput);
//reset game after loss
reset.addEventListener('click', defeatState);
//add round after win
nextRound.addEventListener('click', winState);
//remove start button
start.addEventListener('click', () => {
start.classList.add('hide');
showBg(true);
play();
});
//FUNCTIONS
//random between 1 and 5 and store up to roundNumber
function funkyChoice() {
let choice = Math.floor(Math.random() * 5) + 1;
let choiceString = choice.toString();
funkyArray.push(choiceString);
}
function displayFunk(funkyArray, i = 0) {
//exit display iteration
if (i === funkyArray.length) {
//audio
doit.play();
//rhythm pulse
pressIndicator();
//put player input on Timeout to avoid input during DOIT cue
setTimeout(() => {
playerTurn = true;
//initial rhythm start
go = Date.now();
}, fiveHundo);
return;
}
//cycle through display styling
let num = funkyArray[i];
document.getElementById(`${num}`).classList.add('pulse');
//audio
if (num === '1') {
comp1.play();
} else if (num === '2') {
comp2.play();
} else if (num === '3') {
comp3.play();
} else if (num === '4') {
comp4.play();
} else if (num === '5') {
comp5.play();
}
//recursive!
setTimeout(() => {
document.getElementById(`${num}`).classList.remove('pulse');
setTimeout(() => displayFunk(funkyArray, i + 1), fiveHundo);
}, fiveHundo);
}
//play
function play() {
//reset song
funkySong.load();
//for speed up - future update
// if (roundNumber === 5) {
// doubleTime();
// }
roundNumber++;
round.innerText = `Round: ${roundNumber}`;
//countdown audio
comp4.play();
setTimeout(() => comp3.play(), 600);
setTimeout(() => comp2.play(), 1000);
setTimeout(() => doit.play(), 1500);
setTimeout(() => {
funkySong.play();
//computer goes and triggers player input
funkyChoice();
displayFunk(funkyArray);
}, 2000);
}
// MASSIVE player input styling (part of playerInput below)
function playerStyle(key) {
let id = `player${key}`;
document.getElementById(id).classList.add('pulse');
setTimeout(() => document.getElementById(id).classList.remove('pulse'), fiveHundo);
if (key === '1') {
//audio
woah.play();
//animation
document.querySelector('.woah').classList.remove('hide');
document
.querySelector('.woah')
.classList.add('animate__animated', 'animate__rollOut');
setTimeout(() => {
document.querySelector('.woah').classList.add('hide');
document
.querySelector('.woah')
.classList.remove('animate__animated', 'animate__rollOut');
}, fiveHundo);
} else if (key === '2') {
yeah.play();
document.querySelector('.yeah').classList.remove('hide');
document
.querySelector('.yeah')
.classList.add('animate__animated', 'animate__flipOutX');
setTimeout(() => {
document.querySelector('.yeah').classList.add('hide');
document
.querySelector('.yeah')
.classList.remove('animate__animated', 'animate__flipOutX');
}, fiveHundo);
} else if (key === '3') {
hot.play();
document.querySelector('.hot').classList.remove('hide');
document
.querySelector('.hot')
.classList.add('animate__animated', 'animate__fadeOutBottomLeft');
setTimeout(() => {
document.querySelector('.hot').classList.add('hide');
document
.querySelector('.hot')
.classList.remove('animate__animated', 'animate__fadeOutBottomLeft');
}, fiveHundo);
} else if (key === '4') {
dance.play();
document.querySelector('.funky1').classList.remove('hide');
document
.querySelector('.funky1')
.classList.add('animate__animated', 'animate__zoomOut');
setTimeout(() => {
document.querySelector('.funky1').classList.add('hide');
document
.querySelector('.funky1')
.classList.remove('animate__animated', 'animate__zoomOut');
}, fiveHundo);
} else if (key === '5') {
disco.play();
document.querySelector('.disco').classList.remove('hide');
document
.querySelector('.disco')
.classList.add('animate__animated', 'animate__rotateOutDownRight');
setTimeout(() => {
document.querySelector('.disco').classList.add('hide');
document
.querySelector('.disco')
.classList.remove('animate__animated', 'animate__rotateOutDownRight');
}, fiveHundo);
}
}
//decides win or loss and shows .text style
function playerInput(event) {
if (playerTurn) {
//rhythm
input = Date.now();
if (input - go < fourHundo) {
document.querySelector('.early').classList.remove('hide');
setTimeout(
() => document.querySelector('.early').classList.add('hide'),
fiveHundo
);
lose();
return;
} else if (input - go > sixHundo) {
document.querySelector('.late').classList.remove('hide');
setTimeout(
() => document.querySelector('.late').classList.add('hide'),
fiveHundo
);
lose();
return;
}
go = input + fiveHundo;
//memory
const keys = ['1', '2', '3', '4', '5'];
//variable for event key
if (keys.indexOf(event.key) !== -1) {
playerArray.push(event.key);
playerStyle(event.key);
//check for loss every input
if (playerArray[n] !== funkyArray[n]) {
lose();
} else {
//win round if you get through entire funky array
n++;
if (n > funkyArray.length - 1) {
playerTurn = false;
playerCue.classList.add('hide');
playerCue.innerText = 'READY...';
setTimeout(() => {
ow.play();
nextRound.classList.remove('hide');
setTimeout(() => funkyClip.play(), fiveHundo);
setTimeout(() => funkySong.pause(), 1500);
}, fiveHundo);
return;
}
}
}
}
}
function lose() {
funkySong.pause();
recordScratch.play();
playerCue.classList.add('hide');
playerCue.innerText = 'READY...';
playerTurn = false;
showBg(false);
//button text with high score
reset.innerText = `THAT WASN\'T FUNKY...\nTRY AGAIN?\n HIGH SCORE: ${highScoreNumber}`;
//defeat state button
setTimeout(() => reset.classList.remove('hide'), 1000);
return;
}
//for reset button click
function defeatState() {
ow.play();
//reset variables
roundNumber = 0;
funkyArray = [];
playerArray = [];
//reset checker variable
n = 0;
//remove button
reset.classList.add('hide');
//reset button text
reset.innerText = '';
//show start button
start.classList.remove('hide');
}
//for between rounds
function winState() {
playerArray = [];
//update High Score
if (roundNumber > highScoreNumber) {
highScoreNumber = roundNumber;
highScore.innerText = `High Score ${highScoreNumber}`;
}
//reset checker variable
n = 0;
play();
setTimeout(() => {
nextRound.classList.add('hide');
}, 1500);
}
//for background style
function showBg(x) {
if (!x) {
bgOne.forEach((x) => {
x.style.animationName = '';
});
bgTwo.forEach((x) => {
x.style.animationName = '';
});
bgThree.forEach((x) => {
x.style.animationName = '';
});
bgFour.forEach((x) => {
x.style.animationName = '';
});
} else {
bgOne.forEach((x) => {
x.style.animationName = 'background1';
});
bgTwo.forEach((x) => {
x.style.animationName = 'background2';
});
bgThree.forEach((x) => {
x.style.animationName = 'background3';
});
bgFour.forEach((x) => {
x.style.animationName = 'background4';
});
}
}
//for rhythm guide
function pressIndicator() {
playerCue.classList.remove('hide');
setTimeout(() => (playerCue.innerText = 'NOW!'), fiveHundo);
}
//for double time
function doubleTime() {
fiveHundo = 250;
fourHundo = 150;
sixHundo = 350;
speedUp.play();
}