-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
284 lines (236 loc) · 11.2 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
281
282
283
284
// Global Variables
// import {
// CLUE_HOLD_TIME,
// CLUE_PAUSE_TIME,
// NEXT_CLUE_WAIT_TIME,
// PATTERN,
// JASMINE_FLOWER,
// JASMINE_FLOWER_CONT
// } from "constants.mjs";
//const parse = require('constants');
// import {Context} from "context.mjs";
const CLUE_HOLD_TIME = 500; //how long to hold each clue's light/sound
const CLUE_PAUSE_TIME = 200; //how long to pause in between clues
const NEXT_CLUE_WAIT_TIME = 1000; //how long to wait before starting playback of the clue sequence
const PATTERN = [3, 3, 5, 6, 8, 8, 6, 5, 5, 6, 5,5, 5, 5, 3, 5, 6, 6, 5, 3, 2, 3, 5, 3, 2, 1, 1, 2, 1];
var randome_pattern;
const COLORS = {
brown: "#481e1c",
beige: "#F3EBE0",
yellow: "#EAD3A7",
green: "#AFBC8F"
}
var progress = 0;
var gamePlaying = false;
// Sound
var tonePlaying = false;
var volume = 0.5;
// Game Play
var guessCounter = 0;
var speed = 1;
var strikeCount = 0;
var timeInterval;
function startGame() {
buttonSetUp(PATTERN);
// initialize game variables
progress = 0;
gamePlaying = true; //function initializeGame()
strikeCount = 0;
speed = 1;
// swap the Start and Stop buttons
document.getElementById("startBtn").classList.add("hidden");
document.getElementById("stopBtn").classList.remove("hidden");
// display the current speed:
document.getElementById("speedDisplay").classList.remove("hidden");
// reset timer
document.getElementById("countDown").innerHTML = `${0}:${0}`;
// clues
playClueSequence();
}
function stopGame() {
gamePlaying = false;
// swap the Start and Stop buttons
document.getElementById("startBtn").classList.remove("hidden");
document.getElementById("stopBtn").classList.add("hidden");
// reset countdown
resetTime();
}
/***************************Clue to Button Functions****************************/
//helper functions to change button visuals
//.lit is when button is not clicked but light up
function lightButton(btn) {
document.getElementById("button" + btn).classList.add("lit");
}
function clearButton(btn) {
document.getElementById("button" + btn).classList.remove("lit");
}
function playSingleClue(btn) {
if (gamePlaying) {
lightButton(btn);
playTone(btn, CLUE_HOLD_TIME / speed);
setTimeout(clearButton, CLUE_HOLD_TIME / speed, btn);
}
}
function playClueSequence() {
console.log("play sequence with progress: " + progress);
console.log("play sequence with speed: " + speed);
guessCounter = 0; // guessCounter needs to be initialized every time clue plays
context.resume();
let delay = NEXT_CLUE_WAIT_TIME / speed; //set delay to initial wait time
for (let i = 0; i <= progress; i++) {
// for each clue that is revealed so far
console.log("play single clue: " + PATTERN[i] + " in " + delay + "ms");
setTimeout(playSingleClue, delay, PATTERN[i]); // set a timeout to play that clue
delay += CLUE_HOLD_TIME / speed;
delay += CLUE_PAUSE_TIME / speed;
}
let time = {
milisecond: 5000
}
timeInterval = setInterval(function() {tickingClock(time)}, 100);
}
function buttonSetUp(song) {
for (var i = 1; i < 8; ++i) {
document.getElementById("button" + i).classList.add("hidden");
}
let uniqueButtons = [...new Set(song)]; // works
uniqueButtons.sort();
for(var item = 0; item < uniqueButtons.length; ++item){
console.log(uniqueButtons[item]);
document.getElementById("button" + uniqueButtons[item]).classList.remove("hidden");
if(item%2 == 0){
document.getElementById("button" + uniqueButtons[item]).style.background = "url('https://cdn.glitch.global/e8c92ccb-e3a1-45c5-a4c2-48e4d7eae45b/green-jasmine?v=1650665969414')";
}else{
document.getElementById("button" + uniqueButtons[item]).style.background = "url('https://cdn.glitch.global/e8c92ccb-e3a1-45c5-a4c2-48e4d7eae45b/orig-jasmine.png?v=1650670560449')";
}
}
// uniqueButtons.forEach(item => document.getElementById("button" + item).classList.remove("hidden"));
}
/***************************Speed Functions****************************/
// isSpeedingUp: boolean variable that holds if speed is becoming faster or slower
function speedChange(isSpeedingUp) {
if (isSpeedingUp) {
speed += 0.1;
} else {
speed -= 0.1;
}
}
/***************************User Interaction Functions****************************/
function loseGame() {
resetTime();
stopGame();
alert("Game Over. You lost.");
}
function winGame() {
stopGame();
alert("Game Over. You won!!");
}
function guess(btn) {
console.log("user guessed: " + btn);
//stop count down timer
clearInterval(timeInterval);
if (!gamePlaying) {
// alert("Press Start button to start game!");
return;
}
// Is guess correct?
else if (btn == PATTERN[guessCounter]) {
// Is turn over?
if (guessCounter == progress) {
// Is this the last turn?
if (progress == PATTERN.length - 1) {
winGame();
} else {
speedChange(true); //speed up
progress++; // increment progress
playClueSequence(); // play next clue sequence
}
} else {
guessCounter++; // increment guessCounter
}
} else {
// Guess is incorrect --> increment strikeCount
incorrectGuess();
}
}
function incorrectGuess(){
beep();
if (strikeCount < 2) {
strikeCount++;
alert(`Guessed wrong! Try again. Strike: ${strikeCount}`);
playClueSequence();
} else {
loseGame();
}
}
/***************************Ticking Clock Functions****************************/
function tickingClock(time){
if(time.milisecond <= 0){
resetTime();
incorrectGuess();
}
var second = parseInt(time.milisecond / 1000);
let milisec = time.milisecond % 1000;
document.getElementById("countDown").innerHTML = `${second}:${milisec}`;
time.milisecond = time.milisecond - 100;
}
function resetTime(){
document.getElementById("countDown").innerHTML = `${0}:${0}`;
clearInterval(timeInterval);
}
/***************************Sound Synthesis Functions****************************/
// Determines pitch
const freqMap = {
1: 261.6,
2: 293.7,
3: 329.6,
4: 349.2,
5: 392.1,
6: 440.0,
7: 493.9,
8: 523.3,
};
// Page Initialization
// Init Sound Synthesizer
var AudioContext = window.AudioContext || window.webkitAudioContext;
var context = new AudioContext();
var o = context.createOscillator();
var g = context.createGain();
g.connect(context.destination);
g.gain.setValueAtTime(0, context.currentTime);
o.connect(g);
o.start(0);
function playTone(btn, len) {
o.frequency.value = freqMap[btn];
g.gain.setTargetAtTime(volume, context.currentTime + 0.05, 0.025);
context.resume();
tonePlaying = true;
setTimeout(function () {
stopTone();
}, len);
}
function startTone(btn) {
if (!tonePlaying) {
context.resume();
o.frequency.value = freqMap[btn];
g.gain.setTargetAtTime(volume, context.currentTime + 0.05, 0.025);
context.resume();
tonePlaying = true;
}
}
function stopTone() {
g.gain.setTargetAtTime(0, context.currentTime + 0.05, 0.025);
tonePlaying = false;
}
function beep() {
var snd = new Audio(
"data:audio/wav;base64,//uQRAAAAWMSLwUIYAAsYkXgoQwAEaYLWfkWgAI0wWs/ItAAAGDgYtAgAyN+QWaAAihwMWm4G8QQRDiMcCBcH3Cc+CDv/7xA4Tvh9Rz/y8QADBwMWgQAZG/ILNAARQ4GLTcDeIIIhxGOBAuD7hOfBB3/94gcJ3w+o5/5eIAIAAAVwWgQAVQ2ORaIQwEMAJiDg95G4nQL7mQVWI6GwRcfsZAcsKkJvxgxEjzFUgfHoSQ9Qq7KNwqHwuB13MA4a1q/DmBrHgPcmjiGoh//EwC5nGPEmS4RcfkVKOhJf+WOgoxJclFz3kgn//dBA+ya1GhurNn8zb//9NNutNuhz31f////9vt///z+IdAEAAAK4LQIAKobHItEIYCGAExBwe8jcToF9zIKrEdDYIuP2MgOWFSE34wYiR5iqQPj0JIeoVdlG4VD4XA67mAcNa1fhzA1jwHuTRxDUQ//iYBczjHiTJcIuPyKlHQkv/LHQUYkuSi57yQT//uggfZNajQ3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bf/pD6ElLqSX+rUmOQNpJFa/r+sa4e/pBlAABoAAAAA3CUgShLdGIxsY7AUABPRrgCABdDuQ5GC7DqPQCgbbJUAoRSUj+NIEig0YfyWUho1VBBBA//uQZB4ABZx5zfMakeAAAAmwAAAAF5F3P0w9GtAAACfAAAAAwLhMDmAYWMgVEG1U0FIGCBgXBXAtfMH10000EEEEEECUBYln03TTTdNBDZopopYvrTTdNa325mImNg3TTPV9q3pmY0xoO6bv3r00y+IDGid/9aaaZTGMuj9mpu9Mpio1dXrr5HERTZSmqU36A3CumzN/9Robv/Xx4v9ijkSRSNLQhAWumap82WRSBUqXStV/YcS+XVLnSS+WLDroqArFkMEsAS+eWmrUzrO0oEmE40RlMZ5+ODIkAyKAGUwZ3mVKmcamcJnMW26MRPgUw6j+LkhyHGVGYjSUUKNpuJUQoOIAyDvEyG8S5yfK6dhZc0Tx1KI/gviKL6qvvFs1+bWtaz58uUNnryq6kt5RzOCkPWlVqVX2a/EEBUdU1KrXLf40GoiiFXK///qpoiDXrOgqDR38JB0bw7SoL+ZB9o1RCkQjQ2CBYZKd/+VJxZRRZlqSkKiws0WFxUyCwsKiMy7hUVFhIaCrNQsKkTIsLivwKKigsj8XYlwt/WKi2N4d//uQRCSAAjURNIHpMZBGYiaQPSYyAAABLAAAAAAAACWAAAAApUF/Mg+0aohSIRobBAsMlO//Kk4soosy1JSFRYWaLC4qZBYWFRGZdwqKiwkNBVmoWFSJkWFxX4FFRQWR+LsS4W/rFRb/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////VEFHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAU291bmRib3kuZGUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjAwNGh0dHA6Ly93d3cuc291bmRib3kuZGUAAAAAAAAAACU="
);
snd.play();
}
/***************************Test Create Button Functions****************************/
function createGameArenaBtn(btnName){
let btn = document.createElement("button");
btn.name = btnName;
document.gameButtonArea.appendChild(btn);
}