-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
349 lines (196 loc) · 7.83 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
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
const groceryList = {
fruits: ['apple', 'banana', 'orange', 'grapes'],
vegetables: ['carrot', 'tomato', 'onion', 'potato'],
drinks: ['soda', 'juice', 'milk', 'bubble water'],
meat: ['beef', 'chicken', 'pork', 'lamb'],
sweets: ['candy', 'chocolate', 'ice cream', 'cake']
}
let randomItems = [];
function fruitGenerator() {
let randomIndex= Math.floor(Math.random() * groceryList.fruits.length);
let randomFruit = groceryList.fruits[randomIndex];
randomItems.push(randomFruit);
return randomFruit;
}
function vegetablesGenerator() {
let randomIndex = Math.floor(Math.random() * groceryList.vegetables.length);
let randomVegetable = groceryList.vegetables[randomIndex];
randomItems.push(randomVegetable);
return randomVegetable;
}
function drinksGenerator() {
let randomIndex = Math.floor(Math.random() * groceryList.drinks.length);
let randomDrink = groceryList.drinks[randomIndex];
randomItems.push(randomDrink);
return randomDrink;
}
function meatGenerator() {
let randomIndex = Math.floor(Math.random() * groceryList.meat.length);
let randomMeat = groceryList.meat[randomIndex];
randomItems.push(randomMeat);
return randomMeat;
}
function sweetsGenerator() {
let randomIndex = Math.floor(Math.random() * groceryList.sweets.length);
let randomSweet = groceryList.sweets[randomIndex];
randomItems.push(randomSweet);
return randomSweet;
}
let goButton = document.querySelector('.goButton')
let againButton = document.querySelector('.againButton')
let continueButton = document.querySelector('.continueButton')
againButton.style.display = 'none';
continueButton.style.display = 'none';
//TEXT CONTAINER ETC
let textContainer = document.querySelector('.textContainer');
let displayContainer = document.querySelector('.displayContainer');
let clickedItems = [];
let clickCount = 0;
let level = document.querySelector('.level');
let levelCount = 1;
const progressBar = document.querySelector('.timerInner');
let timeIDs = [];
function countDown(duration) {
const timerElement = document.querySelector('.timerInner');
for (let i = 0; i <= duration; i++) {
let timeID = setTimeout(() => {
let progressWidth = ((duration - i) / duration) * 100;
timerElement.style.width = progressWidth + '%';
if(i=== duration) {
textContainer.innerHTML = 'Time is up!';
}
}, i * 1200);
timeIDs.push(timeID);
}
}
let achievementAudio = new Audio('./resurssit/music/achievement.wav');
//levelCount++ and winning condition
function checkGameCompletion(){
if(randomItems.every(item => clickedItems.includes(item))) {
levelCount++;
level.innerHTML = levelCount;
if(levelCount === 4) {
displayContainer.innerHTML = 'You are the ultimate grocery shopper!'; //does not work
continueButton.style.display = 'none';
levelCount = 'You won the game!'; // works
achievementAudio.pause(); //works
achievementAudio.src = '';
achievementAudio.currentTime = 0;
let winAudio = new Audio('./resurssit/music/winfretless.ogg'); //works
winAudio.play();
}
}
}
//USE this on the other project!
function createButtons() {
textContainer.innerHTML = ''; //clears the buttonareas, so they wont duplicate
//timertest
countDown(10);
let allList = Object.values(groceryList);
if (levelCount === 1) {
allList = allList.filter(category => category !== groceryList.meat && category !== groceryList.sweets);
}else if(levelCount === 2) {
allList = allList.filter(category => category !== groceryList.sweets);
}
//gets every category and creates a button for each item
allList.forEach(category => {
category.forEach(item => {
let newBtn = document.createElement('button');
newBtn.className = 'itemButton';
newBtn.innerText= `${item}`;
document.querySelector('.textContainer').appendChild(newBtn);
newBtn.addEventListener('click', function() {
displayContainer.innerHTML += `${this.innerText}` + ', ';
clickedItems.push(this.innerText);
clickCount++;
let clickAudio = new Audio('./resurssit/music/click.wav');
clickAudio.play();
if(clickCount === randomItems.length) {
checkGameCompletion();
}
if(randomItems.every(item => clickedItems.includes(item))) {
displayContainer.innerHTML = 'You got everything!';
level.textContent = levelCount;
continueButton.style.display = 'block';
goButton.style.display = 'none';
achievementAudio.play();
const timerElement = document.querySelector('.timerInner');
timerElement.style.width = '100%';
timeIDs.forEach(timeID => clearTimeout(timeID));
}
else if(clickCount === randomItems.length) {
displayContainer.innerHTML = 'You missed some items!';
let failAudio = new Audio('./resurssit/music/negative.mp3');
failAudio.play();
const timerElement = document.querySelector('.timerInner');
timerElement.style.width = '100%';
timeIDs.forEach(timeID => clearTimeout(timeID));
}
else {
displayContainer.innerHTML += '';
}
});
});
});
return newBtn;
}
//why inside upper function? well, it works
function startOver() {
goButton.style.display = 'block';
againButton.style.display = 'none';
continueButton.style.display = 'none';
textContainer.innerHTML = '';
displayContainer.innerHTML = '';
clickedItems = [];
clickCount = 0;
randomItems = [];
levelCount = 1;
level.textContent = levelCount;
const timerElement = document.querySelector('.timerInner');
timerElement.style.width = '100%';
goButton.addEventListener('click', yourList); {
}
}
againButton.addEventListener('click', startOver); {
}
//Your randomly generated list
function yourList() {
randomItems = []; //clears the list
clickedItems = []; //clears the clicked items
clickCount = 0; //clears the click count
continueButton.removeEventListener('click', yourList);
goButton.removeEventListener('click', yourList);
goButton.addEventListener('click', yourList);
continueButton.addEventListener('click', yourList);
againButton.style.display = 'block';
goButton.style.display = 'none';
fruitGenerator();
vegetablesGenerator();
drinksGenerator();
if(levelCount >= 2) {
meatGenerator();
}
if(levelCount >= 3) {
sweetsGenerator();
}
//textContainer.innerHTML = 'Your list: <br><br>'
textContainer.innerHTML = '<br><br><br>'
textContainer.innerHTML += randomItems.join('<br>');
//textContainer.style.backgroundColor = 'white';
// works textContainer.style.backgroundImage = "url('./resurssit/kuvat/myList.png')";
textContainer.style.cssText = " background: no-repeat center/cover url('./resurssit/kuvat/myList.png'); background-size: 140%; background-position: -15px -70px;";
displayContainer.innerHTML = '';
setTimeout(() => {
textContainer.innerHTML = '';
textContainer.style.backgroundColor = ''; //modify the background
textContainer.style.cssText = '';
}, 1800);
setTimeout(() => {
textContainer.innerHTML = createButtons();
}, 1900);
goButton.addEventListener('click', yourList);
continueButton.addEventListener('click', yourList);
};
goButton.addEventListener('click', yourList);
continueButton.addEventListener('click', yourList);
//create buttons of every GroceryList key, test