random height to bombs, adjusted death trigger

This commit is contained in:
Joshua Perry 2022-04-29 11:44:48 +01:00
parent 4c744b8269
commit 729056c200
2 changed files with 10 additions and 13 deletions

View File

@ -30,28 +30,24 @@ function spawnBomb() {
bomb.style.zIndex = "1"; bomb.style.zIndex = "1";
bomb.style.display = "absolute"; bomb.style.display = "absolute";
alien[0].appendChild(bomb); alien[0].appendChild(bomb);
bombLogic = setInterval(fall, 100); bombs.push(bomb);
bombs.push([bomb, bombLogic]);
console.log("Bomb Spawned") console.log("Bomb Spawned")
} }
/*Makes a random bomb fall a set amount and explodes if colliding with the floor*/ /*Makes a random bomb fall a set amount and explodes if colliding with the floor*/
function fall() { function fall() {
var bomb = bombs[Math.floor(Math.random() * bombs.length)];
bomb[0].style.top = (bomb[0].offsetTop + 10) + "px";
for (let element of bombs) { for (let element of bombs) {
var closestElement = document.elementFromPoint(element[0].offsetLeft, element[0].offsetTop+10); element.style.top = (element.offsetTop + 10) + "px";
var closestElement = document.elementFromPoint(element.offsetLeft, element.offsetTop+10);
if (!closestElement.classList.contains("sky") && !closestElement.classList.contains("alien") && !closestElement.classList.contains("bomb") && !closestElement.classList.contains("explosion")) { if (!closestElement.classList.contains("sky") && !closestElement.classList.contains("alien") && !closestElement.classList.contains("bomb") && !closestElement.classList.contains("explosion")) {
if(Math.floor(Math.random() * 2) == 1) { if(Math.floor(Math.random() * 100) < 10 || element.bottom >= document.body.bottom) {
clearInterval(element[1]); element.className = "explosion";
element[0].className = "explosion";
console.log("Explosion") console.log("Explosion")
setTimeout(() => {element[0].remove()}, 3000); setTimeout(() => {element.remove()}, 3000);
bombs.splice(bombs.indexOf(element),1); bombs.splice(bombs.indexOf(element),1);
console.log("Bomb Despawned"); console.log("Bomb Despawned");
} }
} }
} }
} }
@ -60,7 +56,7 @@ function checkExplosion() {
for (let element of document.getElementsByClassName("explosion")) { for (let element of document.getElementsByClassName("explosion")) {
var elemRect = element.getBoundingClientRect(); var elemRect = element.getBoundingClientRect();
var playerRect = document.getElementById("player").getBoundingClientRect(); var playerRect = document.getElementById("player").getBoundingClientRect();
if (elemRect.bottom >= playerRect.top && elemRect.right >= playerRect.left && elemRect.left <= playerRect.right && elemRect.top <= playerRect.bottom) { if (elemRect.bottom >= playerRect.top && elemRect.right >= playerRect.left && elemRect.left <= playerRect.right && elemRect.top-40 <= playerRect.bottom) {
document.getElementById("player").className = "character dead"; document.getElementById("player").className = "character dead";
endGame(); endGame();
} }

View File

@ -24,7 +24,8 @@ function startGame() {
document.getElementById("start").style.display = "none"; document.getElementById("start").style.display = "none";
document.getElementsByClassName("weapon")[0].style.display = "none"; document.getElementsByClassName("weapon")[0].style.display = "none";
intervals.push(setInterval(spawnEnemy, 2500)); intervals.push(setInterval(spawnEnemy, 2500));
intervals.push(setInterval(checkExplosion, 10)); setTimeout(intervals.push(setInterval(fall, 50)), 2500);
intervals.push(setInterval(checkExplosion, 1));
} }
function endGame() { function endGame() {