Files
J1B2-Game-controller/webdev/js/basicbullet.js
2023-11-28 13:36:31 +01:00

42 lines
1.4 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.x = targetx;
this.y = targety;
this.radius = radius;
this.speed = speed;
// Set the initial position of the projectile to the player's position
let projectile = createVector(this.x, this.y);
this.x = directionX;
this.y = directionY;
// Calculate the initial direction
direction = createVector(targetX - projectile.x, targetY - projectile.y);
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.
fill(255, 0, 0);
circle(500, 500, 5);
projectile.add(p5.Vector.mult(direction, shotSpeed));
if (dist(projectile.x, projectile.y, directionX, directionY) <= this.radius) {
hit = true;
}
else if (projectile.x < 0 - 10 || projectile.x > width + 10 || projectile.y < 0 - 10 || projectile.y > height + 10) {
bounceX = projectile.x;
bounceY = projectile.y;
shot = false;
}
}
}