Beispiel #1
0
    // Use this for initialization
    void Start()
    {
        theSplitHandler = new scSplitHandler();
        theConnections  = new scTransitiveConnect();

        GameObject theSection = (GameObject)GameObject.Instantiate(Resources.Load("PartionSection"));

        theSection.transform.localScale = new Vector3(100, 100, 0.1f);
        theSection.transform.position   = new Vector3(0, 1, 0);
        theSection.GetComponent <Renderer>().material.color = new Color(Random.Range(0, 100) / 100f, Random.Range(0, 100) / 100f, Random.Range(0, 100) / 100f);

        rootNode = new scBSPNode(theSection, null);

        for (int i = 0; i < 5; i++)
        {
            splitLeafs(rootNode);
        }

        GameObject folder = GameObject.FindGameObjectWithTag("BSPPartitionSections");

        rootNode.getData().transform.parent = folder.transform;

        //rotate all cells to be on correct axis, no longer working in 2D
        folder.transform.eulerAngles = new Vector3(90, 0, 0);
        //move it to ensure no tiles are at a negative position (which will break the grid)
        folder.transform.position = new Vector3(100, 0, 100);
        addRoomToLeafs(rootNode);
    }
Beispiel #2
0
    private GameObject findLeafOfNode(scBSPNode _aNode)
    {
        if (_aNode.getLeftChild() != null)
        {
            findLeafOfNode(_aNode.getLeftChild());
        }

        return(_aNode.getData());
    }
Beispiel #3
0
    //split the leaf partition
    private void split(scBSPNode _aNode)
    {
        GameObject a;
        GameObject b;

        //split this partition
        theSplitHandler.split(_aNode.getData(), out a, out b);

        _aNode.setLeftChild(a);
        _aNode.setRightChild(b);
    }
Beispiel #4
0
 //find and add rooms to the leafs of the BSP tree
 private void addRoomToLeafs(scBSPNode _aNode)
 {
     if (_aNode.getLeftChild() == null)
     {
         createRoom(_aNode.getData());
     }
     else
     {
         addRoomToLeafs(_aNode.getLeftChild());
         addRoomToLeafs(_aNode.getRightChild());
     }
 }
Beispiel #5
0
 //sub divide the leafs of the BSP tree
 private void splitLeafs(scBSPNode _aNode)
 {
     if (_aNode.getLeftChild() == null)
     {
         split(_aNode);
     }
     else
     {
         splitLeafs(_aNode.getLeftChild());
         splitLeafs(_aNode.getRightChild());
     }
 }
Beispiel #6
0
    private GameObject findChildWithRoom(scBSPNode _aNode)
    {
        foreach (scBSPRoom aRoom in roomList)
        {
            if (aRoom.getParentPartition() == _aNode.getData())
            {
                return(aRoom.getParentPartition());
            }
        }

        return(findChildWithRoom(_aNode.getRightChild()));
    }
Beispiel #7
0
    //connect rooms in the BSP together
    private void connectRooms(scBSPNode _aNode)
    {
        if (_aNode.getLeftChild() == null)
        {
            GameObject child0 = _aNode.getParent().getRightChild().getData();

            if (_aNode.getParent().getRightChild().getData() == _aNode.getData())
            {
                child0 = _aNode.getParent().getLeftChild().getData();
            }

            GameObject child1 = _aNode.getData();

            theConnections.addConnection(child0, child1);

            theConnections.findNearestRooms(child0, child1, out child0, out child1);

            theDigger.connect(child0, child1);
        }
        else
        {
            connectRooms(_aNode.getLeftChild());
            connectRooms(_aNode.getRightChild());


            if (_aNode.getParent() != null)
            {
                scBSPNode targetNode = _aNode.getParent().getLeftChild();

                if (targetNode == _aNode)
                {
                    targetNode = _aNode.getParent().getRightChild();
                }

                GameObject child0 = findChildWithRoom(_aNode);

                //GameObject child1 = findChildWithRoom(targetNode);
                GameObject child1 = findChildWithRoom(targetNode);

                if (!theConnections.checkConnected(child0, child1))
                {
                    theConnections.findNearestRooms(child0, child1, out child0, out child1);
                    theConnections.addConnection(child0, child1);


                    theDigger.connect(child0, child1);
                }
            }
        }
    }
Beispiel #8
0
 public scBSPNode(GameObject _partion, scBSPNode _parent)
 {
     data   = _partion;
     parent = _parent;
 }
Beispiel #9
0
 public void setRightChild(GameObject _partion)
 {
     rightChild = new scBSPNode(_partion, this);
 }
Beispiel #10
0
 public void setLeftChild(GameObject _partion)
 {
     leftChild = new scBSPNode(_partion, this);
 }