diff --git a/web/game.js b/web/game.js index ad9e51b..c5f9ede 100644 --- a/web/game.js +++ b/web/game.js @@ -15,9 +15,11 @@ let booleanArray = window.booleanArray; // const squareSize = 100; let shotSpeed = 5; -let projectiles = []; +let projectile; let projSize = 5; let shot; +let shotPosX = 500; +let shotPosY = 100; let time = 0; @@ -88,30 +90,34 @@ 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; - } - - // 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; - } + if (!projectile) { + // Set the initial position of the projectile to the player's position + projectile = createVector(shotPosX, shotPosY); + targetX = directionX; + targetY = directionY; + + // Calculate the initial direction + direction = createVector(targetX - projectile.x, targetY - projectile.y); + direction.normalize(); + } + + // Draw the small circle (projectile) + fill(255, 0, 0); + circle(projectile.x, projectile.y, projSize); + + // Move the projectile towards the movable circle + projectile.add(p5.Vector.mult(direction, shotSpeed)); + + if (dist(projectile.x, projectile.y, targetX, targetY) <= radius) { + if (playerPosX == targetX && playerPosY == targetY) { + projectile = null; + shot = false; + } + // if (projectile.x < 0 || projectile.x > width || projectile.y < 0 || projectile.y > height) { + // projectile = null; + // shot = false; + // } } } @@ -135,15 +141,8 @@ function draw() { // square(squareX,squareY,squareSize); // object_collision(); - - - if (shot == false) { - if (time % 100 === 0) { - let newProjectile = shoot(playerPosX, playerPosY); - projectiles.push(newProjectile); - } - // shoot(playerPosX, playerPosY); + shoot(playerPosX, playerPosY); } else { shot = false; }