-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
283 lines (221 loc) · 6.64 KB
/
script.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
//Global Variables
//var pattern = [5,2, 2, 4, 3, 1, 2, 2, 4, 5, 6]; //keeps track of the pattern the button presses
var pattern = []; //keeps track of the pattern the button presses
var progress = 0 //player current status
var gamePlaying = false //tracks whether the game is currently active
var guessCounter = 0; //keeps track of where user is in the clue sequence
var resume = false; //resume the audioContext
var mistakes = 2; //tracks the mistake made by the user
var life = document.getElementById("life") //for updating the life element
var time = document.getElementById("time") //clock timer for players
var timer = 5;
var timerVar;
//varibales for sound
var tonePlaying = false //tracks whether tone is playing or not
var volume = 0.5 //must be between 0.0 and 1.0
var clueHoldTime = 800;//how long to hold each clue's light/sound
const cluePauseTime = 33; //how long to pause in between clues
const nextClueWaitTime = 900; //how long to wait before starting playback of the clue sequence
var audioFiles = ["","Electronic-Tom-3","Electronic-Kick-2","Electro-Tom","Bass-Drum-3","Clap-1","Claves"]
const ext = ".wav";
const dir ="/assets/"
var audio = new Audio();
//end of variables
//StartGame()
function startGame(){
progress = 0;
gamePlaying = true;
generatePattern();
if(!resume){
context.resume().then(() => {
console.log('Playback resumed successfully');
});
resume = true;
}
//swap the Start and End Buttons
document.getElementById("startBtn").classList.add("hidden");
document.getElementById("endBtn").classList.remove("hidden");
showChance();
playClueSequence();
}//end StartGame()
//generate random pattern
function generatePattern(){
let size = 10;
let max = 6;
for(let i=0;i<size;i++){
pattern[i] = Math.ceil(Math.random() * max);
}
}//end generatePattern()
//show the chance prompt
function showChance(){
document.getElementById("welcome").classList.add("hidden");
document.getElementById("chance_prompt").classList.remove("hidden");
life.textContent = mistakes;
time.innerHTML = timer;
}//end showChance()
//stopGame()
function stopGame(){
gamePlaying = false;
//swap the Start and End Buttons
document.getElementById("startBtn").classList.remove("hidden");
document.getElementById("endBtn").classList.add("hidden");
//resets all variables
clueHoldTime = 1000;
mistakes = 2;
clearTimer();
hideChance();
}//end StartGame()
//hide the chance
function hideChance(){
document.getElementById("welcome").classList.remove("hidden");
document.getElementById("chance_prompt").classList.add("hidden");
}//end hideChance()
// Sound Synthesis Functions
//is called from playSingleClue
function playTone(btn,len){
playAudio(btn);
tonePlaying = true
setTimeout(function(){
stopTone()
},len)
}//end of playTone
//when player pressed the box
//plays the sound
function startTone(btn){
if(!resume){
context.resume().then(() => {
console.log('Playback resumed successfully');
});
}
playAudio(btn);
}//end startTone()
//stops sound when mouse is released
function stopTone(){
g.gain.setTargetAtTime(0,context.currentTime + 0.05,0.025)
tonePlaying = false
}//end stopTone()
//gets the sounds from the assets
//and connects it to audio object
//and plays the sound
function playAudio(btn){
audio.src = dir+audioFiles[btn]+ext;
console.log("btn: ",btn);
audio.loop = false;
audio.play();
}//end playAudio()
//end of Sound Synthesis functons
/* sequence of play*/
function playSingleClue(btn){
if(gamePlaying){
lightButton(btn);
playTone(btn,clueHoldTime);
setTimeout(clearButton,clueHoldTime,btn);
}
}//end playSingleClue()
//when start button pressed
//takes random pattern and play in a sequence
//after sequnece ends a timer runs and
//players needs to catch the sound within that time
function playClueSequence(){
guessCounter = 0;
const holdTime = 800;
let delay = nextClueWaitTime; //set delay to initial wait time
for(let i=0;i<=progress;i++){ // for each clue that is revealed so far
setTimeout(playSingleClue,delay,pattern[i]) // set a timeout to play that clue
delay += clueHoldTime
delay += cluePauseTime;
}
setTimeout(startIntervalForTimer,delay);
console.log(clueHoldTime)
clueHoldTime = clueHoldTime - Math.floor(holdTime / (pattern.length)); //speeds up the sequence
}
/* end sequence of play*/
//starts the interval for the clock timer
function startIntervalForTimer(){
timerVar = setInterval(startTimer, 1000); //sets the setInterval to timerVar
//timerVar will be used later to clear interval
}//end startIntervalForTimer()
//starts the countdown of the clock timer
function startTimer(){
timer--;
time.innerHTML = timer;
if(timer == 0){
loseGame();
}
}//end startTimer()
/*win lose */
function loseGame(){
if(timer == 0){
setTimeout(function(){alert("Times up. Game Over. You lost.");},50);
}else{
setTimeout(function(){alert("Game Over. You lost.");},50);
}
stopGame();
}// end loseGame()
function winGame(){
stopGame();
alert("You won. Congratulations!")
}//end winGme()
/*end of win lose functions*/
/*Guesses Handlings*/
function guess(btn){
console.log("user guessed: " + btn);
if(!gamePlaying){
return;
}
//handling guesses
if(btn == pattern[guessCounter]){
if(guessCounter == progress){
if(progress == (pattern.length)-1){
winGame();
}else{
clearTimer();
time.innerHTML = timer;
progress++;
playClueSequence()
}
}else{
guessCounter++;
}
}else{
mistakes--;
life.innerHTML = mistakes;
if(mistakes == 0){
loseGame()
}
}
}//end guess()
/*end Guesses Handling*/
/* light and clear button*/
function lightButton(btn){
document.getElementById("button"+btn).classList.add("lit")
}
function clearButton(btn){
document.getElementById("button"+btn).classList.remove("lit")
}
/*end of light and clear button */
//clears the timer
function clearTimer(){
timer = 5;
clearInterval(timerVar);
}//end clearTimer()
//Page Initialization
// Init Sound Synthesizer
var contextClass = (window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.oAudioContext ||
window.msAudioContext);
if (contextClass) {
// Web Audio API is available.
var context = new contextClass();
var o = context.createOscillator()
var g = context.createGain()
g.connect(context.destination)
g.gain.setValueAtTime(0,context.currentTime)
o.connect(g)
o.start(0)
} else {
// Web Audio API is not available. Ask the user to use a supported browser.
console.log("Web Audio API is not available. ")
}