-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
371 lines (309 loc) · 11.7 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
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
363
364
365
366
367
368
369
370
const layer1 = document.querySelector('.layer1')
const layer2 = document.querySelector('.layer2')
const layer3 = document.querySelector('.layer3')
const outer = document.querySelector('._outer')
const inner = document.querySelector('._inner')
const percentage = document.querySelector('span')
const processing = document.querySelector('.processing')
const p1 = document.querySelector('.p1')
const p2 = document.querySelector('.p2')
const p3 = document.querySelector('.p3')
let [score, points] = [...document.querySelectorAll(".scoreNm")]
const countdownEl = document.querySelector('#countdown');
let board = document.querySelector(".outer");
let table = document.querySelector(".table");
let Game0N = false
let TIME = 5 * 60
let onTable = 0
//this function triggers when you click play
function StartGame() {
if (parseInt(points.innerHTML) <= 0) { // if no spades left
score.innerHTML = 'No spades left '
board.style.display = "flex";
clearInterval(idItr);
} else if (onTable == 0) { // if no spades on table
// blinking border
table.style.border = '5px dashed red'
setTimeout(() => {
table.style.border = '5px dashed transparent'
}, 500)
setTimeout(() => {
table.style.border = '5px dashed red'
}, 1000)
setTimeout(() => {
table.style.border = '5px dashed transparent'
}, 1500)
} else {
Game0N = true
Showball();
// setTimeout(Showball,);
setTimeout(shuffling, 7000);
}
}
//this function lifts the thimbles
function thimbleup(x) {
x.classList.add("thimbleup");
}
//this function puts the thimble down
function thimbledown(x) {
x.classList.remove("thimbleup");
}
//this function selects one thimble at random and positions the ball
// under it and lifts it at the beginning
let idItr
let timeLeft = TIME ; // 5 minutes in seconds
function Showball() {
document.getElementById("Playbutton").style.pointerEvents = "none";
let rand = getRandNum();
let thimb = document.getElementById(`Cup${rand}`);
document
.getElementById("thimble_ball")
.setAttribute("Class", `thimble_ball_position-${rand}`);
thimb.classList.add("thimbleup");
setTimeout(function () {
thimb.classList.remove("thimbleup");
}, 4000);
setTimeout(function () {
document
.getElementById("thimble_ball")
.classList.remove(`thimble_ball_position-${rand}`);
}, 4500);
// Start Timer
if (countdownEl.innerHTML == '05:00') {
idItr = setInterval(() => {
if (timeLeft <= 0 && !Game0N) {
if (!Game0N) {
setTimeout(() => {
clearInterval(idItr);
board.style.display = "flex";
score.innerHTML = 'Time\'s up! You Won ' + points.innerHTML + ' '
clearInterval(idItr);
}, 2000)
}
} else {
if (timeLeft >= 0) {
let minutes = Math.floor(timeLeft / 60);
let seconds = timeLeft % 60;
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
countdownEl.innerHTML = `${minutes}:${seconds}`;
timeLeft--;
}
}
}, 1000);
}
}
// This function resets the class of all the thimbles to default
function resetthimbclass() {
document
.getElementById("Cup0")
.setAttribute("Class", "sewing_thimble thimble-0");
document
.getElementById("Cup1")
.setAttribute("Class", "sewing_thimble thimble-1");
document
.getElementById("Cup2")
.setAttribute("Class", "sewing_thimble thimble-2");
}
//this functions picks a random integer from 0-2
function getRandNum() {
let random = Math.floor(Math.random() * 3);
return random;
}
//this functions runs pickrandcups function after every 0.5secons
function shuffling() {
mix = setInterval(PickRandCups, 500);
}
let mix;
let shufflecounter = 0;
//this function interchanges the classes of two thimbles
function PickRandCups() {
let Cone = getRandNum();
let Ctwo = getRandNum();
if (Cone != Ctwo) {
let Cupone = document.getElementById(`Cup${Cone}`);
let Cuptwo = document.getElementById(`Cup${Ctwo}`);
let CuponeClass = Cupone.getAttribute("class");
let CuptwoClass = Cuptwo.getAttribute("class");
Cupone.setAttribute("Class", CuptwoClass);
Cuptwo.setAttribute("Class", CuponeClass);
shufflecounter = shufflecounter + 1;
if (shufflecounter > 15) {
clearInterval(mix);
resetthimbclass();
removedisabled();
shufflecounter = 0;
// setTimeout(resetthimbclass,7500);
}
} else {
PickRandCups();
}
}
//this function removes the disabled attribute from all thimbles
function removedisabled() {
let removedis = document.getElementsByClassName("sewing_thimble");
for (var i = 0; i < removedis.length; i++) {
removedis[i].removeAttribute("disabled");
}
}
// this function adds the disabled attribute from all thimbles
function adddisabled() {
let addis = document.getElementsByClassName("sewing_thimble");
for (var i = 0; i < addis.length; i++) {
addis[i].setAttribute("disabled", "disabled");
}
}
// This function triggers when you click on a thimble
function selectthimble(x) {
adddisabled();
let rand = getRandNum();
let winningthimble = document.getElementById(`Cup${rand}`);
let selectedthimble = document.getElementById(`${x}`);
let ballpos = document.getElementById("thimble_ball");
ballpos.setAttribute("Class", `thimble_ball_position-${rand}`); //set the ball position tunder the selected thimble
selectedthimble.classList.add("thimbleup"); //lift the selected thimble up
setTimeout(function () {
if (winningthimble != selectedthimble) {
setTimeout(function () {
selectedthimble.classList.remove("thimbleup");
}, 2000); //bring the selected thimble down after 2 secs
setTimeout(function () {
winningthimble.classList.add("thimbleup");
}, 2500); //bring the winning thimble down after 2.5secs
setTimeout(function () {
winningthimble.classList.remove("thimbleup");
}, 4000); //bring the winning thimble down after 5secs
// subtract points
points.innerHTML = parseInt(points.innerHTML) - onTable;
points.style.color = "crimson";
setTimeout(function () {
points.style.color = "white";
}, 1000)
onTable = 0;
let sum = document.querySelector(".sumNm");
sum.innerHTML = 0
document.getElementById("Playbutton").style.pointerEvents = "all"; //make the play button clickable again
} else if ((winningthimble = selectedthimble)) {
// add points
points.innerHTML = parseInt(points.innerHTML) + onTable;
points.style.color = "#63d673";
setTimeout(function () {
points.style.color = "white";
}, 1000)
if (points.innerHTML <= 0) {
board.style.display = "flex";
score.innerHTML = 'No spades left '
clearInterval(idItr);
}
onTable = 0;
let sum = document.querySelector(".sumNm");
sum.innerHTML = 0
setTimeout(function () {
selectedthimble.classList.remove("thimbleup");
}, 2000); //bring the selected thimble down after 2 secs
setTimeout(function () {
winningthimble.classList.remove("thimbleup");
}, 2500); //bring the winning thimble down after 2.5secs
document.getElementById("Playbutton").style.pointerEvents = "all"; //make the play button clickable again
}
Game0N = false
}, 2000);
}
function setCup(id) {
let elements = document.querySelectorAll(".sewing_thimble");
for (let element of elements) {
element.style.background = `url("Img/c${id}.png") no-repeat fixed center`;
element.style.backgroundSize = `contain`;
}
}
function setBall(id) {
let element = document.querySelector("#thimble_ball");
element.style.background = `url("Img/b${id}.png") no-repeat center`;
element.style.backgroundSize = '2em 2em';
}
function setOnTable(prst) {
if (parseInt(points.innerHTML) > 0) {
let sum = document.querySelector(".sumNm");
onTable = Math.round(points.innerHTML * prst);
sum.innerHTML = onTable;
} else {
board.style.display = "flex";
score.innerHTML = 'No spades left '
clearInterval(idItr);
}
}
function init() {
setOnTable(0);
points.innerHTML = 200;
board.style.display = "none";
countdownEl.innerHTML = '05:00'
// Clear all intervals and timeouts
InID = setInterval(() => {}, 10)
TiID = setTimeout(() => {}, 10)
while (InID--) clearInterval(InID)
while (TiID--) clearTimeout(TiID)
timeLeft = TIME
}
///\ LOAD GAME /\\\
let count = 0;
let GAME0N = false
// Event listener for loading the game
inner.addEventListener('click', loader = () => {
// Remove event listener to ensure only one click is processed
inner.removeEventListener('click', loader)
GAME0N = true
/* Process board smooth appearance */
percentage.textContent = '0%'
// Smooth appearance animation of loading elements
gsap.to(p1, { opacity: 0.5, duration: 1 });
gsap.to(p2, { opacity: 0.5, duration: 2 });
gsap.to(p3, { opacity: 0.5, duration: 3 });
// Start loading animation and transition when loading
setTimeout(() => {
/* Percent Loader */
const id = setInterval(() => {
if (GAME0N) {
if (count == 99) {
// Load reaches 100%, animate out the loading screen
setTimeout(() => {
percentage.textContent = '100%'
outer.classList.remove('active-loader')
outer.classList.add('active-loader2')
layer3.style.top = '-100%'
layer2.style.top = '-100%'
layer1.style.top = '-100%'
}, 1500)
// Hide loading elements and clear interval
setTimeout(() => {
layer3.style.display = 'none'
layer2.style.display = 'none'
layer1.style.display = 'none'
clearInterval(id)
if (window.screen.width < 1200 || window.screen.height < 500) {
alert('This game is not supported on mobile devices')
}
}, 3000)
}
else {
count++
percentage.textContent = count + '%'
outer.classList.add('active-loader')
}
}
}, 60) // Loading speed
/* Process appear one by one */
const ls = [p1, p2, p3];
let i = 1;
gsap.to(p1, { opacity: 1, scale: '1.5', duration: 0.5 });
// Sequential animation for loading elements
const _id = setInterval(() => {
if (i < ls.length) {
gsap.to(ls[i], { opacity: 1, scale: '1.5', duration: 0.5 });
gsap.to(ls[i - 1], { opacity: 0.5, scale: '1', duration: 0.5 });
i++;
} else {
clearInterval(_id);
}
}, 2500);
}, 2000)
})