fixed timer, cleanup junk and improved bullets

This commit is contained in:
Sam
2023-11-30 15:02:24 +01:00
parent d703a82a7f
commit 559621835f
2 changed files with 44 additions and 60 deletions

View File

@@ -1,68 +1,51 @@
class bullet {
//Een constructor voert eerst de code uit die er in staat voordat de rest van de class wordt uitgevoerd.
constructor(targetx, targety, radius, speed, shotPosX, shotPosY) {
//"This" moet gebruikt worden om de variabelen aan te maken in de class en het zorgt er voor dat de variabelen niet alleen in de constructor gebruikt kunnen worden,
//maar ook in de rest van de class
constructor(targetx, targety, radius, speed, shotPosX, shotPosY, hasMoved) { // Add hasMoved parameter
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.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);
// Calculate the initial direction
this.direction = createVector(this.targetx - this.projectile.x, this.targety - this.projectile.y);
this.direction.normalize();
}
draw() {
//push en pop zorgen er voor dat de code tussen push en pop een eigen canvas heeft. Dus dat er meerdere projectielen tegelijk kunnen zijn.
}
draw() {
push();
fill(0, 255, 0);
circle(this.projectile.x, this.projectile.y, this.radius);
pop();
// Set the initial position of the projectile to the player's position
}
// // Move the projectile towards the movable circle
update(targetx, targety) {
}
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;
}
else if (this.projectile.x < 0 - 10 || this.projectile.x > width + 10 || this.projectile.y < 0 - 10 || this.projectile.y > height + 10) {
// bounceX = this.projectile.x;
// bounceY = this.projectile.y;
shot = false;
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;
}
return { hit, shot };
}
}
}
}