Tweaks and docs
This commit is contained in:
@@ -2,17 +2,24 @@ import { CharType, TileMap } from "./ascii_tile_map";
|
|||||||
import { Tile, TileChars } from "./ascii_tile_types";
|
import { Tile, TileChars } from "./ascii_tile_types";
|
||||||
import { Orientation } from "./ascii_types";
|
import { Orientation } from "./ascii_types";
|
||||||
|
|
||||||
class DungeonGenerator {
|
/**
|
||||||
constructor(width, height, roomCount) {
|
* @typedef {object} RoomConfig
|
||||||
this.roomCount = roomCount;
|
* @property {number} x
|
||||||
this.rooms = [];
|
* @property {number} y
|
||||||
this.corridors = [];
|
* @property {number} width
|
||||||
|
* @property {number} height
|
||||||
|
*/
|
||||||
|
|
||||||
// 2d array of pure wall tiles
|
/** Dungeon Generator - generates TileMaps populated with rooms, traps, encounters, etc. */
|
||||||
const tiles = new Array(height).fill().map(() => Array(width).fill(Tile.createWall()));
|
class DungeonFactory {
|
||||||
|
/** @type {number} */
|
||||||
|
roomCount;
|
||||||
|
|
||||||
this.map = new TileMap(tiles);
|
/** @type {RoomConfig[]} */
|
||||||
}
|
rooms;
|
||||||
|
|
||||||
|
/** @type {TileMap} */
|
||||||
|
map;
|
||||||
|
|
||||||
get width() {
|
get width() {
|
||||||
return this.map.width;
|
return this.map.width;
|
||||||
@@ -22,6 +29,21 @@ class DungeonGenerator {
|
|||||||
return this.map.height;
|
return this.map.height;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} width
|
||||||
|
* @param {number} height
|
||||||
|
* @param {number} roomCount
|
||||||
|
*/
|
||||||
|
constructor(width, height, roomCount) {
|
||||||
|
this.roomCount = roomCount | 0;
|
||||||
|
this.rooms = [];
|
||||||
|
|
||||||
|
// 2d array of pure wall tiles
|
||||||
|
const tiles = new Array(height | 0).fill().map(() => Array(width | 0).fill(Tile.createWall()));
|
||||||
|
|
||||||
|
this.map = new TileMap(tiles);
|
||||||
|
}
|
||||||
|
|
||||||
generate() {
|
generate() {
|
||||||
this.generateRooms();
|
this.generateRooms();
|
||||||
this.connectRooms();
|
this.connectRooms();
|
||||||
@@ -407,26 +429,35 @@ class DungeonGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @type {HTMLInputElement} */
|
||||||
|
const widthEl = document.getElementById("width");
|
||||||
|
/** @type {HTMLInputElement} */
|
||||||
|
const heightEl = document.getElementById("height");
|
||||||
|
/** @type {HTMLInputElement} */
|
||||||
|
const roomCountEl = document.getElementById("roomCount");
|
||||||
|
/** @type {HTMLElement} */
|
||||||
|
const dungeonDisplayEl = document.getElementById("dungeonDisplay");
|
||||||
|
|
||||||
/** @type {string} */
|
/** @type {string} */
|
||||||
window.currentDungeon = "";
|
export let currentDungeon = "";
|
||||||
|
|
||||||
window.generateDungeon = () => {
|
export const generateDungeon = () => {
|
||||||
const width = parseInt(document.getElementById("width").value);
|
const width = parseInt(widthEl.value);
|
||||||
const height = parseInt(document.getElementById("height").value);
|
const height = parseInt(heightEl.value);
|
||||||
const roomCount = parseInt(document.getElementById("roomCount").value);
|
const roomCount = parseInt(roomCountEl.value);
|
||||||
|
|
||||||
const generator = new DungeonGenerator(width, height, roomCount);
|
const generator = new DungeonFactory(width, height, roomCount);
|
||||||
window.currentDungeon = generator.generate();
|
|
||||||
|
|
||||||
document.getElementById("dungeonDisplay").textContent = window.currentDungeon;
|
currentDungeon = generator.generate();
|
||||||
|
dungeonDisplayEl.textContent = currentDungeon;
|
||||||
};
|
};
|
||||||
|
|
||||||
window.downloadDungeon = () => {
|
export const downloadDungeon = () => {
|
||||||
if (!window.currentDungeon) {
|
if (!currentDungeon) {
|
||||||
window.generateDungeon();
|
generateDungeon();
|
||||||
}
|
}
|
||||||
|
|
||||||
const blob = new Blob([window.currentDungeon], { type: "text/plain" });
|
const blob = new Blob([currentDungeon], { type: "text/plain" });
|
||||||
const url = URL.createObjectURL(blob);
|
const url = URL.createObjectURL(blob);
|
||||||
const a = document.createElement("a");
|
const a = document.createElement("a");
|
||||||
a.href = url;
|
a.href = url;
|
||||||
@@ -437,17 +468,17 @@ window.downloadDungeon = () => {
|
|||||||
URL.revokeObjectURL(url);
|
URL.revokeObjectURL(url);
|
||||||
};
|
};
|
||||||
|
|
||||||
document.getElementById("width").addEventListener("input", function () {
|
widthEl.addEventListener("input", function () {
|
||||||
document.getElementById("widthValue").textContent = this.value;
|
document.getElementById("widthValue").textContent = this.value;
|
||||||
});
|
});
|
||||||
|
|
||||||
document.getElementById("height").addEventListener("input", function () {
|
heightEl.addEventListener("input", function () {
|
||||||
document.getElementById("heightValue").textContent = this.value;
|
document.getElementById("heightValue").textContent = this.value;
|
||||||
});
|
});
|
||||||
|
|
||||||
document.getElementById("roomCount").addEventListener("input", function () {
|
roomCountEl.addEventListener("input", function () {
|
||||||
document.getElementById("roomCountValue").textContent = this.value;
|
document.getElementById("roomCountValue").textContent = this.value;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Generate initial dungeon
|
// Generate initial dungeon
|
||||||
window.generateDungeon();
|
generateDungeon();
|
||||||
|
|||||||
Reference in New Issue
Block a user