-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBulletGenerator.js
61 lines (50 loc) · 1.71 KB
/
BulletGenerator.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
import Bullet from "./Bullet.js";
export default class BulletGenerator {
bullets = [];
constructor(canvas, maxBullets, color) {
this.maxBullets = maxBullets;
this.canvas = canvas;
this.color = color;
this.playerShoot = new Audio('sound/shoot.wav');
this.playerShoot.volume = 0.4;
}
draw(ctx) {
//remove bullets out of canvas
//then draw the remaining on screen
this.bullets = this.bullets.filter(bullet => bullet.y + bullet.width > 0
&& bullet.y <= this.canvas.height
)
this.bullets.forEach(bullet => bullet.draw(ctx)
// timing management
);
}
ifCollides(sprite) {
const bulletCollidingIndex = this.bullets.findIndex(bullet => bullet.collision(sprite));
if (bulletCollidingIndex >= 0) {
this.bullets.splice(bulletCollidingIndex, 1);
return true;
}
return false;
}
ifBulletsCollide(otherBulletGenerator) {
this.bullets = this.bullets.filter((bullet) => {
const collision = otherBulletGenerator.bullets.findIndex(otherBullet => bullet.collision(otherBullet))
if (collision >= 0) {
otherBulletGenerator.bullets.splice(collision, 1);
return false;
}
return true;
})
}
shoot(x, y, velocity) {
//checking if next bullet is allowed to be fired or not
if (this.bullets.length < this.maxBullets) {
this.bullets.push(new Bullet(this.canvas, x, y, velocity, this.color));
this.playerShoot.currentTime = 0;
this.playerShoot.play;
}
}
reset() {
this.bullets = [];
}
}