From 9c862e7b7bcd57a8bc81a01798fe8db64c74a268 Mon Sep 17 00:00:00 2001 From: sam Date: Tue, 28 Nov 2023 13:19:06 +0100 Subject: [PATCH] added code from game.js --- web/js/basicbullet.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/web/js/basicbullet.js b/web/js/basicbullet.js index 0094b6f..e747d8f 100644 --- a/web/js/basicbullet.js +++ b/web/js/basicbullet.js @@ -1,14 +1,21 @@ class bullet { //Een constructor voert eerst de code uit die er in staat voordat de rest van de class wordt uitgevoerd. - constructor(x, y, radius, speed) { + 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 = x; - this.y = y; + 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(shotPosX, shotPosY); + x = directionX; + y = directionY; + // Calculate the initial direction + direction = createVector(targetX - projectile.x, targetY - projectile.y); + direction.normalize(); } @@ -19,6 +26,16 @@ 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) { + 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; + } + }