Example #1
0
 /// <summary>
 /// Initializes the tree
 /// </summary>
 public void InitQuadtree()
 {
     _tree = new Quadtree(_boundaries);
     _tree.BuildTree(1, 4);
     _tree.AddRoom();
     _treeIsInit = true;
 }
Example #2
0
    /// <summary>
    /// Recursively creates the tree by adding the child nodes
    /// </summary>
    /// <param name="currentDepth">The current depth of the tree</param>
    /// <param name="maxDepth">The max depth the tree can have</param>
    public void BuildTree(int currentDepth, int maxDepth)
    {
        // We want to stop if we have reached the max depth
        if (currentDepth > maxDepth)
        {
            return;
        }
        // Or if it is not possible to store 4 rooms in the space
        XY wh = _box.half / 2;

        if (wh.x < Dungeon.MIN_ROOM_HALFSIZE + 2 * Dungeon.MIN_ROOM_MARGIN || wh.y < Dungeon.MIN_ROOM_HALFSIZE + 2 * Dungeon.MIN_ROOM_MARGIN)
        {
            return;
        }



        // Make sure they are even numbers
        int x = _box.Left() + 2 * Random.Range(Dungeon.MIN_ROOM_HALFSIZE + Dungeon.MIN_ROOM_MARGIN, _box.half.x - Dungeon.MIN_ROOM_HALFSIZE - Dungeon.MIN_ROOM_MARGIN);
        int y = _box.Bottom() + 2 * Random.Range(Dungeon.MIN_ROOM_HALFSIZE + Dungeon.MIN_ROOM_MARGIN, _box.half.y - Dungeon.MIN_ROOM_HALFSIZE - Dungeon.MIN_ROOM_MARGIN);


        // The half sizes
        int left  = (x - _box.Left()) / 2;
        int right = (_box.Right() - x) / 2;
        int up    = (_box.Top() - y) / 2;
        int down  = (y - _box.Bottom()) / 2;

        // Creating each trees and build them
        _northWest = new Quadtree(new AABB(new XY(x - left, y + up), new XY(left, up)));
        _northWest.BuildTree(currentDepth + 1, maxDepth);
        _northEast = new Quadtree(new AABB(new XY(x + right, y + up), new XY(right, up)));
        _northEast.BuildTree(currentDepth + 1, maxDepth);
        _southWest = new Quadtree(new AABB(new XY(x - left, y - down), new XY(left, down)));
        _southWest.BuildTree(currentDepth + 1, maxDepth);
        _southEast = new Quadtree(new AABB(new XY(x + right, y - down), new XY(right, down)));
        _southEast.BuildTree(currentDepth + 1, maxDepth);
    }