-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojectile.js
39 lines (35 loc) · 1004 Bytes
/
projectile.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
class Projectile {
constructor(x, y, vx, vy, damage) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.radius = 5;
this.damage = damage;
}
update(deltaTime) {
this.x += this.vx * deltaTime;
this.y += this.vy * deltaTime;
}
draw(ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = '#ffff00';
ctx.fill();
ctx.strokeStyle = '#ffa500';
ctx.lineWidth = 2;
ctx.stroke();
}
checkCollision(object) {
const dx = this.x - object.x;
const dy = this.y - object.y;
const distance = Math.sqrt(dx * dx + dy * dy);
return distance < this.radius + object.radius;
}
isOffScreen(width, height) {
return this.x < -this.radius ||
this.x > width + this.radius ||
this.y < -this.radius ||
this.y > height + this.radius;
}
}