GridManager.Pathfind C# (CSharp) Method

Pathfind() public method

public Pathfind ( ) : void
return void
    public void Pathfind()
    {
        // Find path from hero to monster. First, get coordinates of hero.

        Point startingPoint = FindCode( TileContent.Finish );

        int pointX = startingPoint.x;
        int pointY = startingPoint.y;
        if (pointX == -1 || pointY == -1) return;

        //starts at distance of 0.

        squares[ pointX, pointY ].DistanceSteps = 0;

        while (true)
        {
            bool madeProgress = false;

            // Look at each square on the board.

            foreach ( Point mainPoint in AllSquares() )
            {
                int x = mainPoint.x;
                int y = mainPoint.y;

                // If the square is open, look through valid moves given the coordinates of that square.

                if ( SquareOpen(x, y) )
                {
                    int passHere = squares[ x, y ].DistanceSteps;

                    foreach ( Point movePoint in ValidMoves( x, y ))
                    {
                        int newX = movePoint.x;
                        int newY = movePoint.y;
                        int newPass = passHere + 1;

                        if( squares[newX, newY].DistanceSteps > newPass )
                        {
                            squares[newX, newY].DistanceSteps = newPass;
                            madeProgress = true;
                        }
                    }
                }
            }
            if (!madeProgress)
            {
                break;
            }
        }
    }

Usage Example

Esempio n. 1
0
    public void RecalcPath()
    {
        GRIDMANAGER.ClearLogic();
        GRIDMANAGER.Pathfind();
        //  GRIDMANAGER.HighlightPath();

        //	GRIDMANAGER.DrawBoard( this );
    }
All Usage Examples Of GridManager::Pathfind