-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
780 lines (639 loc) · 22.2 KB
/
javascript.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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
// ********************
// Menu settings
// ********************
// highlight the multiplayer mode
var menuOn = true,
multiplayer = true,
teamplay = true,
debugging = false;
document.getElementById("2_players").style.background = "rgba(255, 255, 255, .3)";
document.getElementById("teamplay").style.background = "rgba(255, 255, 255, .3)";
function toggleMultiplayer(object) {
if(object == "1_player") {
document.getElementById("2_players").style.background = "none";
document.getElementById("1_player").style.background = "rgba(255, 255, 255, .3)";
multiplayer = false;
} else {
document.getElementById("1_player").style.background = "none";
document.getElementById("2_players").style.background = "rgba(255, 255, 255, .3)";
multiplayer = true;
}
if(debugging) {if(debugging) {console.log("Multiplayer: " + multiplayer);}}
}
function toggleTeamplay(object) {
if(object == "no_teamplay") {
document.getElementById("teamplay").style.background = "none";
document.getElementById("no_teamplay").style.background = "rgba(255, 255, 255, .3)";
teamplay = false;
}else{
document.getElementById("no_teamplay").style.background = "none";
document.getElementById("teamplay").style.background = "rgba(255, 255, 255, .3)";
teamplay = true;
}
if(debugging) {console.log("Teamplay: " + teamplay);}
}
function toggleMenu() {
$(".menu_on").toggleClass("menu_off");
menuOn = !menuOn;
}
function toggleSettings() {
$(".settings_on").toggleClass("settings_off");
if(debugging) {console.log("Settings Toggled");}
}
// Since we are toggeling the class (so we can easily add animations),
// we must display the settings first in order for the css to work.
// When the class is being toggeled, it will look like this:
// <div class="settings_on"> -> <div class="settings_on settings_off>"
// It is always the first css class assigned to an element which decides.
toggleSettings();
function startGame() {
toggleMenu();
draw();
game();
if(debugging) {console.log("Game Started");}
}
// ********************
// Game settings etc.
// ********************
var canvas = document.getElementById("canvas_id");
var ctx = canvas.getContext("2d");
var player1Score = 0;
var player2Score = 0;
var timePlayed = 0;
var multiplayer = true;
var teamplay = false;
var teamScore = 0;
var canvasColor = "#1E023F";
var lvl = 1;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas.style.backgroundColor = canvasColor;
window.onresize = function() {
// alert("WINDOW RESIZED! Refresh page to compensate");
};
// ********************
// Sound effects!
// ********************
// buffers automatically when created
var sfxHitWall = new Audio("sfx/hitWall.mp3"),
sfxHitArray = [new Audio("sfx/hit1.mp3"), new Audio("sfx/hit2.mp3"), new Audio("sfx/hit3.mp3")];
// (new Audio()).canPlayType("audio/ogg; codecs=vorbis");
// more advanced audio stuff: http://webaudio.github.io/web-audio-api/
// ********************
// Sprite Images
// ********************
var bossSprite = new Image();
bossSprite.src = "img/boss1.png";
var bossBall = {
radius: 100,
x: canvas.width/2,
y: -100,
xms: 0,
yms: 0,
mass: 1
};
var bossSprite = sprite({
width: 905,
height: 994,
image: bossSprite,
numberOfFrames: 1,
ticksPerFrame: 1,
x: bossBall.x,
y: bossBall.y,
scaleRatio: 0.0002
});
bossSprite.x = bossBall.x - (bossSprite.width * bossSprite.scaleRatio)/2;
bossSprite.y = bossBall.y;
// **********
// Balls
// **********
var ballsArray = [];
function ballConstructor(x, xms, y, yms, radius, mass) {
this.xmsDefault = 0;
this.ymsDefault = 0;
this.xms = xms;
this.yms = yms;
this.xDefault = canvas.width/2 - radius/2;
this.yDefault = canvas.height/2 - radius/2;
this.x = x;
this.y = y;
this.radius = radius;
this.radiusDefault = 10 * canvas.width/1000;
this.friction = 0.005;
// ball not being hit = 0;
// ball being hit on the left side = 1;
// ball being hit on the right side = 2;
this.beingHit = 0;
this.beingHitX = 0;
this.beingHitY = 0;
this.color = "#E1C829";
this.storeXms = 0;
this.storeYms = 0;
this.mass = mass;
}
// Function for creating balls
function createBall(x, xms, y, yms, radius, mass) {
ballsArray.push(new ballConstructor(x, xms, y, yms, radius, mass));
}
// Function for drawing/animating balls
function drawBall() {
for(var j = 0; j < ballsArray.length; j++) {
if (ballsArray[j].radius > 0) {
ctx.beginPath();
ctx.fillStyle = ballsArray[j].color;
ctx.arc(ballsArray[j].x, ballsArray[j].y, ballsArray[j].radius, 0, Math.PI*2, false);
ctx.fill();
ctx.closePath();
// Update ball position
ballsArray[j].x += ballsArray[j].xms;
ballsArray[j].y += ballsArray[j].yms;
// Reduce radius so that the ballsArray die after a few seconds
// ballsArray[j].radius = Math.max(ballsArray[j].radius - 0.05, 0.0);
// Adds friction to the balls
ballsArray[j].xms -= ballsArray[j].xms * ballsArray[j].friction;
ballsArray[j].yms -= ballsArray[j].yms * ballsArray[j].friction;
} else {
// Remove the particle object from the array
ballsArray.splice(j, 1);
}
}
}
// **********
// PLAYERS
// **********
var player1 = {
height: 100 * canvas.width/1000,
width: 25 * canvas.width/1000,
xDefault: canvas.width - (25 * canvas.width/1000)*3,
yDefault: canvas.height/2 - (100 * canvas.width/1000)/2,
x: this.xDefault,
y: this.yDefault,
xmsDefault: 8 * canvas.width/1000,
ymsDefault: 8 * canvas.width/1000,
xms: 10,
yms: 10,
// For when hitting on the topside of the paddle
topSpeed: 0,
bottomSpeed: 0,
// paddleSpeed * kickspeed when hitting the ball
kickMultiplayer: 1.5,
// Controls
leftPressed: false,
rightPressed: false,
upPressed: false,
downPressed: false,
color: "#eee",
movement: false
};
var player2 = {
height: 100 * canvas.width/1000,
width: 25 * canvas.width/1000,
xDefault: (25 * canvas.width/1000)*2,
yDefault: canvas.height/2 - (100 * canvas.width/1000)/2,
x: this.xDefault,
y: this.yDefault,
xmsDefault: 8 * canvas.width/1000,
ymsDefault: 8 * canvas.width/1000,
xms: 10,
yms: 10,
// For when hitting on the topside of the paddle
topSpeed: 0,
bottomSpeed: 0,
// paddleSpeed * kickspeed when hitting the ball
kickMultiplayer: 1.5,
// Controls
dPressed: false,
aPressed: false,
wPressed: false,
sPressed: false,
color: "#eee",
movement: false
};
// ***********************
// No arrowkey scrolling
// ***********************
var ar = new Array(33, 34, 35, 36, 37, 38, 39, 40);
$(document).keydown(function(e) {
var key = e.which;
if($.inArray(key,ar) > -1) {
e.preventDefault();
return false;
}
return true;
});
// ********************
// Arrowkey controls
// ********************
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
// The jQuery Hotkeys plugin makes key handling across browsers much much easier. Rather than crying over indecipherable cross-browser keyCode and charCode issues, we can bind events like so:
// $(document).bind("keydown", "left", function() { ... });
// function update() {
// if (keydown.left) {
// player.x -= 2;
// }
// if (keydown.right) {
// player.x += 2;
// }
// }
function keyDownHandler(e) {
// Start game with space IF you are in the menu
if(menuOn) {
if (e.keyCode === 0 || e.keyCode === 32) {
startGame();
}
}
if(player1.movement) {
if(e.keyCode == 39) {
player1.rightPressed = true;
}
else if(e.keyCode == 37) {
player1.leftPressed = true;
}
else if(e.keyCode == 38) {
player1.upPressed = true;
}
else if(e.keyCode == 40) {
player1.downPressed = true;
}
}
if(player2.movement) {
if(e.keyCode == 87) {
player2.wPressed = true;
}
else if(e.keyCode == 68) {
player2.dPressed = true;
}
else if(e.keyCode == 83) {
player2.sPressed = true;
}
else if(e.keyCode == 65) {
player2.aPressed = true;
}
}
}
function keyUpHandler(e) {
if(e.keyCode == 39) {
player1.rightPressed = false;
}
else if(e.keyCode == 37) {
player1.leftPressed = false;
}
else if(e.keyCode == 38) {
player1.upPressed = false;
}
else if(e.keyCode == 40) {
player1.downPressed = false;
}
else if(e.keyCode == 87) {
player2.wPressed = false;
}
else if(e.keyCode == 68) {
player2.dPressed = false;
}
else if(e.keyCode == 83) {
player2.sPressed = false;
}
else if(e.keyCode == 65) {
player2.aPressed = false;
}
}
function toggleMovementPlayer1() {
player1.movement = !player1.movement;
}
function toggleMovementPlayer2() {
player2.movement = !player2.movement;
}
// function newBall() {
// player1.movement = false;
// player2.movement = false;
// ball.radius = 0;
// ball.x = ball.xDefault;
// ball.y = ball.yDefault;
// player1.x = player1.xDefault;
// player1.y = player1.yDefault;
// player2.x = player2.xDefault;
// player2.y = player2.yDefault;
// ball.xms = ball.xmsDefault;
// ball.yms = ball.ymsDefault;
// }
function newBall() {
player1.movement = false;
player2.movement = false;
player1.x = player1.xDefault;
player1.y = player1.yDefault;
player2.x = player2.xDefault;
player2.y = player2.yDefault;
createBall(canvas.width/2 - 10/2, 0, canvas.height/2 - 10/2, 0, 10, 1);
// Shoots the last ball created
ballsArray[ballsArray.length - 1].xms = (Math.round(Math.random() * 5) + 7) * (Math.round(Math.random()) * 2 - 1) * canvas.width/1000;
ballsArray[ballsArray.length - 1].yms = (Math.round(Math.random() * 2) + 4) * (Math.round(Math.random()) * 2 - 1) * canvas.width/1000;
console.log("New ball created! = " + ballsArray[0]);
player1.movement = true;
player2.movement = true;
}
newBall();
function drawPlayer1() {
// Main rectangle
ctx.beginPath();
ctx.rect(player1.x, player1.y, player1.width, player1.height);
ctx.fillStyle = player1.color;
ctx.fill();
ctx.closePath();
// Rectangle-ception
ctx.beginPath();
ctx.rect(player1.x + 5, player1.y + 5, player1.width - 10, player1.height - 10);
ctx.fillStyle = "#1E023F";
ctx.fill();
ctx.closePath();
}
function drawPlayer2() {
// Main rectangle
ctx.beginPath();
ctx.rect(player2.x, player2.y, player2.width, player2.height);
ctx.fillStyle = player2.color;
ctx.fill();
ctx.closePath();
}
var particles = [];
// Constructor for particle objects
function collisionParticlesConstructor(x, y, m) {
this.x = x;
this.y = y;
this.radius = 1 * Math.random()*3 + 0.5;
this.vx = m * Math.random()*3;
this.vy = -1.5 + Math.random()*3;
}
// Function for creating particles
function createCollisionParticles(x, y, m, quantity) {
for(i = 0; i < quantity; i++) {
particles.push(new collisionParticlesConstructor(x, y, m));
}
}
// Function for emitting particles
function drawParticles() {
for(var j = 0; j < particles.length; j++) {
if (particles[j].radius > 0) {
ctx.beginPath();
ctx.fillStyle = "#E1C829";
ctx.arc(particles[j].x, particles[j].y, particles[j].radius, 0, Math.PI*2, false);
ctx.fill();
ctx.closePath();
// Update particle position
particles[j].x += particles[j].vx;
particles[j].y += particles[j].vy;
// Reduce radius so that the particles die after a few seconds
particles[j].radius = Math.max(particles[j].radius - 0.05, 0.0);
// Update particle velocity
particles[j].vx -= 0.01 * particles[j].vx;
particles[j].vy -= 0.01 * particles[j].vy;
} else {
// Remove the particle object from the array
particles.splice(j, 1);
}
}
}
var boolDrawbossBall = false;
function newBoss(lvl) {
ball.storeXms = ball.xms;
ball.storeYms = ball.yms;
ball.yms = 0;
ball.xms = 0;
// player1.movement = false;
// player2.movement = false;
if(lvl == 1) {
drawbossBall();
}
}
function drawbossBall() {
ctx.beginPath();
ctx.arc(bossBall.x, bossBall.y, bossBall.radius, 10, 0, Math.PI*2);
ctx.fillStyle = "rgba(200, 150, 200, 0.5)";
ctx.fill();
ctx.closePath();
}
function sprite (options) {
var that = {},
frameIndex = 0,
tickCount = 0,
ticksPerFrame = options.ticksPerFrame || 0,
numberOfFrames = options.numberOfFrames || 1;
that.ctx = ctx;
that.width = options.width;
that.height = options.height;
that.x = options.x || 0;
that.y = options.y || 0;
that.image = options.image;
that.scaleRatio = options.scaleRatio || 1;
that.update = function () {
tickCount += 1;
if (tickCount > ticksPerFrame) {
tickCount = 0;
// If the current frame index is in range
if (frameIndex < numberOfFrames - 1) {
// Go to the next frame
frameIndex += 1;
} else {
frameIndex = 0;
}
}
};
that.render = function () {
// Draw the animation
ctx.drawImage(
that.image,
frameIndex * that.width / numberOfFrames,
0,
that.width / numberOfFrames,
that.height,
that.x,
that.y,
that.width / numberOfFrames * that.scaleRatio,
that.height * that.scaleRatio);
};
that.getFrameWidth = function () {
return that.width / numberOfFrames;
};
return that;
}
function draw() {
requestAnimationFrame(draw);
if(!menuOn) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
drawPlayer1();
drawPlayer2();
drawParticles();
// For debugging
// console.log("Ball: x = " + ballsArray[0].x + " y = " + ballsArray[0].y);
// ctx.beginPath();
// ctx.moveTo(canvas.width / 2, canvas.height / 2);
// ctx.lineTo(, );
// ctx.stroke();
if(boolDrawbossBall) {
drawbossBall();
bossSprite.update();
bossSprite.render();
if(bossBall.y < canvas.height/2) {
bossBall.y = bossBall.y + 3;
bossSprite.y = bossBall.y;
}
}
}
}
/*---------------------------------------------------------------------------------------------------------------------------------------------------
COLLISION LOGIC
---------------------------------------------------------------------------------------------------------------------------------------------------*/
var lastHit;
function game() {
requestAnimationFrame(game);
// :::BALLZ TO THE WALLZ COLLISION:::
// right side
for(i = 0; i < ballsArray.length; i++) {
if(ballsArray[i].x + ballsArray[i].xms > canvas.width + ballsArray[i].radius) {
// Destroys the ball
ballsArray.splice(i, 1);
player2Score++;
teamScore = 0;
document.getElementById("ball_in_play_count").innerHTML = teamScore;
document.getElementById("scorePlayer2").innerHTML = player2Score;
// Reset score
teamScore = 0;
document.getElementById("ball_in_play_count").innerHTML = teamScore;
lastHit = null;
newBall();
}
// left side
if(ballsArray[i].x + ballsArray[i].xms < 0 - ballsArray[i].radius) {
// Destroys the ball
ballsArray.splice(i, 1);
player1Score++;
teamScore = 0;
document.getElementById("ball_in_play_count").innerHTML = teamScore;
document.getElementById("scorePlayer1").innerHTML = player1Score;
// Reset score
teamScore = 0;
document.getElementById("ball_in_play_count").innerHTML = teamScore;
lastHit = null;
newBall();
}
// top side
if(ballsArray[i].y + ballsArray[i].yms > canvas.height - ballsArray[i].radius) {
ballsArray[i].yms = -ballsArray[i].yms;
sfxHitWall.play();
lastHit = null;
}
// bottom side
if(ballsArray[i].y + ballsArray[i].yms < ballsArray[i].radius) {
ballsArray[i].yms = -ballsArray[i].yms;
sfxHitWall.play();
lastHit = null;
}
}
// Paddle movement (paddle + wall collision)
if(player1.rightPressed && player1.x < canvas.width - player1.width) {
player1.x += player1.xms;
}
if(player1.leftPressed && player1.x > 0) {
player1.x -= player1.xms;
}
if(player1.upPressed && player1.y > 0) {
player1.y -= player1.yms;
}
if(player1.downPressed && player1.y < canvas.height - player1.height) {
player1.y += player1.yms;
}
if(player2.dPressed && player2.x < canvas.width-player1.width) {
player2.x += player2.xms;
}
if(player2.aPressed && player2.x > 0) {
player2.x -= player2.xms;
}
if(player2.wPressed && player2.y > 0) {
player2.y -= player2.yms;
}
if(player2.sPressed && player2.y < canvas.height - player2.height) {
player2.y += player2.yms;
}
// :::PLAYER 1 + BALL COLLISION:::
for(i = 0; i < ballsArray.length; i++) {
// Distance between ball x-position and paddle-center < ball radius + half the paddle width (+ xms)
if(Math.abs(ballsArray[i].x - (player1.x + player1.width/2)) <= ballsArray[i].radius + player1.width/2 + Math.abs(ballsArray[i].xms) &&
// Distance between ball y-position and paddle-center < ball radius + half the paddle height (+ yms)
Math.abs(ballsArray[i].y - (player1.y + player1.height/2)) <= ballsArray[i].radius + player1.height/2 + Math.abs(ballsArray[i].yms) && lastHit != "player1") {
// Sets the ball's x-velocity to either a positive or a negative value depending on which side of the paddle it hit
// Left-side hit
if(ballsArray[i].x < player1.x + player1.width/2) {
ballsArray[i].xms = -player1.xms * player1.kickMultiplayer;
createCollisionParticles(ballsArray[i].x + ballsArray[i].radius - Math.abs(ballsArray[i].xms), ballsArray[i].y, -1, 20);
// Right-side hit
}else{
ballsArray[i].xms = player1.xms * player1.kickMultiplayer;
createCollisionParticles(ballsArray[i].x - ballsArray[i].radius + Math.abs(ballsArray[i].xms), ballsArray[i].y, 1, 20);
}
// Changes the ball's y-speed and changes the movement in the y-direction depending on where it hits the paddle
ballsArray[i].yms = (((((100 / player1.height) * (ballsArray[i].y - player1.y)) * 2) - 100) / 100) * player1.xms;
// Plays a random sound effect from the array
sfxHitArray[Math.floor(Math.random() * sfxHitArray.length)].play();
// Update score
teamScore ++;
document.getElementById("ball_in_play_count").innerHTML = teamScore;
// lastHit is set to player1 to prevent the collision code from being executed more than once
lastHit = "player1";
}
// :::PLAYER 2 + BALL COLLISION:::
if(Math.abs(ballsArray[i].x - (player2.x + player2.width/2)) <= ballsArray[i].radius + player2.width/2 + Math.abs(ballsArray[i].xms) &&
Math.abs(ballsArray[i].y - (player2.y + player2.height/2)) <= ballsArray[i].radius + player2.height/2 + Math.abs(ballsArray[i].yms) && lastHit != "player2") {
if(ballsArray[i].x < player2.x + player2.width/2) {
ballsArray[i].xms = -player2.xms * player2.kickMultiplayer;
createCollisionParticles(ballsArray[i].x + ballsArray[i].radius - Math.abs(ballsArray[i].xms), ballsArray[i].y, -1, 20);
}else{
ballsArray[i].xms = player2.xms * player2.kickMultiplayer;
createCollisionParticles(ballsArray[i].x - ballsArray[i].radius + Math.abs(ballsArray[i].xms), ballsArray[i].y, 1, 20);
}
ballsArray[i].yms = (((((100 / player2.height) * (ballsArray[i].y - player2.y)) * 2) - 100) / 100) * player2.xms;
sfxHitArray[Math.floor(Math.random() * sfxHitArray.length)].play();
teamScore ++;
document.getElementById("ball_in_play_count").innerHTML = teamScore;
lastHit = "player2";
}
}
// :::PADDLE + PARTICLE COLLISION:::
for(var i = 0; i < particles.length; i++) {
// Player 1
if(Math.abs(particles[i].x - (player1.x + player1.width/2)) <= particles[i].radius + player1.width/2 + Math.abs(particles[i].vx) &&
Math.abs(particles[i].y - (player1.y + player1.height/2)) <= particles[i].radius + player1.height/2 + Math.abs(particles[i].vy)) {
particles[i].vx -= (particles[i].x < player1.x + player1.width/2) ? player1.xms : -player1.xms;
particles[i].x += particles[i].vx;
particles[i].y += particles[i].vy;
}
// Player 2
if(Math.abs(particles[i].x - (player2.x + player2.width/2)) <= particles[i].radius + player2.width/2 + Math.abs(particles[i].vx) &&
Math.abs(particles[i].y - (player2.y + player2.height/2)) <= particles[i].radius + player2.height/2 + Math.abs(particles[i].vy)) {
particles[i].vx -= (particles[i].x < player2.x + player2.width/2) ? player2.xms : -player2.xms;
particles[i].x += particles[i].vx;
particles[i].y += particles[i].vy;
}
}
// :::BALL + BOSS COLLISION:::
for(i = 0; i < ballsArray.length; i++) {
if(Math.sqrt(Math.pow(ballsArray[i].x - bossBall.x, 2) + Math.pow(ballsArray[i].y - bossBall.y, 2)) <= ballsArray[i].radius + bossBall.radius /*&& lastHit != "boss"*/) {
if(debugging) {console.log("green!");}
var ballVelocity = Math.sqrt(Math.pow(ballsArray[i].xms, 2) + Math.pow(ballsArray[i].yms, 2));
if(debugging) {console.log("ballVelocity = " + ballVelocity);}
var bossVelocity = Math.sqrt(Math.pow(bossBall.xms, 2) + Math.pow(bossBall.yms, 2)) + 1; // alert +1 to avoid devide by zero
if(debugging) {console.log("bossVelocity = " + bossVelocity);}
var thetaBall = Math.abs(Math.acos(ballsArray[i].xms / ballVelocity));
if(debugging) {console.log("thetaBall = " + thetaBall);}
var thetaBoss = Math.abs(Math.acos(bossBall.xms / bossVelocity));
if(debugging) {console.log("thetaBoss = " + thetaBoss);}
var phi = Math.acos((ballsArray[i].xms * (bossBall.x - ballsArray[i].x) + ballsArray[i].yms * (bossBall.y - ballsArray[i].y)) / (ballVelocity * Math.sqrt(Math.pow(bossBall.x - ballsArray[i].x, 2) + Math.pow(bossBall.y - ballsArray[i].y, 2))));
if(debugging) {console.log("phi = " + phi);}
ballsArray[i].xms = (ballVelocity * Math.cos(thetaBall - phi) * (ballsArray[i].mass - bossBall.mass) + 2 * bossBall.mass * bossVelocity * Math.cos(thetaBoss - phi)) / (ballsArray[i].mass + bossBall.mass) * Math.cos(phi) + ballVelocity * Math.sin(thetaBall - phi) * Math.cos(phi + Math.PI / 2);
ballsArray[i].yms = (ballVelocity * Math.cos(thetaBall - phi) * (ballsArray[i].mass - bossBall.mass) + 2 * bossBall.mass * bossVelocity * Math.cos(thetaBoss - phi)) / (ballsArray[i].mass + bossBall.mass) * Math.sin(phi) + ballVelocity * Math.sin(thetaBall - phi) * Math.sin(phi + Math.PI / 2);
if(debugging) {console.log("ballsArray[i].xms = " + ballsArray[i].xms + " ballsArray[i].yms = " + ballsArray[i].yms);}
lastHit = "boss";
}
}
} // End of game()
startGame();