The bullet flies towards player using a class

This commit is contained in:
Sam
2023-11-29 14:22:06 +01:00
parent ee7f031775
commit 6273854a3d
2 changed files with 16 additions and 13 deletions

View File

@@ -8,8 +8,7 @@ class bullet {
this.x = 500; this.x = 500;
this.y = 100; this.y = 100;
this.radius = 10; this.radius = 10;
this.speed = speed; this.speed = 5;
this.directionX = null; this.directionX = null;
@@ -21,7 +20,7 @@ class bullet {
this.projectile = createVector(600, 100); this.projectile = createVector(600, 100);
// Calculate the initial direction // Calculate the initial direction
this.direction = createVector(this.targetX - this.projectile.x, this.targetY - this.projectile.y); this.direction = createVector(this.targetx - this.projectile.x, this.targety - this.projectile.y);
this.direction.normalize(); this.direction.normalize();
} }
@@ -33,15 +32,25 @@ class bullet {
//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 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(); push();
fill(0, 255, 0); fill(0, 255, 0);
circle(500, 500, this.radius); circle(this.projectile.x, this.projectile.y, this.radius);
pop(); pop();
// Set the initial position of the projectile to the player's position // 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)); this.projectile.add(p5.Vector.mult(this.direction, this.speed));
if (dist(this.projectile.x, this.projectile.y, this.directionX, this.directionY) <= this.radius) { if (dist(this.projectile.x, this.projectile.y, this.targetx, this.targety) <= this.radius) {
hit = true; hit = true;
} }
else if (this.projectile.x < 0 - 10 || this.projectile.x > width + 10 || this.projectile.y < 0 - 10 || this.projectile.y > height + 10) { else if (this.projectile.x < 0 - 10 || this.projectile.x > width + 10 || this.projectile.y < 0 - 10 || this.projectile.y > height + 10) {
@@ -49,12 +58,6 @@ class bullet {
bounceY = this.projectile.y; bounceY = this.projectile.y;
shot = false; shot = false;
} }
}
// // Move the projectile towards the movable circle
update() {
this.projectile.add(p5.Vector.mult(this.direction, this.speed));
} }

View File

@@ -115,9 +115,9 @@ function setup(){
function draw(){ function draw(){
keyPressed() keyPressed()
// clear the background with a transparent black color // clear the background with a transparent black color
background(0,0,0,10); background(0,0,0,100);
myBullet.draw(); myBullet.draw();
myBullet.update(); myBullet.update(playerPosX, playerPosY);
// draw a circle at the mouse position // draw a circle at the mouse position
circle(playerPosX, playerPosY, radius); circle(playerPosX, playerPosY, radius);
} }