Beispiel #1
0
    void ConnectRegions(int to)
    {
        BSPNode nearFromNode = null;
        BSPNode nearToNode   = null;
        float   nearDistance = -1;

        foreach (KeyValuePair <int, List <BSPNode> > iter in regions)
        {
            int from = iter.Key;

            if (from != to)
            {
                List <BSPNode> fromRegion = regions[from];
                List <BSPNode> toRegion   = regions[to];

                foreach (var fromNode in fromRegion)
                {
                    foreach (var toNode in toRegion)
                    {
                        float distance = Vector2.Distance(fromNode.rect.center, toNode.rect.center);
                        if (nearDistance == -1 || distance < nearDistance)
                        {
                            nearDistance = distance;
                            nearFromNode = fromNode;
                            nearToNode   = toNode;
                        }
                    }
                }
            }
        }

        nearToNode.AddConnection(nearFromNode);
        MergeRegions(nearToNode.regionId, nearFromNode.regionId);
    }
Beispiel #2
0
    private void PrintConnections(BSPNode node)
    {
        if (node != null)
        {
            if (node.Area == 0)
            {
                if (node.childNodes != null)
                {
                    if (node.childNodes[0].Area != 0 && node.childNodes[1].Area != 0)
                    {
                        node.childNodes[0].AddConnection(node.childNodes[1]);
                        //_connectionsData.Add(new Vector3(node.childNodes[0].rect.center.x, 5, node.childNodes[0].rect.center.y));
                        //_connectionsData.Add(new Vector3(node.childNodes[1].rect.center.x, 5, node.childNodes[1].rect.center.y));
                    }
                    else if (node.childNodes[0].Area == 0 && node.childNodes[1].Area != 0)
                    {
                        BSPNode refNode     = null;
                        float   refDistance = -1;
                        GetNearbyNode(node.childNodes[0], node.childNodes[1], ref refNode, ref refDistance);
                        refNode.AddConnection(node.childNodes[1]);
                        //_connectionsData.Add(new Vector3(refNode.rect.center.x, 5, refNode.rect.center.y));
                        //_connectionsData.Add(new Vector3(node.childNodes[1].rect.center.x, 5, node.childNodes[1].rect.center.y));
                    }
                    else if (node.childNodes[1].Area == 0 && node.childNodes[0].Area != 0)
                    {
                        BSPNode refNode     = null;
                        float   refDistance = -1;
                        GetNearbyNode(node.childNodes[1], node.childNodes[0], ref refNode, ref refDistance);
                        refNode.AddConnection(node.childNodes[0]);
                        //_connectionsData.Add(new Vector3(refNode.rect.center.x, 5, refNode.rect.center.y));
                        //_connectionsData.Add(new Vector3(node.childNodes[0].rect.center.x, 5, node.childNodes[0].rect.center.y));
                    }
                    int length = node.childNodes.Length;

                    for (int index = 0; index < length; index++)
                    {
                        PrintConnections(node.childNodes[index]);
                    }
                }
            }
        }
    }