refactor
This commit is contained in:
@@ -13,35 +13,47 @@ import { Orientation } from "./ascii_types";
|
||||
/** Dungeon Generator - generates TileMaps populated with rooms, traps, encounters, etc. */
|
||||
class DungeonFactory {
|
||||
/** @type {number} */
|
||||
roomCount;
|
||||
#roomCount;
|
||||
|
||||
/** @type {RoomConfig[]} */
|
||||
rooms;
|
||||
#rooms;
|
||||
|
||||
/** @type {TileMap} */
|
||||
map;
|
||||
#map;
|
||||
|
||||
get roomCount() {
|
||||
return this.#roomCount;
|
||||
}
|
||||
|
||||
get rooms() {
|
||||
return this.#rooms;
|
||||
}
|
||||
|
||||
get map() {
|
||||
return this.#map;
|
||||
}
|
||||
|
||||
get width() {
|
||||
return this.map.width;
|
||||
return this.#map.width;
|
||||
}
|
||||
|
||||
get height() {
|
||||
return this.map.height;
|
||||
return this.#map.height;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} width
|
||||
* @param {number} height
|
||||
* @param {number} roomCount
|
||||
* @param {number} #roomCount
|
||||
*/
|
||||
constructor(width, height, roomCount) {
|
||||
this.roomCount = roomCount | 0;
|
||||
this.rooms = [];
|
||||
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);
|
||||
this.#map = new TileMap(tiles);
|
||||
}
|
||||
|
||||
generate() {
|
||||
@@ -52,18 +64,18 @@ class DungeonFactory {
|
||||
this.addFeatures();
|
||||
this.addPlayerStart();
|
||||
this.addPortals();
|
||||
return this.map.toString(CharType.TYPE_ID);
|
||||
return this.#map.toString(CharType.TYPE_ID);
|
||||
}
|
||||
|
||||
generateRooms() {
|
||||
this.rooms = [];
|
||||
const maxAttempts = this.roomCount * 10;
|
||||
this.#rooms = [];
|
||||
const maxAttempts = this.#roomCount * 10;
|
||||
let attempts = 0;
|
||||
|
||||
while (this.rooms.length < this.roomCount && attempts < maxAttempts) {
|
||||
while (this.#rooms.length < this.#roomCount && attempts < maxAttempts) {
|
||||
const room = this.generateRoom();
|
||||
if (room && !this.roomOverlaps(room)) {
|
||||
this.rooms.push(room);
|
||||
this.#rooms.push(room);
|
||||
this.carveRoom(room);
|
||||
}
|
||||
attempts++;
|
||||
@@ -83,7 +95,7 @@ class DungeonFactory {
|
||||
}
|
||||
|
||||
roomOverlaps(newRoom) {
|
||||
return this.rooms.some(
|
||||
return this.#rooms.some(
|
||||
(room) =>
|
||||
newRoom.x < room.x + room.width + 2 &&
|
||||
newRoom.x + newRoom.width + 2 > room.x &&
|
||||
@@ -95,26 +107,26 @@ class DungeonFactory {
|
||||
carveRoom(room) {
|
||||
for (let y = room.y; y < room.y + room.height; y++) {
|
||||
for (let x = room.x; x < room.x + room.width; x++) {
|
||||
this.map.tiles[y][x] = Tile.createFloor();
|
||||
this.#map.tiles[y][x] = Tile.createFloor();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
connectRooms() {
|
||||
if (this.rooms.length < 2) return;
|
||||
if (this.#rooms.length < 2) return;
|
||||
|
||||
// Connect each room to at least one other room
|
||||
for (let i = 1; i < this.rooms.length >> 1; i++) {
|
||||
const roomA = this.rooms[i - 1];
|
||||
const roomB = this.rooms[i];
|
||||
for (let i = 1; i < this.#rooms.length >> 1; i++) {
|
||||
const roomA = this.#rooms[i - 1];
|
||||
const roomB = this.#rooms[i];
|
||||
this.createCorridor(roomA, roomB);
|
||||
}
|
||||
|
||||
// Add some extra connections for more interesting layouts
|
||||
const extraConnections = Math.floor(this.rooms.length / 3);
|
||||
const extraConnections = Math.floor(this.#rooms.length / 3);
|
||||
for (let i = 0; i < extraConnections; i++) {
|
||||
const roomA = this.rooms[this.random(0, this.rooms.length - 1)];
|
||||
const roomB = this.rooms[this.random(0, this.rooms.length - 1)];
|
||||
const roomA = this.#rooms[this.random(0, this.#rooms.length - 1)];
|
||||
const roomB = this.#rooms[this.random(0, this.#rooms.length - 1)];
|
||||
if (roomA !== roomB) {
|
||||
this.createCorridor(roomA, roomB);
|
||||
}
|
||||
@@ -138,7 +150,7 @@ class DungeonFactory {
|
||||
|
||||
for (let x = 0; x < this.width; x++) {
|
||||
//
|
||||
if (this.map.get(x, y).isWall()) {
|
||||
if (this.#map.get(x, y).isWall()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -180,7 +192,7 @@ class DungeonFactory {
|
||||
row.push(Tile.createWall()); // Initial wall tile on this row
|
||||
for (let x = dungeonStartX; x <= dungeonEndX; x++) {
|
||||
/**/
|
||||
const tile = this.map.get(x, y);
|
||||
const tile = this.#map.get(x, y);
|
||||
row.push(tile);
|
||||
}
|
||||
row.push(Tile.createWall()); // Final wall tile on this row
|
||||
@@ -190,7 +202,7 @@ class DungeonFactory {
|
||||
// Final row is all walls
|
||||
newTiles.push(new Array(newWidth).fill(Tile.createWall()));
|
||||
|
||||
this.map = new TileMap(newTiles);
|
||||
this.#map = new TileMap(newTiles);
|
||||
}
|
||||
|
||||
createCorridor(roomA, roomB) {
|
||||
@@ -220,7 +232,7 @@ class DungeonFactory {
|
||||
|
||||
while (x !== x2 || y !== y2) {
|
||||
if (x >= 0 && x < this.width && y >= 0 && y < this.height) {
|
||||
this.map.tiles[y][x] = Tile.createFloor();
|
||||
this.#map.tiles[y][x] = Tile.createFloor();
|
||||
}
|
||||
|
||||
if (x !== x2) x += dx;
|
||||
@@ -229,7 +241,7 @@ class DungeonFactory {
|
||||
|
||||
// Ensure endpoint is carved
|
||||
if (x2 >= 0 && x2 < this.width && y2 >= 0 && y2 < this.height) {
|
||||
this.map.tiles[y2][x2] = Tile.createFloor();
|
||||
this.#map.tiles[y2][x2] = Tile.createFloor();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,14 +250,14 @@ class DungeonFactory {
|
||||
for (let y = 1; y < this.height - 1; y++) {
|
||||
//
|
||||
for (let x = 1; x < this.width - 1; x++) {
|
||||
const cell = this.map.get(x, y);
|
||||
const cell = this.#map.get(x, y);
|
||||
|
||||
if (!cell) {
|
||||
console.warn("out of bounds [%d, %d] (%s)", x, y, typeof cell);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (this.map.get(x, y).isFloor()) {
|
||||
if (this.#map.get(x, y).isFloor()) {
|
||||
walkabilityCache.push([x, y]);
|
||||
}
|
||||
}
|
||||
@@ -263,7 +275,7 @@ class DungeonFactory {
|
||||
|
||||
for (let [x, y] of walkabilityCache) {
|
||||
//
|
||||
const walkable = (offsetX, offsetY) => this.map.isFloorLike(x + offsetX, y + offsetY);
|
||||
const walkable = (offsetX, offsetY) => this.#map.isFloorLike(x + offsetX, y + offsetY);
|
||||
|
||||
const surroundingFloorCount =
|
||||
0 +
|
||||
@@ -283,7 +295,7 @@ class DungeonFactory {
|
||||
|
||||
if (surroundingFloorCount >= 7) {
|
||||
// MAGIC NUMBER 7
|
||||
this.map.tiles[y][x] = Tile.createWall();
|
||||
this.#map.tiles[y][x] = Tile.createWall();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -293,14 +305,14 @@ class DungeonFactory {
|
||||
for (let y = 1; y < this.height - 1; y++) {
|
||||
//
|
||||
for (let x = 1; x < this.width - 1; x++) {
|
||||
const cell = this.map.get(x, y);
|
||||
const cell = this.#map.get(x, y);
|
||||
|
||||
if (!cell) {
|
||||
console.warn("out of bounds [%d, %d] (%s)", x, y, typeof cell);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (this.map.isFloorLike(x, y)) {
|
||||
if (this.#map.isFloorLike(x, y)) {
|
||||
walkabilityCache.push([x, y]);
|
||||
}
|
||||
}
|
||||
@@ -309,7 +321,7 @@ class DungeonFactory {
|
||||
const idx = this.random(0, walkabilityCache.length - 1);
|
||||
const [x, y] = walkabilityCache[idx];
|
||||
|
||||
const walkable = (offsetX, offsetY) => this.map.isFloorLike(x + offsetX, y + offsetY);
|
||||
const walkable = (offsetX, offsetY) => this.#map.isFloorLike(x + offsetX, y + offsetY);
|
||||
|
||||
//
|
||||
// When spawning in, which direction should the player be oriented?
|
||||
@@ -324,23 +336,23 @@ class DungeonFactory {
|
||||
// they don't face a wall upon spawning.
|
||||
const dirIdx = this.random(0, directions.length - 1);
|
||||
|
||||
this.map.tiles[y][x] = Tile.createPlayerStart(directions[dirIdx]);
|
||||
this.#map.tiles[y][x] = Tile.createPlayerStart(directions[dirIdx]);
|
||||
}
|
||||
|
||||
// Add portals to isolated areas
|
||||
addPortals() {
|
||||
let traversableTileCount = this.map.getFloorlikeTileCount();
|
||||
let traversableTileCount = this.#map.getFloorlikeTileCount();
|
||||
|
||||
//
|
||||
// Find the player's start point, and let this be the
|
||||
// bases of area 0
|
||||
const [x, y] = this.map.forEach((tile, x, y) => {
|
||||
const [x, y] = this.#map.forEach((tile, x, y) => {
|
||||
if (tile.typeId === TileChars.PLAYER_START_POINT) {
|
||||
return [x, y];
|
||||
}
|
||||
});
|
||||
|
||||
const result = this.map.getAllTraversableTilesConnectedTo(x, y);
|
||||
const result = this.#map.getAllTraversableTilesConnectedTo(x, y);
|
||||
|
||||
if (result.size === traversableTileCount) {
|
||||
// There are no isolated areas, return
|
||||
@@ -385,7 +397,7 @@ class DungeonFactory {
|
||||
const floorTiles = [];
|
||||
for (let y = 0; y < this.height; y++) {
|
||||
for (let x = 0; x < this.width; x++) {
|
||||
if (this.map.get(x, y).isFloor()) {
|
||||
if (this.#map.get(x, y).isFloor()) {
|
||||
floorTiles.push({ x, y });
|
||||
}
|
||||
}
|
||||
@@ -405,11 +417,11 @@ class DungeonFactory {
|
||||
// }
|
||||
|
||||
// Add monsters
|
||||
const encouterCount = Math.min(5, this.rooms.length);
|
||||
const encouterCount = Math.min(5, this.#rooms.length);
|
||||
for (let i = 0; i < encouterCount; i++) {
|
||||
const pos = floorTiles[this.random(0, floorTiles.length - 1)];
|
||||
if (this.map.tiles[pos.y][pos.x].isFloor()) {
|
||||
this.map.tiles[pos.y][pos.x] = Tile.createEncounterStartPoint("PLACEHOLDER_ENCOUNTER_ID");
|
||||
if (this.#map.tiles[pos.y][pos.x].isFloor()) {
|
||||
this.#map.tiles[pos.y][pos.x] = Tile.createEncounterStartPoint("PLACEHOLDER_ENCOUNTER_ID");
|
||||
// TODO: Add encounter to the dungeon's "roaming entities" array.
|
||||
}
|
||||
}
|
||||
@@ -468,15 +480,15 @@ export const downloadDungeon = () => {
|
||||
URL.revokeObjectURL(url);
|
||||
};
|
||||
|
||||
widthEl.addEventListener("input", function () {
|
||||
widthEl.addEventListener("input", function() {
|
||||
document.getElementById("widthValue").textContent = this.value;
|
||||
});
|
||||
|
||||
heightEl.addEventListener("input", function () {
|
||||
heightEl.addEventListener("input", function() {
|
||||
document.getElementById("heightValue").textContent = this.value;
|
||||
});
|
||||
|
||||
roomCountEl.addEventListener("input", function () {
|
||||
roomCountEl.addEventListener("input", function() {
|
||||
document.getElementById("roomCountValue").textContent = this.value;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user