Redo Room in terms of Rect
This commit is contained in:
parent
8a5fc9c820
commit
b8ac3126da
1 changed files with 68 additions and 28 deletions
|
@ -81,58 +81,98 @@ class Rect {
|
||||||
}
|
}
|
||||||
|
|
||||||
class Grid {
|
class Grid {
|
||||||
#width;
|
#size;
|
||||||
#height;
|
#cells = [];
|
||||||
cells = [];
|
|
||||||
|
#rooms = [];
|
||||||
|
|
||||||
constructor(width, height) {
|
constructor(width, height) {
|
||||||
this.#width = width;
|
this.#size = new Size(width, height);
|
||||||
this.#height = height;
|
|
||||||
|
|
||||||
this.cells = new Array(width * height);
|
this.#cells = new Array(width * height);
|
||||||
for (let i = 0; i < this.cells.length; i++) {
|
for (let i = 0; i < this.#cells.length; i++) {
|
||||||
this.cells[i] = new Cell(" ");
|
this.#cells[i] = new Cell(" ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get width() {
|
get width() {
|
||||||
return this.#width;
|
return this.#size.width;
|
||||||
}
|
}
|
||||||
|
|
||||||
get height() {
|
get height() {
|
||||||
return this.#height;
|
return this.#size.height;
|
||||||
|
}
|
||||||
|
|
||||||
|
generate(p, roomGenerator) {
|
||||||
|
this.#generateRooms(p, roomGenerator);
|
||||||
|
this.#placeStairs();
|
||||||
|
}
|
||||||
|
|
||||||
|
#generateRooms(p, generator) {
|
||||||
|
this.#rooms = generator.rooms;
|
||||||
|
|
||||||
|
for (let room of this.#rooms) {
|
||||||
|
for (let y = room.minY; y <= room.maxY; y++) {
|
||||||
|
for (let x = room.minX; x <= room.maxX; x++) {
|
||||||
|
let charAtXY = room.charAt(x, y);
|
||||||
|
if (!charAtXY) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let cell = this.cellAt(x, y);
|
||||||
|
cell.character = charAtXY;
|
||||||
|
cell.characterColor = p.color(255);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#placeStairs() {
|
||||||
|
const indexOfRoomWithUpStairs = randomInt(this.#rooms.length);
|
||||||
|
const coordinateOfUpStair = this.#rooms[indexOfRoomWithUpStairs].randomPoint();
|
||||||
|
this.cellAt(coordinateOfUpStair.x, coordinateOfUpStair.y).upStair();
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
let indexOfRoomForDownStair = randomInt(this.#rooms.length);
|
||||||
|
if (indexOfRoomForDownStair == indexOfRoomWithUpStairs) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const coordinateOfDownStair = this.#rooms[indexOfRoomForDownStair].randomPoint();
|
||||||
|
this.cellAt(coordinateOfDownStair.x, coordinateOfDownStair.y).downStair();
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cellAt(x, y) {
|
cellAt(x, y) {
|
||||||
return this.cells[y * this.#width + x];
|
return this.#cells[y * this.width + x];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Room {
|
class Room {
|
||||||
x = 0;
|
#rect;
|
||||||
y = 0;
|
|
||||||
width = 0;
|
|
||||||
height = 0;
|
|
||||||
|
|
||||||
constructor(x, y, w, h) {
|
constructor(rect) {
|
||||||
this.x = x;
|
this.#rect = rect;
|
||||||
this.y = y;
|
|
||||||
this.width = w;
|
|
||||||
this.height = h;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get maxX() {
|
get minX() { return this.#rect.minX; }
|
||||||
return this.x + this.width;
|
get minY() { return this.#rect.minY; }
|
||||||
}
|
get maxX() { return this.#rect.maxX; }
|
||||||
|
get maxY() { return this.#rect.maxY; }
|
||||||
|
|
||||||
get maxY() {
|
randomPoint() {
|
||||||
return this.y + this.height;
|
return new Point(
|
||||||
|
this.#rect.minX + 1 + randomInt(this.#rect.size.width - 2),
|
||||||
|
this.#rect.minY + 1 + randomInt(this.#rect.size.height - 2)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
charAt(x, y) {
|
charAt(x, y) {
|
||||||
const minX = this.x;
|
const minX = this.minX;
|
||||||
const minY = this.y;
|
const minY = this.minY;
|
||||||
const maxX = this.maxX
|
const maxX = this.maxX;
|
||||||
const maxY = this.maxY;
|
const maxY = this.maxY;
|
||||||
|
|
||||||
if (y == minY && x == minX) { return "┌"; }
|
if (y == minY && x == minX) { return "┌"; }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue