public void Split(bool horizontal, int maxTreeDepth, List <RoomContainer> roomContainers) { int newTreeDepth = this.treeDepth + 1; this.isSplitHorizontally = horizontal; this.isLastNode = false; // create two new roomcontainers if (horizontal) { int split_x = this.x + FindRandomSplit(this.width); this.split = split_x; int left_w = split_x - this.x; int right_w = this.width - left_w; l_child = new RoomContainer(this.x, this.y, left_w, this.height, newTreeDepth, roomContainers); r_child = new RoomContainer(split_x, this.y, right_w, this.height, newTreeDepth, roomContainers); } else { int split_y = this.y + FindRandomSplit(this.height); this.split = split_y; int left_h = split_y - this.y; int right_h = this.height - left_h; l_child = new RoomContainer(this.x, this.y, this.width, left_h, newTreeDepth, roomContainers); r_child = new RoomContainer(this.x, split_y, this.width, right_h, newTreeDepth, roomContainers); } // recursively create more sub rooms in newly created rooms if maxTreeDepth hasn't been reached yet // if this room was split horizontally, split the next rooms vertically (and vice versa) // Somtimes it will randomly stop splitting a room and leave it bigger. if (newTreeDepth < maxTreeDepth) { if (Random.Range(0, 1f) > (STOP_SPLITTING_CHANCE * newTreeDepth) + (2 * -STOP_SPLITTING_CHANCE)) { l_child.Split(!horizontal, maxTreeDepth, roomContainers); } if (Random.Range(0, 1f) > (STOP_SPLITTING_CHANCE * newTreeDepth) + (2 * -STOP_SPLITTING_CHANCE)) { r_child.Split(!horizontal, maxTreeDepth, roomContainers); } } }
void GenerateRoomContainers() { RoomContainer mainContainer = new RoomContainer(0, 0, WIDTH, HEIGHT, 0, roomContainers); mainContainer.Split(true, MAX_TREE_DEPTH, roomContainers); }