Esempio n. 1
0
    Road AddBranch(Road prevSegment, Road straight, Junction j, float angle, float length, float t, RoadType type)
    {
        Road branchRoad = segFactory.CreateRoad(
            prevSegment.end,
            Quaternion.Euler(prevSegment.transform.localEulerAngles.x, angle, 0),
            length,
            t,
            type
            );

        if (j == null)
        {
            j = segFactory.CreateJunction(prevSegment.end, Quaternion.identity);
            straight.attachedSegments.Add(j);
            j.AddOutgoing(straight);
        }
        branchRoad.attachedSegments.Add(j);
        j.AddIncoming(prevSegment);
        j.AddOutgoing(branchRoad);

        return(branchRoad);
    }
Esempio n. 2
0
    private void Start()
    {
        List <Vector3> corners = new List <Vector3>();
        float          radius  = 100;
        float          n       = 5;

        for (int i = 0; i < n; i++)
        {
            float angle = i * (2f * Mathf.PI) / n;
            print(angle);
            Vector3 c = radius * new Vector3(
                Mathf.Cos(angle),
                0,
                Mathf.Sin(angle)
                );
            corners.Add(c);
            segFactory.CreateJunction(c, Quaternion.identity);
        }
        corners.Reverse();
        MeshBuilder mb = Polygon.Mesh(corners);

        GetComponent <MeshFilter>().mesh = mb.Generate();
    }
    public bool Check(Road road, RoadMap roadMap)
    {
        if (stop)
        {
            return(false);      // delete this
        }
        foreach (int i in nums)
        {
            if (road.id == i)
            {
                print(road.id);
            }
        }

        if (segFactory == null)
        {
            Initialize();
        }

        int      actionPriority            = 0;
        Vector3  finalCrossingIntersection = Vector3.zero;
        Vector3  finalEdgeIntersection     = Vector3.zero;
        Vector3  finalNewIntersection      = Vector3.zero;
        Road     finalOtherRoad            = null;
        Junction finalOtherJunction        = null;

        // cut off road if outside map bounds

        bool    intersects;
        Vector3 edgeIntersection = InterestsMapBoundary(road, out intersects);

        if (intersects)
        {
            actionPriority = 3;
            if (finalEdgeIntersection == Vector3.zero ||
                Vector3.Distance(road.start, edgeIntersection) < Vector3.Distance(road.start, finalEdgeIntersection))
            {
                finalEdgeIntersection = edgeIntersection;
            }
        }

        Rect           searchBounds = GetSearchBounds(road);
        List <Segment> matches      = roadMap.QuadTree.Query(road.Bounds);

        foreach (Segment other in matches)
        {
            if (other.GetType() == typeof(Road))
            {
                // check for intersecting roads

                Road otherRoad = (Road)other;

                if (actionPriority <= 4)
                {
                    bool    found;
                    Vector3 crossingIntersection = DoRoadsIntersect(road, otherRoad, out found);
                    if (!(road.Parent.id == otherRoad.id) && !(road.Parent.id == otherRoad.Parent.id))
                    {
                        if (found)
                        {
                            actionPriority = 4;

                            if (ExceedsMinimumIntersectionAngle(road, otherRoad))
                            {
                                if (finalCrossingIntersection == Vector3.zero ||
                                    Vector3.Distance(road.start, crossingIntersection) < Vector3.Distance(road.start, finalCrossingIntersection))
                                {
                                    finalCrossingIntersection = crossingIntersection;
                                    finalOtherRoad            = otherRoad;
                                }
                            }
                            else
                            {
                                if (CityConfig.SHOW_FAILED_JUNCTIONS)
                                {
                                    Junction j = segFactory.CreateJunction(road.start, Quaternion.identity);
                                    j.SetColor(Color.red);
                                }

                                return(false);
                            }
                        }
                    }
                }

                if (actionPriority <= 1)
                {
                    // check for potential crossings within snap distance

                    Vector3 newIntersection = NearestPointOnLine(otherRoad.start, otherRoad.end - otherRoad.start, road.end);
                    if (Vector3.Distance(newIntersection, road.end) < CityConfig.ROAD_SNAP_DISTANCE)
                    {
                        actionPriority = 1;

                        if (ExceedsMinimumIntersectionAngle(road, otherRoad))
                        {
                            if (finalNewIntersection == Vector3.zero ||
                                Vector3.Distance(road.start, newIntersection) < Vector3.Distance(road.start, finalNewIntersection))
                            {
                                finalNewIntersection = newIntersection;
                                finalOtherRoad       = otherRoad;
                            }
                        }
                        else
                        {
                            if (CityConfig.SHOW_FAILED_JUNCTIONS)
                            {
                                Junction j = segFactory.CreateJunction(road.start, Quaternion.identity);
                                j.SetColor(Color.red);
                            }

                            return(false);
                        }
                    }
                }
            }
            else if (actionPriority <= 2 && other.GetType() == typeof(Junction))
            {
                Junction otherJunction = (Junction)other;

                // check for existing crossings within snap distance

                if (!road.attachedSegments.Contains(otherJunction) &&
                    Vector3.Distance(otherJunction.transform.localPosition, road.end) < CityConfig.ROAD_SNAP_DISTANCE
                    )
                {
                    actionPriority = 2;
                    if (finalOtherJunction == null ||

                        Vector3.Distance(road.start, otherJunction.transform.position) < Vector3.Distance(road.start, finalOtherJunction.transform.position))
                    {
                        finalOtherJunction = otherJunction;
                    }
                }
            }
        }

        if (actionPriority == 4)
        {
            MakeRoadIntersection(road, finalOtherRoad, finalCrossingIntersection, roadMap);
        }
        else if (actionPriority == 3)
        {
            MakeEdgeIntersection(road, finalEdgeIntersection);
        }
        else if (actionPriority == 2)
        {
            SnapToExistingJunction(road, finalOtherJunction);
        }
        else if (actionPriority == 1)
        {
            SnapToNewJunction(road, finalOtherRoad, finalNewIntersection, roadMap);
        }

        return(true);
    }