From cd675fe834709f02010a2b16e22b9f39b6ba1dd5 Mon Sep 17 00:00:00 2001 From: Mees Roelofsz Date: Sun, 26 Nov 2023 15:11:18 +0100 Subject: [PATCH] trying to add projectiles into list --- web/game.js | 68 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/web/game.js b/web/game.js index d682492..ad9e51b 100644 --- a/web/game.js +++ b/web/game.js @@ -15,10 +15,12 @@ let booleanArray = window.booleanArray; // const squareSize = 100; let shotSpeed = 5; -let projectile; +let projectiles = []; let projSize = 5; let shot; +let time = 0; + // the function setup() is called once when the page is loaded function setup() { // create a canvas element and append it to the body @@ -29,6 +31,13 @@ function setup() { noStroke(); } +async function score() { + while (true) { + time += 1; + await sleep(1000); + } +} + function keyPressed() { if (keyIsDown(LEFT_ARROW) && playerPosX > 0 + radius) { playerPosX -= playerSpeed; @@ -44,7 +53,6 @@ function keyPressed() { } } - async function Movementloop() { window.addEventListener('booleanArrayUpdated', function (event) { // event.detail contains the booleanArray @@ -80,28 +88,30 @@ async function Movementloop() { function shoot(directionX, directionY) { shot = true; + for (let i; i < projectiles.length; i++) { + if (!projectiles[i]) { + // Set the initial position of the projectile to the player's position + projectiles[i] = createVector(500, 100); + Xbuffer = directionX; + Ybuffer = directionY; + } - if (!projectile) { - // Set the initial position of the projectile to the player's position - projectile = createVector(500, 100); - Xbuffer = directionX; - Ybuffer = directionY; - } + // Draw the small circle (projectile) + fill(255, 0, 0); + circle(projectiles[i].x, projectiles[i].y, projSize); + + // Move the projectile towards the movable circle + let target = createVector(directionX, directionY); + let direction = target.copy().sub(projectiles[i]); + direction.normalize(); + direction.mult(shotSpeed); + projectiles[i].add(direction); + + if (projectiles[i].dist(target) <= radius) { + shot = false; + projectiles[i] = null; + } - // Draw the small circle (projectile) - fill(255, 0, 0); - circle(projectile.x, projectile.y, projSize); - - // Move the projectile towards the movable circle - let target = createVector(Xbuffer, Ybuffer); - let direction = target.copy().sub(projectile); - direction.normalize(); - direction.mult(shotSpeed); - projectile.add(direction); - - if (projectile.dist(target) <= radius) { - shot = false; - projectile = null; } } @@ -119,14 +129,22 @@ function draw() { fill(255,165,0) circle(500, 100, 50); + score(); + text(time, 10, 20); + // square(squareX,squareY,squareSize); // object_collision(); + + + if (shot == false) { - shoot(playerPosX, playerPosY); - + if (time % 100 === 0) { + let newProjectile = shoot(playerPosX, playerPosY); + projectiles.push(newProjectile); + } + // shoot(playerPosX, playerPosY); } else { shot = false; } - } \ No newline at end of file