trying to add projectiles into list
This commit is contained in:
46
web/game.js
46
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;
|
||||
|
||||
if (!projectile) {
|
||||
for (let i; i < projectiles.length; i++) {
|
||||
if (!projectiles[i]) {
|
||||
// Set the initial position of the projectile to the player's position
|
||||
projectile = createVector(500, 100);
|
||||
projectiles[i] = createVector(500, 100);
|
||||
Xbuffer = directionX;
|
||||
Ybuffer = directionY;
|
||||
}
|
||||
|
||||
// Draw the small circle (projectile)
|
||||
fill(255, 0, 0);
|
||||
circle(projectile.x, projectile.y, projSize);
|
||||
circle(projectiles[i].x, projectiles[i].y, projSize);
|
||||
|
||||
// Move the projectile towards the movable circle
|
||||
let target = createVector(Xbuffer, Ybuffer);
|
||||
let direction = target.copy().sub(projectile);
|
||||
let target = createVector(directionX, directionY);
|
||||
let direction = target.copy().sub(projectiles[i]);
|
||||
direction.normalize();
|
||||
direction.mult(shotSpeed);
|
||||
projectile.add(direction);
|
||||
projectiles[i].add(direction);
|
||||
|
||||
if (projectile.dist(target) <= radius) {
|
||||
if (projectiles[i].dist(target) <= radius) {
|
||||
shot = false;
|
||||
projectile = null;
|
||||
projectiles[i] = 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 (shot == false) {
|
||||
if (time % 100 === 0) {
|
||||
let newProjectile = shoot(playerPosX, playerPosY);
|
||||
projectiles.push(newProjectile);
|
||||
}
|
||||
// shoot(playerPosX, playerPosY);
|
||||
} else {
|
||||
shot = false;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user