Add NRandomRoomsGenerator
This commit is contained in:
parent
5517fd0110
commit
e4a7550abc
1 changed files with 61 additions and 0 deletions
|
@ -319,6 +319,67 @@ class BSPNode {
|
|||
}
|
||||
}
|
||||
|
||||
class NRandomRoomsGenerator {
|
||||
static MIN_ROOM_DIMENSION = 7;
|
||||
static MAX_ROOM_DIMENSION = 12;
|
||||
|
||||
#numberOfRooms = 12;
|
||||
#rooms;
|
||||
|
||||
#bounds;
|
||||
|
||||
constructor(bounds, numberOfRooms) {
|
||||
if (bounds) {
|
||||
this.#bounds = bounds;
|
||||
}
|
||||
|
||||
if (numberOfRooms) {
|
||||
this.#numberOfRooms = numberOfRooms;
|
||||
}
|
||||
}
|
||||
|
||||
get numberOfRooms() {
|
||||
return this.#numberOfRooms;
|
||||
}
|
||||
|
||||
get rooms() {
|
||||
if (!this.#rooms) {
|
||||
this.#generateRooms();
|
||||
}
|
||||
|
||||
return this.#rooms;
|
||||
}
|
||||
|
||||
#generateRooms() {
|
||||
let rects = new Array();
|
||||
|
||||
const sizeRange = NRandomRoomsGenerator.MAX_ROOM_DIMENSION - NRandomRoomsGenerator.MIN_ROOM_DIMENSION;
|
||||
|
||||
while (rects.length < this.#numberOfRooms) {
|
||||
const randomSize = new Size(
|
||||
NRandomRoomsGenerator.MIN_ROOM_DIMENSION + randomInt(sizeRange),
|
||||
NRandomRoomsGenerator.MIN_ROOM_DIMENSION + randomInt(sizeRange)
|
||||
);
|
||||
|
||||
const randomOrigin = new Point(
|
||||
this.#bounds.minX + randomInt(this.#bounds.maxX - randomSize.width),
|
||||
this.#bounds.minY + randomInt(this.#bounds.maxY - randomSize.height)
|
||||
);
|
||||
|
||||
const proposedRoomRect = new Rect(randomOrigin, randomSize);
|
||||
|
||||
// Check that the rect doesn't intersect with any other rects.
|
||||
if (rects.some(e => e.intersects(proposedRoomRect))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
rects.push(proposedRoomRect);
|
||||
}
|
||||
|
||||
this.#rooms = rects.map(r => new Room(r.insetRect(1)));
|
||||
}
|
||||
}
|
||||
|
||||
function randomInt(n) {
|
||||
max = Math.floor(n);
|
||||
return Math.floor(Math.random() * max);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue