From 41d0746fe2c9812303767ae1c59c5fedc4a046e1 Mon Sep 17 00:00:00 2001 From: Mees Roelofsz Date: Wed, 17 Jan 2024 14:50:00 +0100 Subject: [PATCH] added collision with boss & first wave can now hit (thx sam) --- web/game.js | 56 ++++++++++++++++++++++++++----------------- web/js/basicbullet.js | 5 ++-- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/web/game.js b/web/game.js index 89d8796..81ab079 100644 --- a/web/game.js +++ b/web/game.js @@ -625,27 +625,31 @@ function spawnRandomBullet() { bulletAmount = 5; } } -// function bouncing() { -// // Update position -// bossPosX += bossVelX; -// bossPosY += bossVelY; -// // Check for bounce -// if (bossPosX > width - 25) { -// bossVelX = random([-4,-5,-6]) -// } -// if (bossPosX < 25) { -// bossVelX = random([4,5,6]) -// } -// if (bossPosY > height - 25) { -// bossVelY = random([-4,-5,-6]) -// } -// if (bossPosY < 25) { -// bossVelY = random([4,5,6]) -// } +function bouncing() { + // Update position + bossPosX += bossVelX; + bossPosY += bossVelY; + // Check for bounce + if (bossPosX > width - 25) { + bossVelX = random([-4,-5,-6]); + } + if (bossPosX < 25) { + bossVelX = random([4,5,6]); + } + if (bossPosY > height - 25) { + bossVelY = random([-4,-5,-6]); + } + if (bossPosY < 25) { + bossVelY = random([4,5,6]); + } -// // Draw the boss -// circle(bossPosX, bossPosY, 50); -// } + // Draw the boss + circle(bossPosX, bossPosY, 50); + + if (dist(bossPosX, bossPosY, playerPosX, playerPosY) <= 25 + radius) { + lives -= 1; + } +} function game() { if (!(lives == 0)) { @@ -656,7 +660,15 @@ function game() { circle(constrain(playerPosX, 0 + radius, width - radius), constrain(playerPosY, 0 + radius, height - radius), playerSize); pop(); movementCheck(); - //bouncing(); + if (phases[2] == false || phase[3] == false) { + bouncing(); + } else if (phases[2] = true) { + bossPosX = x2; + bossPosY = y2; + } else{ + bossPosX = width / 2; + bossPosY = height / 6; + } push(); fill(255, 165, 0) @@ -714,7 +726,7 @@ function game() { if (!pausescreenOn) { bullets.forEach(myBullet => { //zolang mybullet bestaat blijft hij drawen en updaten - ({ hit, shot, isOffScreen, originalPos } = myBullet.update(playerPosX, playerPosY)); + ({ hit, shot, isOffScreen, originalPos } = myBullet.update(playerPosX, playerPosY, hasMoved)); myBullet.draw(); if (isOffScreen == true) { diff --git a/web/js/basicbullet.js b/web/js/basicbullet.js index 5cd6b9c..ba46912 100644 --- a/web/js/basicbullet.js +++ b/web/js/basicbullet.js @@ -11,7 +11,6 @@ class bullet { this.bulletHit = false; this.directionX = null; this.directionY = null; - this.hasMoved = hasMoved; // Set a variable that cant be changed if ((this.directionX === null) || (this.directionY === null)) { this.directionX = this.targetx; @@ -33,7 +32,7 @@ class bullet { pop(); } - update(targetx, targety) { + update(targetx, targety, hasMoved) { // keeps the projetile updated so hit collision can be checked this.targetx = targetx; this.targety = targety; @@ -48,7 +47,7 @@ class bullet { hit = true; shot = false; // if the projectile has hit the player and the player has lives left, remove a life - if (this.hasMoved && lives != 0 && this.bulletHit == false) { + if (hasMoved && lives != 0 && this.bulletHit == false) { this.bulletHit = true; lives -= 1; }