From 85fd2dcbd5f2c4e25c20a4f28c223e4755450f6c Mon Sep 17 00:00:00 2001 From: Dominic DiTaranto Date: Thu, 4 Sep 2025 15:49:24 -0400 Subject: [PATCH] movement --- assets/script.js | 119 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 113 insertions(+), 6 deletions(-) diff --git a/assets/script.js b/assets/script.js index c51b8b9..94b5359 100644 --- a/assets/script.js +++ b/assets/script.js @@ -1,7 +1,7 @@ class Game { constructor() { this.mapInstance = null; - this.level = 153; + this.level = 0; this.lives = 10; this.points = 0; this.totalMoves = 0; @@ -16,11 +16,12 @@ class Game { loop() { this.mapInstance.display(); + this.mapInstance.getKeystroke(); // this.mapInstance.waitForInput(); // this.mapInstance.checkWinCondition(); } - handlePoints() {} + handlePoints() { } } class Map { @@ -33,6 +34,7 @@ class Map { this.playerX = null; this.playerY = null; + this.playerDir = 'f' this.previousElement = " "; this.win = false; @@ -55,13 +57,13 @@ class Map { let rowElements = levelArray[i].split(""); if (rowElements.includes("@")) { - this.playerX = i - 1; - this.playerY = rowElements.indexOf("@"); + this.playerY = i - 1; + this.playerX = rowElements.indexOf("@"); } let storageTypes = [".", "*"]; for (let j = 0; j < storageTypes.length; j++) { - if (storageTypes[j] in rowElements) { + if (rowElements.includes(storageTypes[j])) { for (let k = 0; k < rowElements.length; k++) { if (rowElements[k] === storageTypes[j]) { this.storageLocations.push([i - 1, k]); @@ -73,7 +75,36 @@ 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(); + } + }); + } + display() { + 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]] = '.'; + } + } + let map = ""; for (let i = 0; i < this.mapArray.length; i++) { for (let j = 0; j < this.mapArray[i].length; j++) { @@ -86,16 +117,92 @@ class Map { } else if (this.mapArray[i][j] === "*") { map += ''; } else if (this.mapArray[i][j] === "@") { - map += ''; + map += ``; } else if (this.mapArray[i][j] === ".") { map += ''; } } map += "
"; } + let mapDiv = document.getElementById("map"); + mapDiv.innerHTML = ''; mapDiv.innerHTML = map; } + + move(direction) { + g.levelMoves[this.level] += 1; + g.totalMoves += 1; + + 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)) { + let canMove = true; + 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.playerX = tmpX; + this.playerY = tmpY; + } + } + } + + calculateFuturePosition(x, y, direction) { + let dirList = direction.split(' '); + + 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 + } + + return [x, y] + + } + + pushBoulder(tmpY, tmpX, direction) { + if (direction.split(' ').length > 1) { + return false + } + + let tmpBoulderPos = this.calculateFuturePosition(tmpX, tmpY, direction); + let tmpBoulderX = tmpBoulderPos[0]; + let tmpBoulderY = tmpBoulderPos[1]; + + let futurePosition = this.mapArray[tmpBoulderY][tmpBoulderX]; + + if (!['#', '0', '*'].includes(futurePosition)) { + if (futurePosition == '.') { + this.mapArray[tmpBoulderY][tmpBoulderX] = '*' + } else { + this.mapArray[tmpBoulderY][tmpBoulderX] = '0' + } + + return true + } else { + return false + } + + + } } let levels = microban_levels;