Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Room(roomName, outside, obstacles, entryPoint, exitPoint, cols, rows) {
- this.rows = rows;
- this.cols = cols;
- /* name of the room */
- this.roomName = roomName;
- let chestCounter = 0;
- let chests = [];
- /* if true sets background to grass else to stone */
- this.outside = outside;
- /* arraz of trees and stones */
- this.obstacles = obstacles;
- /* start and exitPoint of room */
- this.entryPoint = entryPoint;
- this.exitPoint = exitPoint;
- /* this.checkIfChestIsOpened = function (chest) {
- return chest.opened;
- } */
- function checkIfChestIsOpened(chest) {
- // console.log(chest.opened);
- return chest.opened;
- }
- getTreeImg = function () {
- return document.getElementById("tree");
- }
- getStoneImg = function () {
- return document.getElementById("stone");
- }
- getChestImg = function (chest) {
- if (typeof chest == "undefined") {
- return document.getElementById("chest");
- } else {
- if (checkIfChestIsOpened(chest)) {
- return document.getElementById("chestOpened");
- } else {
- return document.getElementById("chest");
- }
- }
- }
- /* creates Obstacles */
- this.generateObstacles = function (squareSize, bool) {
- for (let i = 0; i < this.obstacles.length; i++) {
- for (let j = 1; j < this.obstacles[i].length; j++) {
- for (let k = 0; k < this.obstacles[i][j].length; k++) {
- if (this.obstacles[i][0] === "tree") {
- let treeIMG = getTreeImg();
- ctx.drawImage(treeIMG, this.obstacles[i][j][0] * squareSize, this.obstacles[i][j][1] * squareSize, squareSize, squareSize);
- } else if (this.obstacles[i][0] === "stone") {
- // ctx.fillStyle = "grey";
- let stoneIMG = getStoneImg();
- ctx.drawImage(stoneIMG, this.obstacles[i][j][0] * squareSize, this.obstacles[i][j][1] * squareSize, squareSize, squareSize);
- } else {
- if (k != 1) {
- if (!bool) {
- let chest = new Chest(this.obstacles[i][j][0], this.obstacles[i][j][1]);
- chests.push(chest);
- chestCounter++;
- }
- let chestIMG = getChestImg(chest);
- ctx.drawImage(chestIMG, this.obstacles[i][j][0] * squareSize, this.obstacles[i][j][1] * squareSize, squareSize, squareSize);
- }
- }
- }
- }
- }
- }
- // returns chests to Player
- this.getChests = function () {
- return chests;
- }
- /* creates Start and Exit */
- this.getStartEnd = function (squareSize) {
- let startIMG = document.getElementById("start");
- ctx.drawImage(startIMG, this.entryPoint[0] * squareSize, this.entryPoint[1] * squareSize, squareSize, squareSize);
- let doorIMG = document.getElementById("door");
- ctx.drawImage(doorIMG, this.exitPoint[0] * squareSize, this.exitPoint[1] * squareSize, squareSize, squareSize);
- }
- this.openDoor = function () {
- // console.log("You open the door!");
- mapCounter++;
- swapRoom = true;
- loadRoom();
- }
- /* creates every object in the room */
- this.generateRoom = function (squareSize, bool) {
- this.showBackground(squareSize);
- this.getStartEnd(squareSize);
- this.generateObstacles(squareSize, bool);
- }
- // shows background
- this.showBackground = function (squareSize) {
- if (this.outside == false) {
- background = document.getElementById("ground");
- } else {
- background = document.getElementById("grass");
- }
- for (let i = 0; i < cols; i++) {
- for (let j = 0; j < rows; j++) {
- let x = i * squareSize;
- let y = j * squareSize;
- ctx.drawImage(background, x, y, squareSize - 1, squareSize - 1);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement