Intergrated bullet class into game.js

This commit is contained in:
Sam
2023-11-29 15:46:04 +01:00
parent 77ba652a69
commit 2e9c22dee2
2 changed files with 101 additions and 70 deletions

View File

@@ -1,42 +1,68 @@
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;
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
this.targetx = targetx;
this.targety = targety;
this.x = shotPosX;
this.y = shotPosY;
this.radius = radius;
this.speed = speed;
// 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) <= radius) {
this.directionX = null;
this.directionY = null;
if ((this.directionX === null) || (this.directionY === null)) {
this.directionX = this.targetx;
this.directionY = this.targety;
}
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.
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) {
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;
}
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;
}
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;
}
return { hit, shot };
}
}
}
}