trying to add projectiles into list

This commit is contained in:
Mees Roelofsz
2023-11-26 15:11:18 +01:00
parent 308d4e60ce
commit cd675fe834

View File

@@ -15,10 +15,12 @@ let booleanArray = window.booleanArray;
// const squareSize = 100; // const squareSize = 100;
let shotSpeed = 5; let shotSpeed = 5;
let projectile; let projectiles = [];
let projSize = 5; let projSize = 5;
let shot; let shot;
let time = 0;
// the function setup() is called once when the page is loaded // the function setup() is called once when the page is loaded
function setup() { function setup() {
// create a canvas element and append it to the body // create a canvas element and append it to the body
@@ -29,6 +31,13 @@ function setup() {
noStroke(); noStroke();
} }
async function score() {
while (true) {
time += 1;
await sleep(1000);
}
}
function keyPressed() { function keyPressed() {
if (keyIsDown(LEFT_ARROW) && playerPosX > 0 + radius) { if (keyIsDown(LEFT_ARROW) && playerPosX > 0 + radius) {
playerPosX -= playerSpeed; playerPosX -= playerSpeed;
@@ -44,7 +53,6 @@ function keyPressed() {
} }
} }
async function Movementloop() { async function Movementloop() {
window.addEventListener('booleanArrayUpdated', function (event) { window.addEventListener('booleanArrayUpdated', function (event) {
// event.detail contains the booleanArray // event.detail contains the booleanArray
@@ -80,28 +88,30 @@ async function Movementloop() {
function shoot(directionX, directionY) { function shoot(directionX, directionY) {
shot = true; 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) { // Draw the small circle (projectile)
// Set the initial position of the projectile to the player's position fill(255, 0, 0);
projectile = createVector(500, 100); circle(projectiles[i].x, projectiles[i].y, projSize);
Xbuffer = directionX;
Ybuffer = directionY; // 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) fill(255,165,0)
circle(500, 100, 50); circle(500, 100, 50);
score();
text(time, 10, 20);
// square(squareX,squareY,squareSize); // square(squareX,squareY,squareSize);
// object_collision(); // object_collision();
if (shot == false) { if (shot == false) {
shoot(playerPosX, playerPosY); if (time % 100 === 0) {
let newProjectile = shoot(playerPosX, playerPosY);
projectiles.push(newProjectile);
}
// shoot(playerPosX, playerPosY);
} else { } else {
shot = false; shot = false;
} }
} }