Remove BSPNode
This commit is contained in:
parent
aa50054108
commit
78caa686c2
1 changed files with 0 additions and 129 deletions
|
@ -190,135 +190,6 @@ class Room {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class BSPNode {
|
|
||||||
static MIN_AREA = 36;
|
|
||||||
static MIN_ROOM_DIMENSION = 5;
|
|
||||||
|
|
||||||
x;
|
|
||||||
y;
|
|
||||||
width;
|
|
||||||
height;
|
|
||||||
leftChild;
|
|
||||||
rightChild;
|
|
||||||
room;
|
|
||||||
#done = false;
|
|
||||||
|
|
||||||
constructor(x, y, w, h) {
|
|
||||||
this.x = x;
|
|
||||||
this.y = y;
|
|
||||||
this.width = w;
|
|
||||||
this.height = h;
|
|
||||||
|
|
||||||
this.leftChild;
|
|
||||||
this.rightChild;
|
|
||||||
this.room;
|
|
||||||
}
|
|
||||||
|
|
||||||
get maxX() {
|
|
||||||
return this.x + this.width;
|
|
||||||
}
|
|
||||||
|
|
||||||
get maxY() {
|
|
||||||
return this.y + this.height;
|
|
||||||
}
|
|
||||||
|
|
||||||
get rooms() {
|
|
||||||
let rooms = new Array();
|
|
||||||
|
|
||||||
if (this.room) {
|
|
||||||
rooms.push(this.room);
|
|
||||||
return rooms;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.leftChild) {
|
|
||||||
rooms = rooms.concat(this.leftChild.rooms);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.rightChild) {
|
|
||||||
rooms = rooms.concat(this.rightChild.rooms);
|
|
||||||
}
|
|
||||||
|
|
||||||
return rooms;
|
|
||||||
}
|
|
||||||
|
|
||||||
divide() {
|
|
||||||
if (this.#done) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const area = this.width * this.height;
|
|
||||||
if (area < BSPNode.MIN_AREA) {
|
|
||||||
if (!this.#done && Math.random() > 0.8) {
|
|
||||||
this.#createRoom();
|
|
||||||
}
|
|
||||||
this.#done = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (area < 100 && Math.random() > 0.9) {
|
|
||||||
this.#createRoom();
|
|
||||||
this.#done = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let shouldSplitVertically = Math.random() < 0.5;
|
|
||||||
console.debug("Should split vertically:", shouldSplitVertically);
|
|
||||||
|
|
||||||
if (shouldSplitVertically) {
|
|
||||||
let xCoordinateOfDivision = this.#randomIntBetween(this.x, this.maxX);
|
|
||||||
if (xCoordinateOfDivision) {
|
|
||||||
this.leftChild = new BSPNode(this.x, this.y, xCoordinateOfDivision - this.x, this.height);
|
|
||||||
this.rightChild = new BSPNode(xCoordinateOfDivision, this.y, this.maxX - xCoordinateOfDivision, this.height);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let yCoordinateOfDivision = this.#randomIntBetween(this.y, this.maxY);
|
|
||||||
if (yCoordinateOfDivision) {
|
|
||||||
this.leftChild = new BSPNode(this.x, this.y, this.width, yCoordinateOfDivision - this.y);
|
|
||||||
this.rightChild = new BSPNode(this.x, yCoordinateOfDivision, this.width, this.maxY - yCoordinateOfDivision);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.leftChild && !this.rightChild) {
|
|
||||||
if (!this.#done && Math.random() > 0.5) {
|
|
||||||
this.#createRoom();
|
|
||||||
}
|
|
||||||
this.#done = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
divideRecursively() {
|
|
||||||
this.divide();
|
|
||||||
|
|
||||||
if (this.room) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.leftChild) {
|
|
||||||
this.leftChild.divideRecursively();
|
|
||||||
}
|
|
||||||
if (this.rightChild) {
|
|
||||||
this.rightChild.divideRecursively();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#createRoom() {
|
|
||||||
this.room = new Room(this.x + 1, this.y + 1, this.width - 2, this.height - 2);
|
|
||||||
console.log("Created a room:", this.room);
|
|
||||||
}
|
|
||||||
|
|
||||||
#randomIntBetween(lower, upper) {
|
|
||||||
const randomMin = lower + BSPNode.MIN_ROOM_DIMENSION;
|
|
||||||
const randomMax = upper - BSPNode.MIN_ROOM_DIMENSION;
|
|
||||||
|
|
||||||
console.debug(`Random int between: ${lower} -> ${randomMin} and ${upper} -> ${randomMax}`);
|
|
||||||
|
|
||||||
const result = randomMax > randomMin ? randomMin + Math.floor(Math.random() * (randomMax - randomMin + 1)) : null;
|
|
||||||
console.debug("Result:", result);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class NRandomRoomsGenerator {
|
class NRandomRoomsGenerator {
|
||||||
static MIN_ROOM_DIMENSION = 7;
|
static MIN_ROOM_DIMENSION = 7;
|
||||||
static MAX_ROOM_DIMENSION = 12;
|
static MAX_ROOM_DIMENSION = 12;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue