diff --git a/assets/script.js b/assets/script.js index cb22ec1..3f3ecf5 100644 --- a/assets/script.js +++ b/assets/script.js @@ -10,18 +10,21 @@ class Game { initializeLevel() { this.mapInstance = new Map(); - this.mapInstance.generateMapArray(); - this.loop(); - } - - loop() { - this.mapInstance.display(); this.mapInstance.getKeystroke(); - // this.mapInstance.waitForInput(); - // this.mapInstance.checkWinCondition(); + this.mapInstance.generateMapArray(); + this.mapInstance.display(); } - handlePoints() { } + reset() { + g.levelMoves[this.level] = 0; + this.lives -= 1; + // handle points + this.initializeLevel(); + } + + continue() {} + + handlePoints() {} } class Map { @@ -34,7 +37,7 @@ class Map { this.playerX = null; this.playerY = null; - this.playerDir = 'f' + this.playerDir = "f"; this.previousElement = " "; this.win = false; @@ -77,35 +80,62 @@ class Map { getKeystroke() { var self = this; - document.addEventListener('keydown', function(event) { - if (event.key === 'h') { - self.playerDir = 'l'; - self.move('left'); - self.display(); - } else if (event.key === 'j') { - self.playerDir = 'f'; - self.move('down'); - self.display(); - } else if (event.key === 'k') { - self.playerDir = 'b'; - self.move('up'); - self.display(); - } else if (event.key === 'l') { - self.playerDir = 'r'; - self.move('right'); - self.display(); + document.addEventListener("keydown", function handler(event) { + console.log("hi"); + if (event.key === "h" || event.key === "a" || event.key === "ArrowLeft") { + self.playerDir = "l"; + self.move("left"); + } else if ( + event.key === "j" || + event.key === "s" || + event.key === "ArrowDown" + ) { + self.playerDir = "f"; + self.move("down"); + } else if ( + event.key === "k" || + event.key === "w" || + event.key === "ArrowUp" + ) { + self.playerDir = "b"; + self.move("up"); + } else if ( + event.key === "l" || + event.key === "d" || + event.key === "ArrowRight" + ) { + self.playerDir = "r"; + self.move("right"); } + self.loop(); + this.removeEventListener("keydown", handler); }); } + loop() { + this.display(); + this.checkWin(); + this.getKeystroke(); + } + display() { let grillChar = false; for (let i = 0; i < this.storageLocations.length; i++) { - if (this.mapArray[this.storageLocations[i][0]][this.storageLocations[i][1]] === ' ') { - this.mapArray[this.storageLocations[i][0]][this.storageLocations[i][1]] = '.'; + if ( + this.mapArray[this.storageLocations[i][0]][ + this.storageLocations[i][1] + ] === " " + ) { + this.mapArray[this.storageLocations[i][0]][ + this.storageLocations[i][1] + ] = "."; } - if (this.mapArray[this.storageLocations[i][0]][this.storageLocations[i][1]] === '@') { + if ( + this.mapArray[this.storageLocations[i][0]][ + this.storageLocations[i][1] + ] === "@" + ) { grillChar = true; } } @@ -135,36 +165,39 @@ class Map { } let mapDiv = document.getElementById("map"); - mapDiv.innerHTML = ''; + mapDiv.innerHTML = ""; mapDiv.innerHTML = map; let statsDiv = document.getElementById("stats"); - statsDiv.innerHTML = `LEVEL: ${this.level} | MOVES: ${g.levelMoves[this.level]} | LIVES: ${g.lives} | POINTS: ${g.points}` + statsDiv.innerHTML = `LEVEL: ${this.level} | MOVES: ${g.levelMoves[this.level]} | LIVES: ${g.lives} | POINTS: ${g.points}`; } move(direction) { g.levelMoves[this.level] += 1; g.totalMoves += 1; - let tmpPos = this.calculateFuturePosition(this.playerX, this.playerY, direction) + let tmpPos = this.calculateFuturePosition( + this.playerX, + this.playerY, + direction, + ); let tmpX = tmpPos[0]; let tmpY = tmpPos[1]; let futurePosition = this.mapArray[tmpY][tmpX]; - if (!['#'].includes(futurePosition)) { + if (!["#"].includes(futurePosition)) { let canMove = true; - if (['0', '*'].includes(futurePosition)) { - futurePosition = ' '; + if (["0", "*"].includes(futurePosition)) { + futurePosition = " "; canMove = this.pushBoulder(tmpY, tmpX, direction); - } if (canMove) { this.mapArray[this.playerY][this.playerX] = this.previousElement; this.previousElement = futurePosition; - this.mapArray[tmpY][tmpX] = '@'; + this.mapArray[tmpY][tmpX] = "@"; this.playerX = tmpX; this.playerY = tmpY; } @@ -172,27 +205,26 @@ class Map { } calculateFuturePosition(x, y, direction) { - let dirList = direction.split(' '); + let dirList = direction.split(" "); - if (dirList.includes('right')) { - x += 1 - } else if (dirList.includes('left')) { - x -= 1 + if (dirList.includes("right")) { + x += 1; + } else if (dirList.includes("left")) { + x -= 1; } - if (dirList.includes('down')) { - y += 1 - } else if (dirList.includes('up')) { - y -= 1 + if (dirList.includes("down")) { + y += 1; + } else if (dirList.includes("up")) { + y -= 1; } - return [x, y] - + return [x, y]; } pushBoulder(tmpY, tmpX, direction) { - if (direction.split(' ').length > 1) { - return false + if (direction.split(" ").length > 1) { + return false; } let tmpBoulderPos = this.calculateFuturePosition(tmpX, tmpY, direction); @@ -201,19 +233,44 @@ class Map { let futurePosition = this.mapArray[tmpBoulderY][tmpBoulderX]; - if (!['#', '0', '*'].includes(futurePosition)) { - if (futurePosition == '.') { - this.mapArray[tmpBoulderY][tmpBoulderX] = '*' + if (!["#", "0", "*"].includes(futurePosition)) { + if (futurePosition == ".") { + this.mapArray[tmpBoulderY][tmpBoulderX] = "*"; } else { - this.mapArray[tmpBoulderY][tmpBoulderX] = '0' + this.mapArray[tmpBoulderY][tmpBoulderX] = "0"; } - return true + return true; } else { - return false + return false; } + } + checkWin() { + this.win = true; + for (let y = 0; y < this.mapArray.length; y++) { + for (let x = 0; x < this.mapArray[y].length; x++) { + if (this.mapArray[y][x] === "0") { + if (!this.storageLocations.includes([x, y])) { + this.win = false; + } + } + } + } + let continueLink = document.getElementById("continueLink"); + if (this.win) { + continueLink.classList.remove("disabled"); + } else { + continueLink.classList.add("disabled"); + } + } + continue() { + // handle points + g.level += 1; + g.initializeLevel(); + let continueLink = document.getElementById("continueLink"); + continueLink.classList.add("disabled"); } }