Added documentation and cleaned up code

This commit is contained in:
Sam
2023-12-12 21:41:18 +01:00
parent 7c0e90886c
commit fbe19ba35e

View File

@@ -1,57 +1,66 @@
class bullet { class bullet {
constructor(targetx, targety, radius, speed, shotPosX, shotPosY, hasMoved, angle) { // Add hasMoved parameter constructor(targetx, targety, radius, speed, shotPosX, shotPosY, hasMoved, angle) { // Add hasMoved parameter
this.angle = radians(angle); // add all incoming variables to the class
this.targetx = targetx; this.angle = radians(angle);
this.targety = targety; this.targetx = targetx;
this.x = shotPosX; this.targety = targety;
this.y = shotPosY; this.x = shotPosX;
this.radius = radius; this.y = shotPosY;
this.speed = speed; this.radius = radius;
this.bulletHit = false; this.speed = speed;
this.directionX = null; this.bulletHit = false;
this.directionY = null; this.directionX = null;
if ((this.directionX === null) || (this.directionY === null)) { this.directionY = null;
this.directionX = this.targetx; this.hasMoved = hasMoved;
this.directionY = this.targety; // Set a variable that cant be changed
} if ((this.directionX === null) || (this.directionY === null)) {
this.hasMoved = hasMoved; // Set this.hasMoved to the value of hasMoved this.directionX = this.targetx;
this.directionY = this.targety;
}
// create a vector for the projectile and the direction
this.projectile = createVector(this.x, this.y);
this.direction = createVector(this.targetx - this.projectile.x, this.targety - this.projectile.y);
this.direction.normalize();
// rotate the direction towards the player
this.direction.rotate(this.angle);
}
this.projectile = createVector(this.x, this.y); draw() {
this.direction = createVector(this.targetx - this.projectile.x, this.targety - this.projectile.y); //draw the bullet
this.direction.normalize(); push();
fill(0, 255, 0);
circle(this.projectile.x, this.projectile.y, this.radius);
pop();
}
this.direction.rotate(this.angle); update(targetx, targety) {
} // keeps the projetile updated so hit collision can be checked
this.targetx = targetx;
this.targety = targety;
// update the projectile
this.projectile.add(p5.Vector.mult(this.direction, this.speed));
draw() { let hit = false;
push(); let shot = true;
fill(0, 255, 0); // check if the projectile has hit the player
circle(this.projectile.x, this.projectile.y, this.radius); if (dist(this.projectile.x, this.projectile.y, this.targetx, this.targety) <= this.radius) {
pop(); hit = true;
} shot = false;
// if the projectile has hit the player and the player has lives left, remove a life
if (this.hasMoved && lives != 0 && this.bulletHit == false) {
this.bulletHit = true;
lives -= 1;
}
//if the bullet hits one of the wall reset the shot variable so a new wave of bullets can be fired
} else if (this.projectile.x < 0 - 10 || this.projectile.x > width + 10 || this.projectile.y < 0 - 10 || this.projectile.y > height + 10) {
shot = false;
}
// check if the projectile is off screen so it can be removed
let isOffScreen = this.projectile.x < 0 || this.projectile.x > width || this.projectile.y < 0 || this.projectile.y > height;
//unused variable to check if the projetile passsed the original player position when it was fired
let originalPos = this.projectile.x == this.targetx || this.projectile.y == this.targety;
update(targetx, targety) { // return all the variables
this.targetx = targetx; return { hit, shot, isOffScreen, originalPos };
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};
}
} }