testing
This commit is contained in:
		
							parent
							
								
									de6f46b36d
								
							
						
					
					
						commit
						69e1912b25
					
				
					 1 changed files with 114 additions and 57 deletions
				
			
		
							
								
								
									
										171
									
								
								assets/script.js
									
									
									
									
									
								
							
							
						
						
									
										171
									
								
								assets/script.js
									
									
									
									
									
								
							| 
						 | 
					@ -10,18 +10,21 @@ class Game {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  initializeLevel() {
 | 
					  initializeLevel() {
 | 
				
			||||||
    this.mapInstance = new Map();
 | 
					    this.mapInstance = new Map();
 | 
				
			||||||
    this.mapInstance.generateMapArray();
 | 
					 | 
				
			||||||
    this.loop();
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  loop() {
 | 
					 | 
				
			||||||
    this.mapInstance.display();
 | 
					 | 
				
			||||||
    this.mapInstance.getKeystroke();
 | 
					    this.mapInstance.getKeystroke();
 | 
				
			||||||
    // this.mapInstance.waitForInput();
 | 
					    this.mapInstance.generateMapArray();
 | 
				
			||||||
    // this.mapInstance.checkWinCondition();
 | 
					    this.mapInstance.display();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  handlePoints() { }
 | 
					  reset() {
 | 
				
			||||||
 | 
					    g.levelMoves[this.level] = 0;
 | 
				
			||||||
 | 
					    this.lives -= 1;
 | 
				
			||||||
 | 
					    // handle points
 | 
				
			||||||
 | 
					    this.initializeLevel();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  continue() {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  handlePoints() {}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Map {
 | 
					class Map {
 | 
				
			||||||
| 
						 | 
					@ -34,7 +37,7 @@ class Map {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this.playerX = null;
 | 
					    this.playerX = null;
 | 
				
			||||||
    this.playerY = null;
 | 
					    this.playerY = null;
 | 
				
			||||||
    this.playerDir = 'f'
 | 
					    this.playerDir = "f";
 | 
				
			||||||
    this.previousElement = " ";
 | 
					    this.previousElement = " ";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this.win = false;
 | 
					    this.win = false;
 | 
				
			||||||
| 
						 | 
					@ -77,35 +80,62 @@ class Map {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  getKeystroke() {
 | 
					  getKeystroke() {
 | 
				
			||||||
    var self = this;
 | 
					    var self = this;
 | 
				
			||||||
    document.addEventListener('keydown', function(event) {
 | 
					    document.addEventListener("keydown", function handler(event) {
 | 
				
			||||||
      if (event.key === 'h') {
 | 
					      console.log("hi");
 | 
				
			||||||
        self.playerDir = 'l';
 | 
					      if (event.key === "h" || event.key === "a" || event.key === "ArrowLeft") {
 | 
				
			||||||
        self.move('left');
 | 
					        self.playerDir = "l";
 | 
				
			||||||
        self.display();
 | 
					        self.move("left");
 | 
				
			||||||
      } else if (event.key === 'j') {
 | 
					      } else if (
 | 
				
			||||||
        self.playerDir = 'f';
 | 
					        event.key === "j" ||
 | 
				
			||||||
        self.move('down');
 | 
					        event.key === "s" ||
 | 
				
			||||||
        self.display();
 | 
					        event.key === "ArrowDown"
 | 
				
			||||||
      } else if (event.key === 'k') {
 | 
					      ) {
 | 
				
			||||||
        self.playerDir = 'b';
 | 
					        self.playerDir = "f";
 | 
				
			||||||
        self.move('up');
 | 
					        self.move("down");
 | 
				
			||||||
        self.display();
 | 
					      } else if (
 | 
				
			||||||
      } else if (event.key === 'l') {
 | 
					        event.key === "k" ||
 | 
				
			||||||
        self.playerDir = 'r';
 | 
					        event.key === "w" ||
 | 
				
			||||||
        self.move('right');
 | 
					        event.key === "ArrowUp"
 | 
				
			||||||
        self.display();
 | 
					      ) {
 | 
				
			||||||
 | 
					        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() {
 | 
					  display() {
 | 
				
			||||||
    let grillChar = false;
 | 
					    let grillChar = false;
 | 
				
			||||||
    for (let i = 0; i < this.storageLocations.length; i++) {
 | 
					    for (let i = 0; i < this.storageLocations.length; i++) {
 | 
				
			||||||
      if (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]
 | 
				
			||||||
 | 
					        ] === " "
 | 
				
			||||||
 | 
					      ) {
 | 
				
			||||||
 | 
					        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;
 | 
					        grillChar = true;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					@ -135,36 +165,39 @@ class Map {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mapDiv = document.getElementById("map");
 | 
					    let mapDiv = document.getElementById("map");
 | 
				
			||||||
    mapDiv.innerHTML = '';
 | 
					    mapDiv.innerHTML = "";
 | 
				
			||||||
    mapDiv.innerHTML = map;
 | 
					    mapDiv.innerHTML = map;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let statsDiv = document.getElementById("stats");
 | 
					    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) {
 | 
					  move(direction) {
 | 
				
			||||||
    g.levelMoves[this.level] += 1;
 | 
					    g.levelMoves[this.level] += 1;
 | 
				
			||||||
    g.totalMoves += 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 tmpX = tmpPos[0];
 | 
				
			||||||
    let tmpY = tmpPos[1];
 | 
					    let tmpY = tmpPos[1];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let futurePosition = this.mapArray[tmpY][tmpX];
 | 
					    let futurePosition = this.mapArray[tmpY][tmpX];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!['#'].includes(futurePosition)) {
 | 
					    if (!["#"].includes(futurePosition)) {
 | 
				
			||||||
      let canMove = true;
 | 
					      let canMove = true;
 | 
				
			||||||
      if (['0', '*'].includes(futurePosition)) {
 | 
					      if (["0", "*"].includes(futurePosition)) {
 | 
				
			||||||
        futurePosition = ' ';
 | 
					        futurePosition = " ";
 | 
				
			||||||
        canMove = this.pushBoulder(tmpY, tmpX, direction);
 | 
					        canMove = this.pushBoulder(tmpY, tmpX, direction);
 | 
				
			||||||
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if (canMove) {
 | 
					      if (canMove) {
 | 
				
			||||||
        this.mapArray[this.playerY][this.playerX] = this.previousElement;
 | 
					        this.mapArray[this.playerY][this.playerX] = this.previousElement;
 | 
				
			||||||
        this.previousElement = futurePosition;
 | 
					        this.previousElement = futurePosition;
 | 
				
			||||||
        this.mapArray[tmpY][tmpX] = '@';
 | 
					        this.mapArray[tmpY][tmpX] = "@";
 | 
				
			||||||
        this.playerX = tmpX;
 | 
					        this.playerX = tmpX;
 | 
				
			||||||
        this.playerY = tmpY;
 | 
					        this.playerY = tmpY;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
| 
						 | 
					@ -172,27 +205,26 @@ class Map {
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  calculateFuturePosition(x, y, direction) {
 | 
					  calculateFuturePosition(x, y, direction) {
 | 
				
			||||||
    let dirList = direction.split(' ');
 | 
					    let dirList = direction.split(" ");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (dirList.includes('right')) {
 | 
					    if (dirList.includes("right")) {
 | 
				
			||||||
      x += 1
 | 
					      x += 1;
 | 
				
			||||||
    } else if (dirList.includes('left')) {
 | 
					    } else if (dirList.includes("left")) {
 | 
				
			||||||
      x -= 1
 | 
					      x -= 1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (dirList.includes('down')) {
 | 
					    if (dirList.includes("down")) {
 | 
				
			||||||
      y += 1
 | 
					      y += 1;
 | 
				
			||||||
    } else if (dirList.includes('up')) {
 | 
					    } else if (dirList.includes("up")) {
 | 
				
			||||||
      y -= 1
 | 
					      y -= 1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return [x, y]
 | 
					    return [x, y];
 | 
				
			||||||
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  pushBoulder(tmpY, tmpX, direction) {
 | 
					  pushBoulder(tmpY, tmpX, direction) {
 | 
				
			||||||
    if (direction.split(' ').length > 1) {
 | 
					    if (direction.split(" ").length > 1) {
 | 
				
			||||||
      return false
 | 
					      return false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let tmpBoulderPos = this.calculateFuturePosition(tmpX, tmpY, direction);
 | 
					    let tmpBoulderPos = this.calculateFuturePosition(tmpX, tmpY, direction);
 | 
				
			||||||
| 
						 | 
					@ -201,19 +233,44 @@ class Map {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let futurePosition = this.mapArray[tmpBoulderY][tmpBoulderX];
 | 
					    let futurePosition = this.mapArray[tmpBoulderY][tmpBoulderX];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!['#', '0', '*'].includes(futurePosition)) {
 | 
					    if (!["#", "0", "*"].includes(futurePosition)) {
 | 
				
			||||||
      if (futurePosition == '.') {
 | 
					      if (futurePosition == ".") {
 | 
				
			||||||
        this.mapArray[tmpBoulderY][tmpBoulderX] = '*'
 | 
					        this.mapArray[tmpBoulderY][tmpBoulderX] = "*";
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
        this.mapArray[tmpBoulderY][tmpBoulderX] = '0'
 | 
					        this.mapArray[tmpBoulderY][tmpBoulderX] = "0";
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      return true
 | 
					      return true;
 | 
				
			||||||
    } else {
 | 
					    } 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");
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue