From 6273854a3db1cc416e82105c17328132442f0d47 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 29 Nov 2023 14:22:06 +0100 Subject: [PATCH] The bullet flies towards player using a class --- webdev/basicbullet.js | 25 ++++++++++++++----------- webdev/game.js | 4 ++-- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/webdev/basicbullet.js b/webdev/basicbullet.js index 217d76d..2f1fcfb 100644 --- a/webdev/basicbullet.js +++ b/webdev/basicbullet.js @@ -8,8 +8,7 @@ class bullet { this.x = 500; this.y = 100; this.radius = 10; - this.speed = speed; - + this.speed = 5; this.directionX = null; @@ -21,7 +20,7 @@ class bullet { this.projectile = createVector(600, 100); // 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(); } @@ -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(); fill(0, 255, 0); - circle(500, 500, this.radius); + 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)); - 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; } 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; shot = false; } - - } - - // // Move the projectile towards the movable circle - update() { - this.projectile.add(p5.Vector.mult(this.direction, this.speed)); } diff --git a/webdev/game.js b/webdev/game.js index 9cc3241..c8ada3f 100644 --- a/webdev/game.js +++ b/webdev/game.js @@ -115,9 +115,9 @@ function setup(){ function draw(){ keyPressed() // clear the background with a transparent black color - background(0,0,0,10); + background(0,0,0,100); myBullet.draw(); - myBullet.update(); + myBullet.update(playerPosX, playerPosY); // draw a circle at the mouse position circle(playerPosX, playerPosY, radius); }