From 51ec7f09bb7938b90a7dc7265f792643fc00d750 Mon Sep 17 00:00:00 2001 From: Mees Roelofsz Date: Sat, 25 Nov 2023 17:05:49 +0100 Subject: [PATCH] trying to shoot out projectiles (not working) --- web/game.js | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/web/game.js b/web/game.js index 6b26d0c..d905635 100644 --- a/web/game.js +++ b/web/game.js @@ -75,15 +75,18 @@ const height = 600; let Playerposx = 500; let Playerposy = 300; const playerSize = 10; -radius = playerSize/2; -playerX = 500 -playerY = 300; -playerSpeed = 3; +let radius = playerSize/2; +let playerX = 500 +let playerY = 300; +let playerSpeed = 3; -squareX = 100; -squareY = 100; +let squareX = 100; +let squareY = 100; const squareSize = 100; +let startPosX; +let startPosY; +let shotSpeed = 5; // the function setup() is called once when the page is loaded function setup() { @@ -110,7 +113,6 @@ function keyPressed() { } } - async function Movementloop() { if (booleanArray[1]) { playerX += playerSpeed;} @@ -120,7 +122,7 @@ async function Movementloop() { playerY += playerSpeed;} if (booleanArray[0]) { playerY -= playerSpeed;} - } +} function object_collision() { var squareCenterX = squareX + squareSize / 2; @@ -134,11 +136,26 @@ function object_collision() { } } +async function shoot(startPosX, startPosY) { + circle(startPosX, startPosY, 30); + let shootingCirclePos = createVector(startPosX, startPosY); + let targetCirclePos = createVector(playerX, playerY); + + let direction = p5.Vector.sub(targetCirclePos, shootingCirclePos); + direction.normalize(); + direction.mult(shotSpeed); + + shootingCirclePos.add(direction); + + circle(shootingCirclePos.x, shootingCirclePos.y, 5); +} + // the function draw() is called every frame function draw() { keyPressed(); // clear the background with a transparent black color background(0, 0, 0, 100); + shoot(500,100); // draw a circle at the mouse position