added timer and better hit registration

This commit is contained in:
Sam
2023-11-30 13:27:04 +01:00
parent 931824c1c7
commit d703a82a7f

View File

@@ -29,6 +29,7 @@ let predictiveBounceX;
let predictiveBounceY; let predictiveBounceY;
let bullets = []; let bullets = [];
let direction; let direction;
let framerate = 120;
let time = 0; let time = 0;
@@ -38,7 +39,7 @@ function setup() {
// create a canvas element and append it to the body // create a canvas element and append it to the body
createCanvas(width, height); createCanvas(width, height);
frameRate(120); frameRate(framerate);
myBullet = new bullet(playerPosX, playerPosY, radius, shotSpeed); myBullet = new bullet(playerPosX, playerPosY, radius, shotSpeed);
// disable the outline of shapes // disable the outline of shapes
@@ -46,9 +47,11 @@ function setup() {
noStroke(); noStroke();
} }
async function score() { async function score() {
while (true) { while (true) {
time += 1; time += 1 / framerate;
await sleep(1000); await sleep(1000);
} }
} }
@@ -71,7 +74,7 @@ function life() {
time = 0; time = 0;
bounceX = bossPosX; bounceX = bossPosX;
bounceY = bossPosY; bounceY = bossPosY;
hit = false;
} }
} }
@@ -167,13 +170,8 @@ function draw() {
textAlign(LEFT); textAlign(LEFT);
text(time, 10, 20); text(time, 10, 20);
if (shot == false || hit == true) {
spawnRandomBullet()
spawnRandomBullet()
spawnRandomBullet()
spawnRandomBullet()
}
randomAttackPattern()
bullets.forEach(myBullet => { bullets.forEach(myBullet => {
//zolang mybullet bestaat blijft hij drawen en updaten //zolang mybullet bestaat blijft hij drawen en updaten
@@ -181,16 +179,22 @@ function draw() {
myBullet.draw(); myBullet.draw();
}); });
//blijf de bullet tekenen zolang hit false is //blijf de bullet tekenen zolang hit false is
bullets = bullets.filter(bullet => { bullets = bullets.filter(bullet => !bullet.hit);
const { hit, shot } = bullet.update(playerPosX, playerPosY);
bullet.draw();
return !hit; // als hit true is dan wordt de bullet verwijderd
});
} }
} }
function randomAttackPattern(){
if (shot == false || hit == true) {
spawnRandomBullet()
spawnRandomBullet()
spawnRandomBullet()
spawnRandomBullet()
}
}
function spawnRandomBullet(){ function spawnRandomBullet(){
let patern = random([1,2]); let patern = random([1,2]);
if (patern == 1) { if (patern == 1) {
nextAttack = random([1, 2, 3, 4]); nextAttack = random([1, 2, 3, 4]);