Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- window.onload = init;
- const cardinalDirections = ["n", "e", "s", "w"];
- const defaultGetData = {
- method: "GET",
- headers: {
- "Content-Type": "application/json"
- }
- }; // I'm lazy.
- /* ============ DUNGEON CONSTRUCT ============ */
- let dungeon = {
- player: null,
- currentRoom: null,
- currentDoors: {
- n: null,
- e: null,
- s: null,
- w: null
- },
- map: {
- canvas: null,
- ctx: null,
- rooms: new Array(61),
- posX: 30,
- posY: 30,
- init: function() {
- this.canvas = document.getElementById("dungeon-map");
- this.canvas.setAttribute("width", this.visualSize());
- this.canvas.setAttribute("height", this.visualSize());
- this.ctx = this.canvas.getContext("2d");
- /* this.rooms.fill(new Array(61));
- for (let i in this.rooms) {
- this.rooms[i].fill(null);
- } */
- const boardSize = 61;
- for (let i = 0; i < boardSize; i++) {
- this.rooms[i] = [];
- }
- },
- render: function() {
- this.ctx.clearRect(0, 0, this.visualSize(), this.visualSize());
- for (let y = 0; y < this.rooms.length; y++) {
- for (let x = 0; x < this.rooms[y].length; x++) {
- if (this.rooms[y][x] != null) {
- // console.log("Rendering room at (" + x + ", " + y + ")");
- // console.log("Room is: " + this.rooms[y][x].description);
- this.ctx.fillStyle = this.rooms[y][x].color;
- this.ctx.fillRect(this.visualPos(x), this.visualPos(y), this.roomSize() * this.scale(), this.roomSize() * this.scale());
- }
- }
- }
- },
- updateCurrentRoom: function(roomData) {
- this.rooms[this.posY][this.posX] = roomData;
- },
- roomSize: function() {
- return 4;
- },
- scale: function() {
- return 2;
- },
- offset: function() {
- return 0.5;
- },
- visualSize: function() {
- return ((this.roomSize() + this.offset()) * this.rooms.length - this.offset()) * this.scale();
- },
- visualPos: function(axis) {
- return ((this.roomSize() + this.offset()) * axis) * this.scale();
- }
- }
- };
- /* ============ INITIALISATION ============ */
- function init() {
- dungeon.map.init();
- let x = localStorage.getItem("posX");
- let y = localStorage.getItem("posY");
- if (x != null) {
- dungeon.map.posX = x;
- }
- if (y != null) {
- dungeon.map.posY = y;
- }
- document.querySelector("#up button").addEventListener("click", function() { movePlayer("n"); } );
- document.querySelector("#left button").addEventListener("click", function() { movePlayer("w"); } );
- document.querySelector("#right button").addEventListener("click", function() { movePlayer("e"); } );
- document.querySelector("#down button").addEventListener("click", function() { movePlayer("s"); } );
- getPlayerInfo();
- getRoomInfo();
- }
- /* ============ FUNCTIONS FOR GETTING INFORMATION ============ */
- async function getPlayerInfo() {
- const response = await fetch("/api/person", defaultGetData);
- const json = await response.json();
- dungeon.player = json;
- }
- async function getRoomInfo() {
- const response = await fetch("/api/position", defaultGetData);
- const json = await response.json();
- dungeon.currentRoom = json;
- dungeon.map.updateCurrentRoom(json);
- await getAllDoors();
- dungeon.map.render();
- console.log("Current room: " + dungeon.currentRoom.name + " (" + dungeon.map.posX + ", " + dungeon.map.posY + ")");
- }
- async function getDoorInfo(dir) {
- if (isValidDirection(dir)) {
- try {
- const response = await fetch("/api/door/" + dir, defaultGetData);
- if (response.status >= 400) {
- throw "That ain't good."; // temporary
- }
- const json = await response.json();
- dungeon.currentDoors[dir] = json;
- }
- catch (err) {
- console.error(err);
- }
- }
- else {
- console.error("'" + dir + "' is not a valid cardinal direction.");
- }
- }
- async function getAllDoors() {
- dungeon.currentDoors = {"n": null, "s": null, "w": null, "e": null};
- for (let i in dungeon.currentRoom.directions) {
- await getDoorInfo(dungeon.currentRoom.directions[i]);
- }
- console.log(dungeon.currentDoors);
- }
- async function getChatMessages() {
- }
- /* ============ FUNCTIONS FOR SENDING DATA ============ */
- async function movePlayer(dir) {
- if (isValidDirection(dir)) {
- try {
- const data = {
- go: dir
- }; // Why doesn't this work?
- const request = {
- method: "PATCH",
- body: JSON.stringify(data),
- headers: {
- "Content-Type": "application/json"
- }
- };
- // console.log("== Request ==");
- // console.log(request);
- const response = await fetch("/api/person" + "?go=" + dir, request);
- // console.log("== Response ==");
- // console.log(response);
- if (response.status >= 400) {
- throw "You cannot move in that direction."; // temporary
- }
- const json = await response.json();
- // console.log("== JSON ==");
- // console.log(json);
- changePlayerPosition(dir);
- getRoomInfo();
- }
- catch (err) {
- console.error(err);
- }
- }
- else {
- console.error("'" + dir + "' is not a valid cardinal direction.");
- }
- }
- /* ============ MISCELLANEOUS NONSENSE ============ */
- function isValidDirection(dir) {
- return cardinalDirections.includes(dir);
- }
- function changePlayerPosition(dir) {
- switch (dir) {
- case "n":
- dungeon.map.posY--;
- break;
- case "e":
- dungeon.map.posX++;
- break;
- case "s":
- dungeon.map.posY++;
- break;
- case "w":
- dungeon.map.posX--;
- break;
- default:
- console.error("'" + dir + "' is not a valid cardinal direction.");
- }
- localStorage.setItem("posX", dungeon.map.posX);
- localStorage.setItem("posY", dungeon.map.posY);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement