Esempio n. 1
0
    private void SplitIntersectingRoads(Road road)
    {
        Vector3 roadStart = road.StartGrid.transform.position;
        Vector3 roadEnd   = road.EndGrid.transform.position;

        List <GridRoadPair> intersectionInfoList = new List <GridRoadPair>();

        IEnumerable <Road> otherRoads = SiteManager.Instance.roadManager.Roads
                                        .Concat(SiteManager.Instance.roadManager.Arterials.Cast <Road>())
                                        .Concat(SiteManager.Instance.trafficManager.trafficBuilder.EntryRoads.Cast <Road>())
                                        .Concat(SiteManager.Instance.trafficManager.trafficBuilder.ExitRoads.Cast <Road>());

        foreach (Road other in otherRoads)
        {
            if (other == road)
            {
                // Do not check for self-intersection
                continue;
            }

            Vector3 otherStart = other.StartGrid.transform.position;
            Vector3 otherEnd   = other.EndGrid.transform.position;

            Vector3 intersection;
            if (VectorUtils.AreLineSegmentsIntersecting(roadStart, roadEnd, otherStart, otherEnd, out intersection))
            {
                intersectionInfoList.Add(new GridRoadPair(
                                             SiteManager.Instance.gridManager.GetClosestGrid(intersection),
                                             other));
            }
        }

        intersectionInfoList.Sort((x, y) => Vector3.SqrMagnitude(x.grid.Coordinates - road.StartGrid.Coordinates)
                                  .CompareTo(Vector3.SqrMagnitude(y.grid.Coordinates - road.StartGrid.Coordinates)));

        Road thisRoad = road;

        foreach (GridRoadPair intersectionInfo in intersectionInfoList)
        {
            Grid intersectionGrid = intersectionInfo.grid;
            Road otherRoad        = intersectionInfo.road;

            IBigridTransform otherRoadHeadBT, otherRoadTailBT;

            IBigridTransform thisRoadHeadBT, thisRoadTailBT;

            otherRoad.Split(intersectionGrid, out otherRoadHeadBT, out otherRoadTailBT);
            thisRoad.Split(intersectionGrid, out thisRoadHeadBT, out thisRoadTailBT);

            Road otherRoadHead = (Road)otherRoadHeadBT;
            Road otherRoadTail = (Road)otherRoadTailBT;
            Road thisRoadHead  = (Road)thisRoadHeadBT;
            Road thisRoadTail  = (Road)thisRoadTailBT;

            /*if (thisRoadHead != null && thisRoadTail != null) {
             *      thisRoadHead.AddOutgoingRoad(thisRoadTail);
             *      thisRoadTail.AddIncomingRoad(thisRoadHead);
             * }*/

            // Establish links between this road's new head and other road's new tail
            if (thisRoadHead != null && otherRoadTail != null)
            {
                thisRoadHead.AddOutgoingRoad(otherRoadTail);
                otherRoadTail.AddIncomingRoad(thisRoadHead);
            }

            /*if (otherRoadHead != null && otherRoadTail != null) {
             *      otherRoadHead.AddOutgoingRoad(otherRoadTail);
             *      otherRoadTail.AddIncomingRoad(otherRoadHead);
             * }*/

            // Establish links between this road's new tail and other road's new head
            if (otherRoadHead != null && thisRoadTail != null)
            {
                otherRoadHead.AddOutgoingRoad(thisRoadTail);
                thisRoadTail.AddIncomingRoad(otherRoadHead);
            }

            if (thisRoadTail != null)
            {
                thisRoad = thisRoadTail;
            }
        }
    }