your version in java helped me and I decided to sketch it in c #
maybe will help anyone
public class BspTree
{
public RectInt container;
public RectInt room;
public BspTree left;
public BspTree right;
public BspTree(RectInt a)
{
container = a;
}
internal static BspTree Split(int numberOfOperations, RectInt container)
{
var node = new BspTree(container);
if (numberOfOperations == 0)
{
return node;
}
var splitedContainer = SplitContainer(container);
node.left = Split(numberOfOperations - 1, splitedContainer[0]);
Debug.Log(numberOfOperations);
node.right = Split(numberOfOperations - 1, splitedContainer[1]);
Debug.Log(numberOfOperations);
return node;
}
private static RectInt[] SplitContainer(RectInt container)
{
RectInt c1, c2;
bool horizontal = Random.Range(0f, 1f) > 0.5f ? true : false;
if (horizontal)
{
c1 = new RectInt(container.x, container.y, (int)(container.width * Random.Range(0.3f, 0.6f)), container.height);
c2 = new RectInt(container.x + c1.width, container.y, container.width - c1.width, container.height);
}
else
{
c1 = new RectInt(container.x, container.y, container.width, (int)(container.height * Random.Range(0.3f, 0.6f)));
c2 = new RectInt(container.x, container.y + c1.height, container.width, container.height - c1.height);
}
return new RectInt[] { c1, c2 };
}
}
and to use it in a unity with monobehaviour
public class DungeonCreator : MonoBehaviour
{
[Range (1, 400)]
public int widthRange;
public bool shouldDebugDrawBsp;
private BspTree tree;
void Start()
{
RectInt rect = new RectInt(0, 0, widthRange, widthRange);
tree = BspTree.Split(4, rect);
}
public void DebugDrawBsp()
{
if (tree == null) return;
DebugDrawBspNode(tree);
}
public void DebugDrawBspNode(BspTree node)
{
// Container
Gizmos.color = Color.green;
// top
Gizmos.DrawLine(new Vector3(node.container.x, node.container.y, 0), new Vector3Int(node.container.xMax, node.container.y, 0));
// right
Gizmos.DrawLine(new Vector3(node.container.xMax, node.container.y, 0), new Vector3Int(node.container.xMax, node.container.yMax, 0));
// bottom
Gizmos.DrawLine(new Vector3(node.container.x, node.container.yMax, 0), new Vector3Int(node.container.xMax, node.container.yMax, 0));
// left
Gizmos.DrawLine(new Vector3(node.container.x, node.container.y, 0), new Vector3Int(node.container.x, node.container.yMax, 0));
// children
if (node.left != null) DebugDrawBspNode(node.left);
if (node.right != null) DebugDrawBspNode(node.right);
}
private void OnDrawGizmos()
{
if (shouldDebugDrawBsp)
{
DebugDrawBsp();
}
}
}