commit
fbb628447c
|
|
@ -8,6 +8,7 @@ function setVars() {
|
||||||
|
|
||||||
document.onloadend = setVars;
|
document.onloadend = setVars;
|
||||||
|
|
||||||
|
/*Controls vertical movement*/
|
||||||
function verticalMovement(keyCode) {
|
function verticalMovement(keyCode) {
|
||||||
var playerLeft = player.offsetLeft;
|
var playerLeft = player.offsetLeft;
|
||||||
var playerTop = player.offsetTop;
|
var playerTop = player.offsetTop;
|
||||||
|
|
@ -33,6 +34,7 @@ function verticalMovement(keyCode) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Controls horizontal movement*/
|
||||||
function horizontalMovement(keyCode) {
|
function horizontalMovement(keyCode) {
|
||||||
var playerLeft = player.offsetLeft;
|
var playerLeft = player.offsetLeft;
|
||||||
var playerTop = player.offsetTop;
|
var playerTop = player.offsetTop;
|
||||||
|
|
@ -58,6 +60,7 @@ function horizontalMovement(keyCode) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Controls movement interval*/
|
||||||
function move() {
|
function move() {
|
||||||
if (currentKey == 0) {
|
if (currentKey == 0) {
|
||||||
verticalMovement(lastKey);
|
verticalMovement(lastKey);
|
||||||
|
|
@ -73,11 +76,13 @@ function move() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Gets triggered keystroke*/
|
||||||
function getKey(event) {
|
function getKey(event) {
|
||||||
lastKey = currentKey;
|
lastKey = currentKey;
|
||||||
currentKey = event.keyCode;
|
currentKey = event.keyCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Identifies if no keys are pressed*/
|
||||||
function stop(event) {
|
function stop(event) {
|
||||||
if (currentKey == lastKey) {
|
if (currentKey == lastKey) {
|
||||||
currentKey = 0;
|
currentKey = 0;
|
||||||
|
|
|
||||||
|
|
@ -1,26 +1,67 @@
|
||||||
/* enemy spawning logic */
|
/* enemy spawning logic */
|
||||||
var aliens = [];
|
var aliens = [];
|
||||||
|
var bombs = [];
|
||||||
|
|
||||||
|
/*Spawns Enemies*/
|
||||||
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) {
|
||||||
document.body.appendChild(alien);
|
document.body.appendChild(alien);
|
||||||
aliens.push(alien);
|
alienLogic = setInterval(spawnBomb, 3000);
|
||||||
|
aliens.push([alien, alienLogic]);
|
||||||
|
console.log("Alien Spawned")
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Spawns a bomb*/
|
||||||
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[0].appendChild(bomb);
|
||||||
|
bombLogic = setInterval(fall, 100);
|
||||||
|
bombs.push([bomb, bombLogic]);
|
||||||
|
console.log("Bomb Spawned")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Makes a random bomb fall a set amount and explodes if colliding with the floor*/
|
||||||
|
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) {
|
||||||
|
var closestElement = document.elementFromPoint(element[0].offsetLeft, element[0].offsetTop+10);
|
||||||
|
if (!closestElement.classList.contains("sky") && !closestElement.classList.contains("alien") && !closestElement.classList.contains("bomb") && !closestElement.classList.contains("explosion")) {
|
||||||
|
if(Math.floor(Math.random() * 2) == 1) {
|
||||||
|
clearInterval(element[1]);
|
||||||
|
element[0].className = "explosion";
|
||||||
|
console.log("Explosion")
|
||||||
|
setTimeout(() => {element[0].remove()}, 3000);
|
||||||
|
bombs.splice(bombs.indexOf(element),1);
|
||||||
|
console.log("Bomb Despawned");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*Checks to see if player is colliding with exploded bomb*/
|
||||||
|
function checkExplosion() {
|
||||||
|
for (let element of document.getElementsByClassName("explosion")) {
|
||||||
|
var elemRect = element.getBoundingClientRect();
|
||||||
|
var playerRect = document.getElementById("player").getBoundingClientRect();
|
||||||
|
if (elemRect.bottom >= playerRect.top && elemRect.right >= playerRect.left && elemRect.left <= playerRect.right && elemRect.top <= playerRect.bottom) {
|
||||||
|
document.getElementById("player").style.display = "none";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,6 +6,7 @@ function showDisplay(mode) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Registers interval functions and trigger functions*/
|
||||||
function load() {
|
function load() {
|
||||||
document.addEventListener("keydown", getKey)
|
document.addEventListener("keydown", getKey)
|
||||||
document.addEventListener("keyup", stop)
|
document.addEventListener("keyup", stop)
|
||||||
|
|
@ -16,9 +17,13 @@ function load() {
|
||||||
document.getElementById("start").style.display = "block";
|
document.getElementById("start").style.display = "block";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Starts game functionality*/
|
||||||
function startGame() {
|
function startGame() {
|
||||||
showDisplay("block");
|
showDisplay("block");
|
||||||
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";
|
||||||
|
setInterval(spawnEnemy, 2500);
|
||||||
|
setInterval(checkExplosion, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener("DOMContentLoaded", load);
|
document.addEventListener("DOMContentLoaded", load);
|
||||||
|
|
|
||||||
|
|
@ -1 +1,2 @@
|
||||||
/* logic for bombs */
|
/* logic for bombs */
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue