changed collision logic for bombs

This commit is contained in:
Joshua Perry 2022-05-29 12:35:21 +01:00
parent 1e88921226
commit 7c419d8e33
1 changed files with 5 additions and 3 deletions

View File

@ -38,9 +38,11 @@ function spawnBomb() {
function fall() {
for (let element of bombs) {
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(Math.floor(Math.random() * 100) < 10 || element.bottom >= document.body.bottom || closestElement.classList.contains("character")) {
var elemRect = element.getBoundingClientRect();
var bodyRect = document.body.getBoundingClientRect();
var skyRect = document.getElementsByClassName("sky")[0].getBoundingClientRect();
if (elemRect.bottom >= skyRect.bottom || elemRect.bottom <= bodyRect.offsetHeight) {
if(Math.floor(Math.random() * 100) < 10) {
element.className = "explosion";
console.log("Explosion")
setTimeout(() => {element.remove()}, 3000);