-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweapon.js
More file actions
44 lines (40 loc) · 1.25 KB
/
weapon.js
File metadata and controls
44 lines (40 loc) · 1.25 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
class Weapon {
constructor(player) {
this.player = player;
this.damage = 25;
this.bulletSpeed = 15;
this.bullets = [];
}
update(mouseX, mouseY) {
// Update bullets
for (let i = this.bullets.length - 1; i >= 0; i--) {
const bullet = this.bullets[i];
bullet.x += bullet.vx;
bullet.y += bullet.vy;
// Remove bullets that are out of bounds
if (bullet.x < 0 || bullet.x > canvas.width ||
bullet.y < 0 || bullet.y > canvas.height) {
this.bullets.splice(i, 1);
}
}
}
shoot() {
const angle = Utils.angle(this.player.x, this.player.y, mouseX, mouseY);
this.bullets.push({
x: this.player.x,
y: this.player.y,
vx: Math.cos(angle) * this.bulletSpeed,
vy: Math.sin(angle) * this.bulletSpeed,
damage: this.damage
});
}
draw(ctx) {
// Draw bullets
ctx.fillStyle = '#f1c40f';
for (const bullet of this.bullets) {
ctx.beginPath();
ctx.arc(bullet.x, bullet.y, 3, 0, Math.PI * 2);
ctx.fill();
}
}
}