Exemplo n.º 1
0
    public void Generate()
    {
        foreach (Transform child in RoadNetwork.transform)
        {
            var tempCon = new Connection();
            //Ignore intersections
            if (!child.GetComponent <GSDRoad>())
            {
                foreach (Transform inter in child.transform)
                {
                    var i = new IntersectionPoint();
                    i.IntGSD = inter.GetComponent <GSDRoadIntersection>();
                    i.Node1  = i.IntGSD.Node1;
                    i.Node2  = i.IntGSD.Node2;
                    Intersections.Add(i);
                }
                continue;
            }
            var road   = child.GetChild(0);
            var spline = road.gameObject.GetComponent <GSDSplineC>();
            foreach (var node in spline.mNodes)
            {
                if (node.bIsEndPoint)
                {
                    tempCon.Nodes.Add(node);
                    var s = new IntersectionPoint(true);
                    s.Node1 = node;
                    SpawnPoints.Add(s);
                }
                if (node.bIsIntersection)
                {
                    tempCon.Nodes.Add(node);
                    //Intersections.Add(node.pos);
                }
                if (tempCon.Nodes.Count > 1)
                {
                    tempCon.Spline = spline;
                    Connections.Add(tempCon);
                    tempCon = new Connection();
                    if (!node.bIsEndPoint)
                    {
                        tempCon.Nodes.Add(node);
                    }
                }
            }
        }

        //Link Intersections and connections
        foreach (var connection in Connections)
        {
            int c = connection.Nodes.Count - 1;
            foreach (var intersection in Intersections)
            {
                if (intersection.Node1 == connection.Nodes[0] ||
                    intersection.Node1 == connection.Nodes[c] ||
                    intersection.Node2 == connection.Nodes[0] ||
                    intersection.Node2 == connection.Nodes[c])
                {
                    connection.StartEndPoint.Add(intersection);
                    intersection.Roads.Add(connection);
                }
            }
            foreach (var spawnpoint in SpawnPoints)
            {
                if (spawnpoint.Node1 == connection.Nodes[0] ||
                    spawnpoint.Node1 == connection.Nodes[c])
                {
                    spawnpoint.Roads.Add(connection);
                    connection.StartEndPoint.Add(spawnpoint);
                }
            }
        }

        foreach (var i in Intersections)
        {
            RoadManager.AddIntersection(i);
        }
        foreach (var s in SpawnPoints)
        {
            RoadManager.AddIntersection(s);
        }
        if (RoadManager.DebubMode)
        {
            Debug.Log("RoadManager Intersections: " + RoadManager.IntersectionPoints.Count);
        }
        foreach (var c in Connections)
        {
            RoadManager.AddRoad(c);
        }
        RoadManager.CalculateDijkstraTables();
        if (RoadManager.DebubMode)
        {
            Debug.Log("Intersections: " + Intersections.Count);
        }
        if (RoadManager.DebubMode)
        {
            Debug.Log("Connections: " + Connections.Count);
        }
        if (RoadManager.DebubMode)
        {
            Debug.Log("SpawnPoints: " + SpawnPoints.Count);
        }
    }