示例#1
0
    public IEnumerator <YieldInstruction> CalculatePath()
    {
        if (Running)
        {
            yield break;
        }

        Running    = true;
        MasterPath = new Dictionary <int, Dictionary <int, Path> >();

        //for each entrance find path to every exit
        foreach (int i in Entrances)
        {
            Square     startSquare     = Grid.squares[i];
            Vector2Int dirFromEntrance = new Vector2Int(0, 1);

            if (GridUtilities.IsLeft(i))
            {
                dirFromEntrance = new Vector2Int(1, 0);
            }
            else if (GridUtilities.IsRight(i))
            {
                dirFromEntrance = new Vector2Int(-1, 0);
            }
            else if (GridUtilities.IsTop(i))
            {
                dirFromEntrance = new Vector2Int(0, -1);
            }
            else if (!GridUtilities.IsBottom(i))
            {
                Debug.Log("Enterance must be on edge of board");
            }

            startSquare = GridUtilities.GetNextSquare(startSquare, dirFromEntrance);

            if (startSquare.hasBox)
            {
                Debug.LogWarning("Enterance not valid");
                continue;
            }

            Dictionary <int, Path> currentPath = new Dictionary <int, Path>();
            foreach (int j in Exits)
            {
                Branches        = 0;
                MaxPath         = (Grid.width - 2) * (Grid.height - 2) + 2;
                LowestTotalPath = MaxPath;

                PathIds = new List <int>();

                Path head = new Path();

                Square current = startSquare;
                endSquare = Grid.squares[j];

                head.Current       = startSquare;
                head.FutureSquares = new List <Path>();

                Path newPath = new Path(current, head);
                head.FutureSquares.Add(newPath);

                forward = dirFromEntrance;
                if (GridUtilities.IsLeft(j))
                {
                    forward = new Vector2Int(-1, 0);
                }
                else if (GridUtilities.IsRight(j))
                {
                    forward = new Vector2Int(1, 0);
                }
                else if (GridUtilities.IsTop(j))
                {
                    forward = new Vector2Int(0, 1);
                }
                else if (GridUtilities.IsBottom(j))
                {
                    forward = new Vector2Int(0, -1);
                }
                else
                {
                    Debug.LogError("Exit must be on edge of board");
                }

                Square squareNextToExit = GridUtilities.GetNextSquare(endSquare, -forward);

                if (squareNextToExit.hasBox)
                {
                    Debug.LogWarning("Exit not valid");
                    continue;
                }

                left      = new Vector2Int(-forward.y, forward.x);
                diagLeft  = left + forward;
                right     = -left;
                diagRight = right + forward;
                back      = -forward;

                DownPath(newPath, 1, dirFromEntrance);

                if (newPath.FutureSquares.Count == 0)
                {
                    Debug.LogWarning($"No routes where found to exit {endSquare.gridCoord}");
                }
                else
                {
                    currentPath.Add(j, head);
                }

                yield return(null);
            }

            if (currentPath.Count != 0)
            {
                //add all paths from current entrance to all exits
                MasterPath.Add(i, currentPath);
            }
        }

        Running = false;
    }