Advertisement
RandomGuy32

dungeon.js

Nov 8th, 2019
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. window.onload = init;
  4.  
  5. const cardinalDirections = ["n", "e", "s", "w"];
  6.  
  7. const defaultGetData = {
  8.     method: "GET",
  9.     headers: {
  10.         "Content-Type": "application/json"
  11.     }
  12. };  // I'm lazy.
  13.  
  14. /* ============ DUNGEON CONSTRUCT ============ */
  15.  
  16. let dungeon = {
  17.     player: null,
  18.     currentRoom: null,
  19.     currentDoors: {
  20.         n: null,
  21.         e: null,
  22.         s: null,
  23.         w: null
  24.     },
  25.     map: {
  26.         canvas: null,
  27.         ctx: null,
  28.         rooms: new Array(61),
  29.         posX: 30,
  30.         posY: 30,
  31.         init: function() {
  32.             this.canvas = document.getElementById("dungeon-map");
  33.                        
  34.             this.canvas.setAttribute("width", this.visualSize());
  35.             this.canvas.setAttribute("height", this.visualSize());
  36.            
  37.             this.ctx = this.canvas.getContext("2d");
  38.            
  39.             /* this.rooms.fill(new Array(61));
  40.            
  41.             for (let i in this.rooms) {
  42.                 this.rooms[i].fill(null);
  43.             } */
  44.            
  45.             const boardSize = 61;
  46.            
  47.             for (let i = 0; i < boardSize; i++) {
  48.                 this.rooms[i] = [];
  49.             }
  50.         },
  51.         render: function() {
  52.             this.ctx.clearRect(0, 0, this.visualSize(), this.visualSize());
  53.            
  54.             for (let y = 0; y < this.rooms.length; y++) {
  55.                 for (let x = 0; x < this.rooms[y].length; x++) {
  56.                     if (this.rooms[y][x] != null) {
  57.                         // console.log("Rendering room at (" + x + ", " + y + ")");
  58.                         // console.log("Room is: " + this.rooms[y][x].description);
  59.                        
  60.                         this.ctx.fillStyle = this.rooms[y][x].color;
  61.                        
  62.                         this.ctx.fillRect(this.visualPos(x), this.visualPos(y), this.roomSize() * this.scale(), this.roomSize() * this.scale());
  63.                     }
  64.                 }
  65.             }
  66.         },
  67.         updateCurrentRoom: function(roomData) {
  68.             this.rooms[this.posY][this.posX] = roomData;
  69.         },
  70.         roomSize: function() {
  71.             return 4;
  72.         },
  73.         scale: function() {
  74.             return 2;
  75.         },
  76.         offset: function() {
  77.             return 0.5;
  78.         },
  79.         visualSize: function() {
  80.             return ((this.roomSize() + this.offset()) * this.rooms.length - this.offset()) * this.scale();
  81.         },
  82.         visualPos: function(axis) {
  83.             return ((this.roomSize() + this.offset()) * axis) * this.scale();
  84.         }
  85.     }
  86. };
  87.  
  88. /* ============ INITIALISATION ============ */
  89.  
  90. function init() {  
  91.     dungeon.map.init();
  92.    
  93.     let x = localStorage.getItem("posX");
  94.     let y = localStorage.getItem("posY");
  95.    
  96.     if (x != null) {
  97.         dungeon.map.posX = x;
  98.     }
  99.    
  100.     if (y != null) {
  101.         dungeon.map.posY = y;
  102.     }
  103.    
  104.     document.querySelector("#up button").addEventListener("click", function() { movePlayer("n"); } );
  105.     document.querySelector("#left button").addEventListener("click", function() { movePlayer("w"); } );
  106.     document.querySelector("#right button").addEventListener("click", function() { movePlayer("e"); } );
  107.     document.querySelector("#down button").addEventListener("click", function() { movePlayer("s"); } );
  108.    
  109.     getPlayerInfo();
  110.     getRoomInfo();
  111. }
  112.  
  113. /* ============ FUNCTIONS FOR GETTING INFORMATION ============ */
  114.  
  115. async function getPlayerInfo() {
  116.     const response = await fetch("/api/person", defaultGetData);
  117.     const json = await response.json();
  118.    
  119.     dungeon.player = json;
  120. }
  121.  
  122. async function getRoomInfo() {
  123.     const response = await fetch("/api/position", defaultGetData);
  124.     const json = await response.json();
  125.    
  126.     dungeon.currentRoom = json;
  127.     dungeon.map.updateCurrentRoom(json);
  128.    
  129.     await getAllDoors();
  130.    
  131.     dungeon.map.render();
  132.    
  133.     console.log("Current room: " + dungeon.currentRoom.name + " (" + dungeon.map.posX + ", " + dungeon.map.posY + ")");
  134. }
  135.  
  136. async function getDoorInfo(dir) {
  137.     if (isValidDirection(dir)) {
  138.         try {
  139.             const response = await fetch("/api/door/" + dir, defaultGetData);
  140.            
  141.             if (response.status >= 400) {
  142.                 throw "That ain't good.";   // temporary
  143.             }
  144.            
  145.             const json = await response.json();
  146.        
  147.             dungeon.currentDoors[dir] = json;
  148.         }
  149.         catch (err) {
  150.             console.error(err);
  151.         }
  152.     }
  153.     else {
  154.         console.error("'" + dir + "' is not a valid cardinal direction.");
  155.     }
  156. }
  157.  
  158. async function getAllDoors() {
  159.     dungeon.currentDoors = {"n": null, "s": null, "w": null, "e": null};
  160.    
  161.     for (let i in dungeon.currentRoom.directions) {
  162.         await getDoorInfo(dungeon.currentRoom.directions[i]);
  163.     }
  164.    
  165.     console.log(dungeon.currentDoors);
  166. }
  167.  
  168. async function getChatMessages() {
  169.    
  170. }
  171.  
  172. /* ============ FUNCTIONS FOR SENDING DATA ============ */
  173.  
  174. async function movePlayer(dir) {
  175.     if (isValidDirection(dir)) {
  176.         try {
  177.             const data = {
  178.                 go: dir
  179.             };  // Why doesn't this work?
  180.            
  181.             const request = {
  182.                 method: "PATCH",
  183.                 body: JSON.stringify(data),
  184.                 headers: {
  185.                     "Content-Type": "application/json"
  186.                 }
  187.             };
  188.            
  189.             // console.log("== Request ==");
  190.             // console.log(request);
  191.            
  192.             const response = await fetch("/api/person" + "?go=" + dir, request);
  193.            
  194.             // console.log("== Response ==");
  195.             // console.log(response);
  196.            
  197.             if (response.status >= 400) {
  198.                 throw "You cannot move in that direction."; // temporary
  199.             }
  200.            
  201.             const json = await response.json();
  202.            
  203.             // console.log("== JSON ==");
  204.             // console.log(json);
  205.            
  206.             changePlayerPosition(dir);
  207.            
  208.             getRoomInfo();
  209.         }
  210.         catch (err) {
  211.             console.error(err);
  212.         }
  213.     }
  214.     else {
  215.         console.error("'" + dir + "' is not a valid cardinal direction.");
  216.     }
  217. }
  218.  
  219. /* ============ MISCELLANEOUS NONSENSE ============ */
  220.  
  221. function isValidDirection(dir) {
  222.     return cardinalDirections.includes(dir);
  223. }
  224.  
  225. function changePlayerPosition(dir) {
  226.     switch (dir) {
  227.         case "n":
  228.             dungeon.map.posY--;
  229.             break;
  230.         case "e":
  231.             dungeon.map.posX++;
  232.             break;
  233.         case "s":
  234.             dungeon.map.posY++;
  235.             break;
  236.         case "w":
  237.             dungeon.map.posX--;
  238.             break;
  239.         default:
  240.             console.error("'" + dir + "' is not a valid cardinal direction.");
  241.     }
  242.    
  243.     localStorage.setItem("posX", dungeon.map.posX);
  244.     localStorage.setItem("posY", dungeon.map.posY);
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement