54 lines
1.6 KiB
JavaScript
54 lines
1.6 KiB
JavaScript
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) {
|
|
//"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
|
|
this.targetx = targetx;
|
|
this.targety = targety;
|
|
this.x = 500;
|
|
this.y = 100;
|
|
this.radius = 10;
|
|
this.speed = speed;
|
|
|
|
|
|
|
|
this.directionX = null;
|
|
this.directionY = null;
|
|
if ((this.directionX === null) || (this.directionY === null)) {
|
|
this.directionX = this.targetx;
|
|
this.directionY = this.targety;
|
|
}
|
|
|
|
this.projectile = createVector(600, 100);
|
|
// Calculate the initial direction
|
|
this.direction = createVector(this.targetX - this.projectile.x, this.targetY - this.projectile.y);
|
|
this.direction.normalize();
|
|
|
|
this.projectile.add(p5.Vector.mult(this.direction, this.speed));
|
|
}
|
|
|
|
|
|
|
|
|
|
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.
|
|
fill(0, 255, 0);
|
|
circle(500, 500, this.radius);
|
|
|
|
// Set the initial position of the projectile to the player's position
|
|
|
|
|
|
|
|
if (dist(this.projectile.x, this.projectile.y, this.directionX, this.directionY) <= this.radius) {
|
|
hit = true;
|
|
}
|
|
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;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
} |