public void Generate() { BSP rooms = new BSP(0, 0, WIDTH, HEIGHT); rooms.SplitRecursive(rand, 5, 4, 4, 1.3f, 1.3f); Console.WriteLine(rooms.ToString()); GenRoom(rooms); }
BSP(int x, int y, int w, int h, BSP father) { this.x = x; this.y = y; width = w; height = h; leaf = true; level = (byte)(father.level + 1); this.father = father; }
public void GenFromBSP(BSP bsp) { RoomGraph next; while (!bsp.Leaf) { next = new RoomGraph(); if (bsp.Horizontal) { if (neighbourg.ContainsKey(NeighbourgPos.Right)) { next.addNeighbourg(neighbourg[NeighbourgPos.Right], NeighbourgPos.Right); neighbourg[NeighbourgPos.Right].changeNeighbourg(next, NeighbourgPos.Left); this.changeNeighbourg(next, NeighbourgPos.Right); } else { this.addNeighbourg(next, NeighbourgPos.Right); } next.addNeighbourg(this, NeighbourgPos.Left); } else { if (neighbourg.ContainsKey(NeighbourgPos.Down)) { next.addNeighbourg(neighbourg[NeighbourgPos.Down], NeighbourgPos.Down); neighbourg[NeighbourgPos.Down].changeNeighbourg(next, NeighbourgPos.Up); this.changeNeighbourg(next, NeighbourgPos.Down); } else { this.addNeighbourg(next, NeighbourgPos.Down); } next.addNeighbourg(this, NeighbourgPos.Up); } next.GenFromBSP(bsp.Right); bsp = bsp.Left; } pos = bsp; room = new DungeonRoom(new Vector3(bsp.Width, 1, bsp.Height), new Vector3(bsp.X, 0, bsp.Y)); room.LoadRoom(); }
private void GenRoom(BSP bsp) { rooms.GenFromBSP(bsp); //if (bsp.Leaf) //{ // int x = bsp.X+1; // int y = bsp.Y+1; // int w = bsp.Width-2; // int h = bsp.Height-2; // for (int i = x; i < x + w; i++) // for (int j = y; j < y + h; j++) // { // tiles[i, j] = true; // } //} //else //{ // GenRoom(bsp.Left); // GenRoom(bsp.Right); //} }
/// <summary> /// Once you have the root node, you can split it into two smaller non-overlapping nodes. /// </summary> /// <param name="horizontal">If true, the node will be splitted horizontally, else, vertically.</param> /// <param name="position">Coordinate of the splitting position.</param> public void SplitOnce(bool horizontal, int position) { if (horizontal) { ls = new BSP(x, y, position - x, height, this); rs = new BSP(position, y, width + x - position, height, this); } else { ls = new BSP(x, y, width, position - y, this); rs = new BSP(x, position, width, height + y - position, this); } leaf = false; this.horizontal = horizontal; this.position = position; }