Advertisement
Guest User

room.js

a guest
Feb 19th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Room(roomName, outside, obstacles, entryPoint, exitPoint, cols, rows) {
  2.     this.rows = rows;
  3.     this.cols = cols;
  4.     /* name of the room */
  5.     this.roomName = roomName;
  6.     let chestCounter = 0;
  7.     let chests = [];
  8.  
  9.     /* if true sets background to grass else to stone */
  10.     this.outside = outside;
  11.  
  12.     /* arraz of trees and stones */
  13.     this.obstacles = obstacles;
  14.  
  15.     /* start and exitPoint of room  */
  16.     this.entryPoint = entryPoint;
  17.     this.exitPoint = exitPoint;
  18.    /* this.checkIfChestIsOpened = function (chest) {
  19.         return chest.opened;
  20.     } */
  21.     function checkIfChestIsOpened(chest) {
  22.         // console.log(chest.opened);
  23.         return chest.opened;
  24.     }
  25.  
  26.     getTreeImg = function () {
  27.         return document.getElementById("tree");
  28.     }
  29.     getStoneImg = function () {
  30.         return document.getElementById("stone");
  31.     }
  32.     getChestImg = function (chest) {
  33.         if (typeof chest == "undefined") {
  34.             return document.getElementById("chest");
  35.         } else {
  36.             if (checkIfChestIsOpened(chest)) {
  37.                 return document.getElementById("chestOpened");
  38.             } else {
  39.                 return document.getElementById("chest");
  40.             }
  41.         }
  42.     }
  43.  
  44.  
  45.     /* creates Obstacles */
  46.     this.generateObstacles = function (squareSize, bool) {
  47.         for (let i = 0; i < this.obstacles.length; i++) {
  48.             for (let j = 1; j < this.obstacles[i].length; j++) {
  49.                 for (let k = 0; k < this.obstacles[i][j].length; k++) {
  50.                     if (this.obstacles[i][0] === "tree") {
  51.                         let treeIMG = getTreeImg();
  52.                         ctx.drawImage(treeIMG, this.obstacles[i][j][0] * squareSize, this.obstacles[i][j][1] * squareSize, squareSize, squareSize);
  53.                     } else if (this.obstacles[i][0] === "stone") {
  54.                         // ctx.fillStyle = "grey";
  55.                         let stoneIMG = getStoneImg();
  56.                         ctx.drawImage(stoneIMG, this.obstacles[i][j][0] * squareSize, this.obstacles[i][j][1] * squareSize, squareSize, squareSize);
  57.                     } else {
  58.                         if (k != 1) {
  59.                             if (!bool) {
  60.                                 let chest = new Chest(this.obstacles[i][j][0], this.obstacles[i][j][1]);
  61.                                 chests.push(chest);
  62.                                 chestCounter++;
  63.                             }
  64.                             let chestIMG = getChestImg(chest);
  65.                             ctx.drawImage(chestIMG, this.obstacles[i][j][0] * squareSize, this.obstacles[i][j][1] * squareSize, squareSize, squareSize);
  66.  
  67.                         }
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.  
  73.     }
  74.  
  75.     // returns chests to Player
  76.     this.getChests = function () {
  77.         return chests;
  78.     }
  79.  
  80.     /* creates Start and Exit */
  81.     this.getStartEnd = function (squareSize) {
  82.         let startIMG = document.getElementById("start");
  83.         ctx.drawImage(startIMG, this.entryPoint[0] * squareSize, this.entryPoint[1] * squareSize, squareSize, squareSize);
  84.         let doorIMG = document.getElementById("door");
  85.         ctx.drawImage(doorIMG, this.exitPoint[0] * squareSize, this.exitPoint[1] * squareSize, squareSize, squareSize);
  86.     }
  87.  
  88.     this.openDoor = function () {
  89.         // console.log("You open the door!");
  90.         mapCounter++;
  91.         swapRoom = true;
  92.         loadRoom();
  93.     }
  94.  
  95.     /* creates every object in the room */
  96.     this.generateRoom = function (squareSize, bool) {
  97.         this.showBackground(squareSize);
  98.         this.getStartEnd(squareSize);
  99.         this.generateObstacles(squareSize, bool);
  100.     }
  101.  
  102.     // shows background
  103.     this.showBackground = function (squareSize) {
  104.         if (this.outside == false) {
  105.             background = document.getElementById("ground");
  106.         } else {
  107.             background = document.getElementById("grass");
  108.         }
  109.         for (let i = 0; i < cols; i++) {
  110.             for (let j = 0; j < rows; j++) {
  111.                 let x = i * squareSize;
  112.                 let y = j * squareSize;
  113.                 ctx.drawImage(background, x, y, squareSize - 1, squareSize - 1);
  114.             }
  115.         }
  116.     }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement