-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
449 lines (435 loc) · 10.1 KB
/
app.js
File metadata and controls
449 lines (435 loc) · 10.1 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
var canvasdom = document.querySelector("canvas");
var clockdom =document.getElementById('clock');
var pause=document.getElementById('pausebtn');
var restart=document.getElementById('restartbtn');
var scoredom=document.getElementById('score');
var hscoredom=document.getElementById('hscore');
var menudom=document.getElementById('menu');
var topdom=document.getElementById('top');
var rulesdom=document.getElementById('rules');
var rulesbtn=document.getElementById('rulestxt');
var menubtn=document.getElementById('menubtn');
var startbtn=document.getElementById('startbtn');
var lldom=document.getElementById('liquidLuck');
var allbtns=document.querySelectorAll('.button');
var ms;
var s;
var m;
var t;
var wait;
var endt=false;
var score=0;
var hscore;
var anim=true;
var allowClick=true;
let x;
let y;
let r=20;
var bubbles= [];
var v=300;
var rtemp=0;
var gtemp=0;
var llt;
var lllife=2;
var llstatus=false;
var temp;
var vel;
var sound= new Audio();
sound.src="pop.mp3";
var c=canvasdom.getContext('2d');
canvasdom.width=0.95*window.innerWidth;
canvasdom.height=0.85*window.innerHeight;
addEventListener('resize', event => {
canvasdom.width=0.95*window.innerWidth;
canvasdom.height=0.85*window.innerHeight;
if(canvasdom.style.display=='block'){
bubbles=[];
timerReset();
timer();
anim=true;
v=300;
endt=false;
allowClick=true;
score=0;
scoredom.innerText='0';
rtemp=0;
lllife=2;
llstatus=false;
}
})
//time functions
function timer(){
t=setInterval(run ,10);
}
function run(){
clockdom.textContent=revealtime();
ms++;
if(ms==100){
s++;
ms=0;
}
if(s==60){
m++;
s=0;
}
bubbleTime();
}
function revealtime(){
return( m + ' : ' + (s<10?'0'+ s:s) + ' : ' + (ms<10?'0'+ ms:ms));
}
function timevalue(){
return((m*60*100) + (s*100) +ms);
}
function timerStop(){
clearInterval(t);
ms--;
}
function timerplay(){
t=false;
timer();
}
function timerReset(){
clearInterval(t);
t=false;
ms=0;
s=0;
m=0;
run();
ms--;
}
//time functions ends here
//bubble object
var img= new Image();
img.src='gauntlet.png';
function bubble(x,y,r,vel){
this.x=x;
this.y=y;
this.r=r;
this.duplicatev={
x:0.3,
y:0.3
};
this.velocity=vel;
this.area=Math.PI*this.r*this.r;
this.worth=this.r<55?3-((this.r%30)/10):10;
this.mass=1;
this.color= this.r<55?'rgba(126, 207, 255,0.4)':"#4E2A2A" ;
this.strength= this.r>50?5:1;
this.createGauntlet=function(){
if(r==45){
this.worth=1;
c.drawImage(img,this.x-35,this.y-35,70,70);
}
}
this.create= function() {
c.beginPath();
c.arc(this.x,this.y,this.r,0,Math.PI*2,true);
c.strokeStyle='#82B8D9';
c.fillStyle=this.color;
c.fill();
c.stroke();
c.closePath();
this.createGauntlet();
};
this.bounce=function(){
for(var i=0;i<bubbles.length;i++){
if(bubbles[i]===this){
continue;
}
if(distance(bubbles[i].x,bubbles[i].y,this.x,this.y)<this.r+bubbles[i].r){
resolveCollision(this,bubbles[i]);
}
}
};
this.move= function(){
this.create();
if(llstatus==true&&this.velocity.x!=0.3){
temp=this.velocity;
this.velocity=this.duplicatev;
this.duplicatev=temp;
}
if(this.x+this.velocity.x>=(canvasdom.width-this.r)||this.x+this.velocity.x<=this.r){
this.velocity.x*=-1;
}
if(this.y+this.velocity.y>=(canvasdom.height-this.r)||this.y+this.velocity.y<=this.r){
this.velocity.y*=-1;
}
this.bounce();
this.x+=this.velocity.x;
this.y+=this.velocity.y;
};
}
//bubble object ends here
function distance(x1,y1,x2,y2){
return (Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2)))
}
function generate(){
r=(Math.floor(Math.random()*3)*10)+30;
if(score>70&&score!=rtemp&&score-rtemp>=31){
r=60;
rtemp=score;
}
if (score>75&&score!=gtemp&&score-gtemp>=100) {
r=45;
gtemp=score;
}
vel={
x:(Math.random()-0.5)*3,
y:(Math.random()-0.5)*3
};
x=(Math.random()*(canvasdom.width-(2*r)))+r;
y=(Math.random()*(canvasdom.height-(2*r)))+r;
}
function addBubble(){
let temp1=bubbles.length;
generate();
for(var j=0;j<temp1;j++){
if(distance(bubbles[j].x,bubbles[j].y,x,y)<r+bubbles[j].r){
generate();
j=-1;
}
}
bubbles[temp1]=new bubble(x,y,r,vel);
}
function bubbleTime(){
var q=timevalue();
if((q%1500==0)){
v-=Math.floor(q/600)*20;
if(v<100){
v=100;
}
}
if(q%v==0){
addBubble();
}
}
//pause and restart starts
restart.addEventListener('click',function(){
bubbles=[];
timerReset();
timer();
anim=true;
pause.style.display='inline-block';
if (pause.innerText=='Play') {
pause.innerText='Pause';
}
v=300;
endt=false;
allowClick=true;
score=0;
rtemp=0;
lllife=2;
llstatus=false;
llupdate();
})
pause.addEventListener('click',function(){
var txt=pause.innerText;
if(txt=='Pause'){
timerStop();
pause.innerText='Play';
anim=false;
allowClick=false;
}
else{
timerplay();
pause.innerText='Pause';
anim=true;
allowClick=true;
}
})
//pause and restart ends here
function tarea(){
var ta=0;
for (var i = 0; i < bubbles.length; i++) {
ta+=bubbles[i].area;
}
return ta;
}
function gameover(){
c.clearRect(0, 0, canvasdom.width, canvasdom.height);
timerStop();
bubbles=[];
pause.style.display='none';
anim=false;
c.font='70px Creepster';
c.textAlign='center';
c.fillStyle="#D11E1E";
c.fillText("Game Over",canvasdom.width/2,canvasdom.height/2);
clearInterval(wait);
wait=false;
endt=false;
lllife=2;
llstatus=false;
}
function animate(){
requestAnimationFrame(animate);
if(anim==true){
c.clearRect(0, 0, canvasdom.width, canvasdom.height);
bubbles.forEach(function(bub){
bub.move();
})
if(tarea()>(0.7*canvasdom.width*canvasdom.height)){
endt=true;
}
if(tarea()>(0.5*canvasdom.width*canvasdom.height)){
wait=setInterval(function(){
endt=true;
},5000);
}
else if(tarea()<(0.5*canvasdom.width*canvasdom.height)){
clearInterval(wait);
wait=false;
endt=false;
}
if(endt==true){
gameover();
}
}
}
// collision between balls starts here
function rotate(velocity, angle) {
const rotatedVelocities = {
x: velocity.x * Math.cos(angle) - velocity.y * Math.sin(angle),
y: velocity.x * Math.sin(angle) + velocity.y * Math.cos(angle)
};
return rotatedVelocities;
}
function resolveCollision(particle, otherParticle) {
const xVelocityDiff = particle.velocity.x - otherParticle.velocity.x;
const yVelocityDiff = particle.velocity.y - otherParticle.velocity.y;
const xDist = otherParticle.x - particle.x;
const yDist = otherParticle.y - particle.y;
if (xVelocityDiff * xDist + yVelocityDiff * yDist >= 0) {
const angle = -Math.atan2(otherParticle.y - particle.y, otherParticle.x - particle.x);
const m1 = particle.mass;
const m2 = otherParticle.mass;
const u1 = rotate(particle.velocity, angle);
const u2 = rotate(otherParticle.velocity, angle);
const v1 = { x: u1.x * (m1 - m2) / (m1 + m2) + u2.x * 2 * m2 / (m1 + m2), y: u1.y };
const v2 = { x: u2.x * (m1 - m2) / (m1 + m2) + u1.x * 2 * m1 / (m1 + m2), y: u2.y };
const vFinal1 = rotate(v1, -angle);
const vFinal2 = rotate(v2, -angle);
particle.velocity.x = vFinal1.x;
particle.velocity.y = vFinal1.y;
otherParticle.velocity.x = vFinal2.x;
otherParticle.velocity.y = vFinal2.y;
}
}
//collision between balls ends here
function getPosition(e) {
var rect = e.target.getBoundingClientRect();
var x = e.clientX - rect.left;
var y = e.clientY - rect.top;
return {
x,
y
}
}
canvasdom.addEventListener('click',function(e){
if (allowClick==true) {
var pos=getPosition(e);
for(var i=0;i<bubbles.length;i++)
{
if(distance(pos.x,pos.y,bubbles[i].x,bubbles[i].y)<=bubbles[i].r){
sound.play();
if(bubbles[i].strength==1){
score+=bubbles[i].worth;
scoredom.innerText=score;
assignhs();
if(bubbles[i].r==45){
bubbles.splice(i,1)
gauntletSnap(i);
}
else{
bubbles.splice(i,1);
}
}
else{
bubbles[i].strength--;
bubbles[i].color=`rgba(78,42,42,${bubbles[i].strength*0.1+0.5})`;
}
}
}
}
})
function assignhs(){
if (localStorage.getItem('hs')==null) {
localStorage.setItem('hs',0);
}
if(parseInt(localStorage.getItem('hs'))<score){
localStorage.setItem('hs',score);
}
hscoredom.innerText=localStorage.getItem('hs');
}
//menu starts
function openmenu(){
topdom.style.display="none";
canvasdom.style.display="none";
rulesdom.style.display='none';
menubtn.style.display='none';
}
function hidemenu(){
menudom.style.display="none";
topdom.style.display="block";
canvasdom.style.display="block";
}
openmenu();
function startGame(){
hidemenu();
assignhs();
animate();
timerReset();
timer();
lldom.style.display="inline";
}
rulesbtn.addEventListener('click',function(){
menudom.style.display='none';
rulesdom.style.display='block';
menubtn.style.display='block';
})
menubtn.addEventListener('click',function(){
menudom.style.display='block';
rulesdom.style.display='none';
menubtn.style.display='none';
})
startbtn.addEventListener("click",function(){
startGame();
})
//menu ends
function llupdate(){
if(lllife==2){
lldom.innerText="Liquid Luck ||";
}
else if(lllife==1){
lldom.innerText="Liquid Luck |";
}
else{
lldom.style.display="none";
}
}
lldom.addEventListener('click',function(){
if (lllife>0&&llstatus==false) {
lllife--;
llstatus=true;
llupdate();
llt=setInterval(function(){
bubbles.forEach(function(bub){
temp=bub.velocity;
bub.velocity=bub.duplicatev;
bub.duplicatev=temp;
})
console.log(2);
llstatus=false;
clearInterval(llt);
llt=false;
},5000)
}
})
function gauntletSnap(i){
bubbles.splice(0,parseInt(bubbles.length/2));
}
allbtns.forEach(function(btn){
btn.addEventListener('click',function(){
sound.play()
})
})