private void CreateRooms(Vector2 size) { root = new BspGridLeaf(0, 0, (int)size.x, (int)size.y, null); leaves.Add(root); bool didSplit = true; while (didSplit) { didSplit = false; for (int i = 0; i < leaves.Count; i++) { // If we haven't already split the leaf if (leaves [i].firstChild == null && leaves [i].secondChild == null) { // Attempt to split it if (leaves [i].Split()) { // If we can split it, add its children to the leaves list leaves.Add(leaves [i].firstChild); leaves.Add(leaves [i].secondChild); didSplit = true; } } } root.CreateRooms(); } SetGrid(); CreateCorridors(); }
public Room(Vector2 _size, Vector2 _bottomLeft, BspGridLeaf _parent) { size = _size; bottomLeft = _bottomLeft; parent = _parent; centrePos = bottomLeft + size / 2; }
private void BuildExit() { // Randomly choose an edge to exit at // float ran = Random.Range (0f, 1f); // if (ran <= 0.25f) // { // } // else if (ran <= 0.5f) // { // } // else if (ran <= 0.75f) // { // } // else // { // } foreach (BspGridLeaf leaf in leaves) { if (leaf.hasRoom) { exitLeafMaxX = leaf; exitLeafMaxY = leaf; exitLeafMinX = leaf; exitLeafMinY = leaf; break; } } foreach (BspGridLeaf leaf in leaves) { if (leaf.hasRoom) { if (leaf.room.centrePos.x > exitLeafMaxX.room.centrePos.x) { exitLeafMaxX = leaf; } if (leaf.room.centrePos.y > exitLeafMaxY.room.centrePos.y) { exitLeafMaxY = leaf; } if (leaf.room.centrePos.x < exitLeafMinX.room.centrePos.x) { exitLeafMinX = leaf; } if (leaf.room.centrePos.y < exitLeafMinY.room.centrePos.y) { exitLeafMinY = leaf; } } } // Debug.DrawLine (new Vector2 (treeSize.x / 2f, treeSize.y / 2f), exitLeafMaxX.room.centrePos, Color.red, 5f); // Debug.DrawLine (new Vector2 (treeSize.x / 2f, treeSize.y / 2f), exitLeafMaxY.room.centrePos, Color.red, 5f); // Debug.DrawLine (new Vector2 (treeSize.x / 2f, treeSize.y / 2f), exitLeafMinX.room.centrePos, Color.red, 5f); // Debug.DrawLine (new Vector2 (treeSize.x / 2f, treeSize.y / 2f), exitLeafMinY.room.centrePos, Color.red, 5f); }
public bool Split() { // If this leaf already has children, skip it if (firstChild != null || secondChild != null) { return(false); } // Small chance of leaf not splitting leading to a large room if (Random.Range(0f, 1f) < 0.2f && parent != null) { return(false); } // 50:50 chance to split horizontally or vertically bool splitH = Random.Range(0f, 1f) < 0.5f; // If the width is greater than 1.25 height, split vertically if (width / height > 1.25f) { splitH = false; } // If the height is greater than 1.25 width, split horizontally if (height / width > 1.25f) { splitH = true; } // Determine the max height or width (dependant on splitH) of the child leaf int max = (splitH ? height : width) - MIN_LEAF_SIZE; // If the max is less than minimum size allowed, return false if (max <= MIN_LEAF_SIZE) { return(false); } // Generate split int split = Random.Range(MIN_LEAF_SIZE, max); if (splitH) { firstChild = new BspGridLeaf(leftX, bottomY, width, split, this); secondChild = new BspGridLeaf(leftX, bottomY + split, width, height - split, this); } else { firstChild = new BspGridLeaf(leftX, bottomY, split, height, this); secondChild = new BspGridLeaf(leftX + split, bottomY, width - split, height, this); } // If we've got this far, we've successfully split the leaf return(true); }
public BspGridLeaf(int _leftX, int _bottomY, int _width, int _height, BspGridLeaf _parent) { leftX = _leftX; bottomY = _bottomY; width = _width; height = _height; parent = _parent; hasRoom = false; centre = new Vector2(leftX + width / 2, bottomY + height / 2); }