added bomb falling, collision, and animation logic

This commit is contained in:
Joshua Perry 2022-04-28 21:35:23 +01:00
parent 5c6cd33f39
commit 1548ffe121
3 changed files with 26 additions and 5 deletions

View File

@ -39,7 +39,8 @@
</div> </div>
</div> </div>
</body> </body>

View File

@ -1,10 +1,12 @@
/* enemy spawning logic */ /* enemy spawning logic */
var aliens = []; var aliens = [];
var bombs = [];
function spawnEnemy() { function spawnEnemy() {
var alien = document.createElement("div"); var alien = document.createElement("div");
alien.className = "alien"; alien.className = "alien";
alien.id = "alien"; alien.id = "alien";
alien.style.zIndex = "2"
while (true) { while (true) {
alien.style.left = Math.floor(Math.random() * document.body.offsetWidth) + "px"; alien.style.left = Math.floor(Math.random() * document.body.offsetWidth) + "px";
if (document.elementFromPoint(alien.offsetLeft, alien.offsetTop).classList.contains("alien") == false) { if (document.elementFromPoint(alien.offsetLeft, alien.offsetTop).classList.contains("alien") == false) {
@ -16,11 +18,28 @@ function spawnEnemy() {
} }
function spawnBomb() { function spawnBomb() {
var alien = aliens[0]; var alien = aliens[Math.floor(Math.random() * aliens.length)];
var bomb = document.createElement("div"); var bomb = document.createElement("div");
bomb.className = "bomb"; bomb.className = "bomb";
bomb.style.left = "28px"; bomb.style.left = "28px";
bomb.style.top = "70px"; bomb.style.top = "70px";
bomb.style.zIndex = "-1"; bomb.style.zIndex = "1";
alien.appendChild(bomb); bomb.style.display = "absolute";
alien.appendChild(bomb);
bombLogic = setInterval(fall, 100);
bombs.push([bomb, bombLogic]);
} }
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) {
if (document.elementFromPoint(element[0].offsetLeft, element[0].offsetTop).classList.contains("sky") != true) {
if(Math.floor(Math.random() * 4) == 3) {
clearInterval(element[1]);
element[0].className = "explosion";
}
}
}
}

View File

@ -1 +1,2 @@
/* logic for bombs */ /* logic for bombs */