コード例 #1
0
    public void ProcessRoad(OpenRoad road)
    {
        Vec2i currenttile = road.origin + road.direction;

        while (IsRoad(currenttile, road.direction))
        {
            currenttile += road.direction;
        }
        ProcessCrossRoad(currenttile, road);
    }
コード例 #2
0
    public void ProcessCrossRoad(Vec2i currentTile, OpenRoad incomingRoad)
    {
        int wpIndex = openWaypoints.FindIndex(wp => wp.tilePos == currentTile);

        if (wpIndex >= 0)
        {
            // add Incoming road to existing Waypoint
            if (incomingRoad.direction == new Vec2i(0, 1))
            {
                openWaypoints[wpIndex].wayPoint.down = incomingRoad.lastWayPoint;
                incomingRoad.lastWayPoint.top        = openWaypoints[wpIndex].wayPoint;
            }
            else if (incomingRoad.direction == new Vec2i(0, -1))
            {
                openWaypoints[wpIndex].wayPoint.top = incomingRoad.lastWayPoint;
                incomingRoad.lastWayPoint.down      = openWaypoints[wpIndex].wayPoint;
            }
            else if (incomingRoad.direction == new Vec2i(1, 0))
            {
                openWaypoints[wpIndex].wayPoint.left = incomingRoad.lastWayPoint;
                incomingRoad.lastWayPoint.right      = openWaypoints[wpIndex].wayPoint;
            }
            else if (incomingRoad.direction == new Vec2i(-1, 0))
            {
                openWaypoints[wpIndex].wayPoint.right = incomingRoad.lastWayPoint;
                incomingRoad.lastWayPoint.left        = openWaypoints[wpIndex].wayPoint;
            }
            int otherRoadIndex = openRoads.FindIndex(r => r.lastWayPoint == openWaypoints[wpIndex].wayPoint && (r.direction + incomingRoad.direction) == Vec2i.zero);

            //int otherRoadIndex = openRoads.FindIndex(r => r.lastWayPoint == openWaypoints[wpIndex].wayPoint);

            // remove both Roads from the openRoads
            if (otherRoadIndex >= 0)
            {
                openRoads.RemoveAt(otherRoadIndex);
                //Debug.Log("Removed OtherRoad");
                if ((++openWaypoints[wpIndex].numCheckedRoads) >= openWaypoints[wpIndex].numAdustingRoads)
                {
                    openWaypoints.RemoveAt(wpIndex);
                }
            }
            openRoads.Remove(incomingRoad);
            //Debug.Log("Removed IncomingRoad");
            OpenWayPoint otherOpenWayPoint = openWaypoints.Find(wp => wp.wayPoint == incomingRoad.lastWayPoint);

            // TODO check why this can be null?!
            if (otherOpenWayPoint != null && (++otherOpenWayPoint.numCheckedRoads) >= otherOpenWayPoint.numAdustingRoads)
            {
                openWaypoints.Remove(otherOpenWayPoint);
            }
        }
        else
        {
            // add new OpenCrossRoad
            // openWaypoints.Add(new OpenWayPoint(currentTile, ))
            WayPoint currentWP = Instantiate(wayPointPrefab).GetComponent <WayPoint>();

            currentWP.transform.SetParent(transform);
            currentWP.gameObject.transform.position = tileMap.CellToWorld(currentTile);

            finishedWayPointList.Add(currentWP);
            // Add incomingRoad to WayPoint
            // Find New Outgoing Roads and Add them to Openroads

            int outgoingRoads = 0;

            if (incomingRoad.direction == new Vec2i(1, 0))
            {
                currentWP.left = incomingRoad.lastWayPoint;
                incomingRoad.lastWayPoint.right = currentWP;
            }
            else
            {
                if (IsWalkable(currentTile + new Vec2i(-1, 0)))
                {
                    openRoads.Add(new OpenRoad(currentTile, new Vec2i(-1, 0), currentWP));
                    outgoingRoads++;
                }
            }

            if (incomingRoad.direction == new Vec2i(-1, 0))
            {
                currentWP.right = incomingRoad.lastWayPoint;
                incomingRoad.lastWayPoint.left = currentWP;
            }
            else
            {
                if (IsWalkable(currentTile + new Vec2i(1, 0)))
                {
                    openRoads.Add(new OpenRoad(currentTile, new Vec2i(1, 0), currentWP));
                    outgoingRoads++;
                }
            }

            if (incomingRoad.direction == new Vec2i(0, 1))
            {
                currentWP.down = incomingRoad.lastWayPoint;
                incomingRoad.lastWayPoint.top = currentWP;
            }
            else
            {
                if (IsWalkable(currentTile + new Vec2i(0, -1)))
                {
                    openRoads.Add(new OpenRoad(currentTile, new Vec2i(0, -1), currentWP));
                    outgoingRoads++;
                }
            }

            if (incomingRoad.direction == new Vec2i(0, -1))
            {
                currentWP.top = incomingRoad.lastWayPoint;
                incomingRoad.lastWayPoint.down = currentWP;
            }
            else
            {
                if (IsWalkable(currentTile + new Vec2i(0, 1)))
                {
                    openRoads.Add(new OpenRoad(currentTile, new Vec2i(0, 1), currentWP));
                    outgoingRoads++;
                }
            }

            OpenWayPoint openWp = new OpenWayPoint(currentTile, currentWP, outgoingRoads);
            openWaypoints.Add(openWp);
            openWp.numCheckedRoads = 0;

            OpenWayPoint otherWaypoint = openWaypoints.Find(wp => wp.wayPoint == incomingRoad.lastWayPoint);
            openRoads.Remove(incomingRoad);
            if (++(otherWaypoint.numCheckedRoads) >= otherWaypoint.numAdustingRoads)
            {
                openWaypoints.Remove(otherWaypoint);
            }
        }
    }