added wait function + delay between random patern

This commit is contained in:
Mees Roelofsz
2023-12-06 13:22:42 +01:00
parent a0b69ecb76
commit cb7448380e

View File

@@ -45,6 +45,7 @@ let x4;
let y4;
let x5;
let y5;
let chosen = false;
let bulletAmount = 10;
@@ -84,6 +85,7 @@ function life() {
hasMoved = false;
bullets = [];
shot = false;
chosen = false;
}
}
@@ -95,7 +97,7 @@ function movementCheck() {
}
}
function keyPressed() {
async function keyPressed() {
if (keyIsDown(LEFT_ARROW) && playerPosX > 0 + radius) {
playerPosX -= playerSpeed;
}
@@ -108,23 +110,15 @@ function keyPressed() {
if (keyIsDown(DOWN_ARROW) && playerPosY < height - radius) {
playerPosY += playerSpeed;
}
if (key == 'e') {
// adding a quick movement button that moves you further in the direction your already moving
playerSpeed += 2;
wait(2);
} else {
playerSpeed = 4;
}
}
function wait(waitTime){
startTime = time;
if (startTime + waitTime > time){
return true;
}
function wait(waitTime) {
return new Promise(resolve => {
setTimeout(() => {
resolve(true);
}, waitTime);
});
}
async function Movementloop() {
window.addEventListener('booleanArrayUpdated', function (event) {
// event.detail contains the booleanArray
@@ -205,6 +199,19 @@ function draw() {
bullets = bullets.filter(bullet => !bullet.hit);
}
}
async function randPatern () {
patern = random([1,2]);
chosen = true;
if (patern == 1) {
await wait(3000);
chosen = false;
}
if (patern == 2) {
await wait(5000);
chosen = false;
}
}
function randomAttackPattern() {
if (shot == false || hit == true) {
@@ -215,8 +222,10 @@ function randomAttackPattern() {
}
function spawnRandomBullet() {
if (chosen == false) {
randPatern();
}
patern = random([1,2]);
if (patern == 1) {
nextAttack = random([1, 2, 3, 4, 5]);
//text(nextAttack, 30, 50);