57 lines
1.9 KiB
JavaScript
57 lines
1.9 KiB
JavaScript
class bullet {
|
|
constructor(targetx, targety, radius, speed, shotPosX, shotPosY, hasMoved, angle) { // Add hasMoved parameter
|
|
this.angle = radians(angle);
|
|
this.targetx = targetx;
|
|
this.targety = targety;
|
|
this.x = shotPosX;
|
|
this.y = shotPosY;
|
|
this.radius = radius;
|
|
this.speed = speed;
|
|
this.bulletHit = false;
|
|
this.directionX = null;
|
|
this.directionY = null;
|
|
if ((this.directionX === null) || (this.directionY === null)) {
|
|
this.directionX = this.targetx;
|
|
this.directionY = this.targety;
|
|
}
|
|
this.hasMoved = hasMoved; // Set this.hasMoved to the value of hasMoved
|
|
|
|
this.projectile = createVector(this.x, this.y);
|
|
this.direction = createVector(this.targetx - this.projectile.x, this.targety - this.projectile.y);
|
|
this.direction.normalize();
|
|
|
|
this.direction.rotate(this.angle);
|
|
}
|
|
|
|
draw() {
|
|
push();
|
|
fill(0, 255, 0);
|
|
circle(this.projectile.x, this.projectile.y, this.radius);
|
|
pop();
|
|
}
|
|
|
|
update(targetx, targety) {
|
|
this.targetx = targetx;
|
|
this.targety = targety;
|
|
this.projectile.add(p5.Vector.mult(this.direction, this.speed));
|
|
|
|
let hit = false;
|
|
let shot = true;
|
|
|
|
if (dist(this.projectile.x, this.projectile.y, this.targetx, this.targety) <= this.radius) {
|
|
hit = true;
|
|
shot = false;
|
|
if (this.hasMoved && lives != 0 && this.bulletHit == false) {
|
|
this.bulletHit = true;
|
|
lives -= 1;
|
|
}
|
|
} else if (this.projectile.x < 0 - 10 || this.projectile.x > width + 10 || this.projectile.y < 0 - 10 || this.projectile.y > height + 10) {
|
|
shot = false;
|
|
}
|
|
let isOffScreen = this.projectile.x < 0 || this.projectile.x > width || this.projectile.y < 0 || this.projectile.y > height;
|
|
let originalPos = this.projectile.x == this.targetx || this.projectile.y == this.targety;
|
|
|
|
|
|
return { hit, shot, isOffScreen, originalPos};
|
|
}
|
|
} |