-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
551 lines (451 loc) · 12.4 KB
/
app.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
// Importing souud effect..!
const introMusic = new Audio("./music/introSong.mp3");
const shootingSound = new Audio("./music/shoooting.mp3");
const killEnemySound = new Audio("./music/killEnemy.mp3");
const gameOverSound = new Audio("./music/gameOver.mp3");
const heavyWeaponSound = new Audio("./music/heavyWeapon.mp3");
const hugeWeaponSound = new Audio("./music/hugeWeapon.mp3");
// start music
introMusic.play();
// basics environment setup
const canvas = document.createElement("canvas");
const lightWeaponDamage = 10;
const heavyWeaponDamage = 20;
const LargeWeaponDamage = 50;
let playerScore = 0;
document.querySelector(".myGame").appendChild(canvas);
canvas.width = innerWidth;
canvas.height = innerHeight;
// we have to make 2D animation
const context = canvas.getContext("2d");
let difficulty = 2;
const form = document.querySelector("form");
const scoreBoard = document.querySelector(".scoreBoard");
// basics functions
// EventListener for difficulty
document.querySelector("input").addEventListener("click", (e) => {
e.preventDefault();
// stoping music
introMusic.pause();
// making form invisible
form.style.display = "none";
// making scoreBoard visible
scoreBoard.style.display = "block";
// getting difficulty selected user
const userValue = document.getElementById("difficulty").value;
// alert(userValue);
if (userValue === "Easy") {
setInterval(spawnEnemy, 2000);
return (difficulty = 5);
}
if (userValue === "Medium") {
setInterval(spawnEnemy, 1500);
return (difficulty = 8);
}
if (userValue === "Hard") {
setInterval(spawnEnemy, 1100);
return (difficulty = 10);
}
if (userValue === "Insane") {
setInterval(spawnEnemy, 800);
return (difficulty = 12);
}
});
// ---------end screen------
const gameOverLoader = () => {
// creating end screen div and play again and high score elements
const gameOverBanner = document.createElement('div');
const gameOverBtn = document.createElement('button');
const highScore = document.createElement('div');
highScore.innerText = `High Score: ${localStorage.getItem("highScore") ?
localStorage.getItem("highScore") :
playerScore
}`;
const oldHighScore =
localStorage.getItem("highScore") &&
localStorage.getItem("highScore");
if (oldHighScore < playerScore) {
// oldHighScore = playerScore;
localStorage.setItem("highScore", playerScore);
highScore.innerText = `High Score: ${playerScore}`;
}
// updating high score
// adding text to playagain button
gameOverBtn.innerText = "Play Again";
gameOverBanner.appendChild(highScore);
gameOverBanner.appendChild(gameOverBtn);
gameOverBtn.onclick = () => {
window.location.reload();
};
gameOverBanner.classList.add("gameover");
document.querySelector("body").appendChild(gameOverBanner);
}
// ------------------
playerPosition = {
x: canvas.width / 2,
y: canvas.height / 2,
};
// create a class
class Player {
constructor(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
}
draw() {
context.beginPath();
context.arc(
this.x,
this.y,
this.radius,
(Math.PI / 180) * 0,
(Math.PI / 180) * 360,
false
);
context.fillStyle = this.color;
context.fill();
}
}
// -------- make a weapon---------
class Weapon {
constructor(x, y, radius, color, velocity, damage) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.velocity = velocity;
this.damage = damage;
}
draw() {
context.beginPath();
context.arc(
this.x,
this.y,
this.radius,
(Math.PI / 180) * 0,
(Math.PI / 180) * 360,
false
);
context.fillStyle = this.color;
context.fill();
}
update() {
this.draw();
this.x += this.velocity.x;
this.y += this.velocity.y;
}
}
// -------- make a very large weapon---------
class LargeWeapon {
constructor(x, y, damage) {
this.x = x;
this.y = y;
this.color = "red";
this.damage = damage;
}
draw() {
context.beginPath();
context.fillStyle = this.color;
context.fillRect(this.x, this.y, 150, canvas.height);
// context.fill();
}
update() {
this.draw();
this.x += 10;
// this.y += 10;
}
}
// ------make enemy-------
class Enemy {
constructor(x, y, radius, color, velocity) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.velocity = velocity;
}
draw() {
context.beginPath();
context.arc(
this.x,
this.y,
this.radius,
(Math.PI / 180) * 0,
(Math.PI / 180) * 360,
false
);
context.fillStyle = this.color;
context.fill();
}
update() {
this.draw();
(this.x += this.velocity.x), (this.y += this.velocity.y);
}
}
// ----------------create partical class-------------
const fraction = 0.99;
class Partical {
constructor(x, y, radius, color, velocity) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.velocity = velocity;
this.alpha = 1;
}
draw() {
context.save();
context.globalAlpha = this.alpha;
context.beginPath();
context.arc(
this.x,
this.y,
this.radius,
(Math.PI / 180) * 0,
(Math.PI / 180) * 360,
false
);
context.fillStyle = this.color;
context.fill();
context.restore();
}
update() {
this.draw();
this.velocity.x *= fraction;
this.velocity.y *= fraction;
this.x += this.velocity.x;
this.y += this.velocity.y;
this.alpha -= 0.01;
}
}
// ------------------ Main Code Start Here ---------------------
const a = new Player(playerPosition.x, playerPosition.y, 15, "whitesmoke");
const weapons = [];
const enemies = [];
const particles = [];
const largeWeapons = [];
// function to create spawnEnemy at random location
const spawnEnemy = () => {
// generating random size of enemy
const enemySize = Math.random() * (40 - 5) + 5;
const enemyColor = `hsl(${Math.floor(Math.random() * 360)}, 100%, 50%)`;
let random;
// this condition check enemy position
if (Math.random() < 0.5) {
random = {
x: Math.random() < 0.5 ? canvas.width + enemySize : 0 - enemySize,
y: Math.random() * canvas.height,
};
} else {
random = {
x: Math.random() * canvas.width,
y: Math.random() < 0.5 ? canvas.height + enemySize : 0 - enemySize,
};
}
const myAngle = Math.atan2(
canvas.height / 2 - random.y,
canvas.width / 2 - random.x
);
const velocity = {
x: Math.cos(myAngle) * difficulty,
y: Math.sin(myAngle) * difficulty,
};
enemies.push(new Enemy(random.x, random.y, enemySize, enemyColor, velocity));
};
// ----------- creating animation function ---------
let animationId;
function animation() {
animationId = requestAnimationFrame(animation);
scoreBoard.innerHTML = `Score: ${playerScore}`
context.fillStyle = "rgba(0,0,0,0.2)";
context.fillRect(0, 0, canvas.width, canvas.height);
// context.clearRect
// console.log(Math.random())
a.draw();
// generating particles
particles.forEach((partical, particalIndex) => {
if (partical.alpha <= 0) {
particles.splice(particalIndex, 1);
} else {
partical.update();
}
});
// generting largeWeapon
largeWeapons.forEach((largeWeapon, largeWeaponIndex) => {
if (largeWeapon.x > canvas.width) {
largeWeapons.splice(largeWeaponIndex, 1);
} else {
largeWeapon.update();
}
});
// generating Bullets
weapons.forEach((weapon, weaponIndex) => {
// weapon.draw();
weapon.update();
if (
weapon.x + weapon.radius < 1 ||
weapon.y + weapon.radius < 1 ||
weapon.x - weapon.radius > canvas.width ||
weapon.y - weapon.radius > canvas.height
) {
weapons.splice(weaponIndex, 1);
}
});
// generating enemies
enemies.forEach((enemy, enemyIndex) => {
enemy.update();
const distanceBetweenPlayerAndEnemy = Math.hypot(
a.x - enemy.x,
a.y - enemy.y
);
if (distanceBetweenPlayerAndEnemy - a.radius - enemy.radius < 1) {
cancelAnimationFrame(animationId);
gameOverSound.play();
return gameOverLoader();
// console.log("Game Over..!")
}
//
// generting large weapons
largeWeapons.forEach((largeWeapon) => {
// find the distance largeWeapon and enemy
const distanceBetweenLargeWeaponAndEnemy = largeWeapon.x - enemy.x;
if (
distanceBetweenLargeWeaponAndEnemy <= 200 &&
distanceBetweenLargeWeaponAndEnemy >= -200
) {
// increasing the score
playerScore += 10;
// rendering the score in html
scoreBoard.innerHTML = `Score: ${playerScore}`
setTimeout(() => {
killEnemySound.play();
enemies.splice(enemyIndex, 1);
// weapons.splice(weaponIndex, 1);
}, 0);
enemies.splice(enemyIndex, 1);
}
});
// console.log(playerScore)
// generating weapons
weapons.forEach((weapon, weaponIndex) => {
// finding the distance between weapon and enemy
const distanceBetweenWeaponAndEnemy = Math.hypot(
weapon.x - enemy.x,
weapon.y - enemy.y
);
if (distanceBetweenWeaponAndEnemy - weapon.radius - enemy.radius < 1) {
// console.log("Killed")
// reducing the size of enemy
if (enemy.radius > weapon.damage + 6) {
gsap.to(enemy, {
radius: enemy.radius - weapon.damage,
});
// enemy.radius -= 5;
setTimeout(() => {
// enemies.splice(enemyIndex, 1);
weapons.splice(weaponIndex, 1);
}, 0);
}
// removing enemy
else {
for (let i = 0; i < enemy.radius * 3; i++) {
particles.push(
new Partical(weapon.x, weapon.y, Math.random() * 2, enemy.color, {
x: (Math.random() - 0.5) * (Math.random() * 6),
y: (Math.random() - 0.5) * (Math.random() * 6),
})
);
}
// increasing score
playerScore += 10;
// rendaring player score in scorebord html element
scoreBoard.innerHTML = `Score: ${playerScore}`
setTimeout(() => {
killEnemySound.play();
enemies.splice(enemyIndex, 1);
weapons.splice(weaponIndex, 1);
}, 0);
}
}
});
});
}
// console.log(gsap)
// setInterval(spawnEnemy, 1000)
// ------------- Adding eventListener --------------------------------
canvas.addEventListener("click", (e) => {
shootingSound.play();
const myAngle = Math.atan2(
e.clientY - canvas.height / 2,
e.clientX - canvas.width / 2
);
const velocity = {
y: Math.sin(myAngle) * 6,
x: Math.cos(myAngle) * 6,
};
// console.log(myAngle);
weapons.push(
new Weapon(
canvas.width / 2,
canvas.height / 2,
6,
"white",
velocity,
lightWeaponDamage
)
);
});
// --------------event listener for right click
canvas.addEventListener("contextmenu", (e) => {
e.preventDefault();
if (playerScore <= 0) {
return;
}
heavyWeaponSound.play();
// Decrering the player score
playerScore -= 2;
// updating player score in html
scoreBoard.innerHTML = `Score: ${playerScore}`
const myAngle = Math.atan2(
e.clientY - canvas.height / 2,
e.clientX - canvas.width / 2
);
const velocity = {
y: Math.sin(myAngle) * 4,
x: Math.cos(myAngle) * 4,
};
// console.log(myAngle);
weapons.push(
new Weapon(
canvas.width / 2,
canvas.height / 2,
20,
"cyan",
velocity,
heavyWeaponDamage
)
);
});
addEventListener("keypress", (e) => {
if (e.key === " ") {
if (playerScore < 25) {
return;
}
hugeWeaponSound.play();
// Decrering the player score
playerScore -= 25;
// updating player score in html
scoreBoard.innerHTML = `Score: ${playerScore}`
// console.log("Enter key pressed");
largeWeapons.push(new LargeWeapon(0, 0, LargeWeaponDamage));
}
});
addEventListener("contextmenu", (e) => {
e.preventDefault();
});
addEventListener("resize", () => {
window.location.reload();
})
// console.log(`key: ${e.key}`)
animation();