-
Notifications
You must be signed in to change notification settings - Fork 0
/
projectile.js
44 lines (35 loc) · 1.11 KB
/
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
40
41
42
43
44
"use strict";
class Projectile {
constructor(gameWidth, gameHeight, playerX, playerY, xVel, yVel, radius, damage) {
this.radius = radius;
this.color = "dodgerblue";
this.xVel = xVel;
this.yVel = yVel;
this.damage = damage;
this.position = {
x: playerX,
y: playerY
}
}
draw(ctx) {
ctx.beginPath();
ctx.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2, false);
ctx.fillStyle = this.color;
ctx.fill();
}
update() {
this.position.x += this.xVel;
this.position.y += this.yVel;
}
checkCollision(enemyX, enemyY, enemyRadius) {
let xDist = Math.abs(enemyX - this.position.x);
let yDist = Math.abs(enemyY - this.position.y);
let totalRadii = enemyRadius + this.radius;
if (Math.sqrt(Math.pow(xDist, 2) + Math.pow(yDist, 2)) <= totalRadii) return true;
else return false;
}
checkEdgeHit(gameWidth, gameHeight) {
if (this.position.x - this.radius <= 0 || this.position.y - this.radius <= 0 || this.position.x + this.radius >= gameWidth || this.position.y + this.radius >= gameHeight) return true;
else return false;
}
}