示例#1
0
        /// <summary>
        /// Calculate A* path between two Cells
        /// </summary>
        /// <param name="startLocation"></param>
        /// <param name="destination"></param>
        /// <returns>A* Path</returns>
        public List <PathNode> CalculatePath(Cell startLocation, Cell destination, bool fourNeighbours = false)
        {
            // Reset Game World's pathfinding scores to default
            for (int i = 0; i < _game.X; i++)
            {
                for (int j = 0; j < _game.Y; j++)
                {
                    _board[i, j]          = new PathNode(_game.GetCell(i, j));
                    _board[i, j].GScore   = int.MaxValue;
                    _board[i, j].CameFrom = null;
                }
            }

            // Get starting Cell, add to open list and begin AStar search
            PathNode startNode = _board[startLocation.X, startLocation.Y];
            PathNode destNode  = _board[destination.X, destination.Y];

            startNode.GScore = 0;
            startNode.HScore = CalulateMoveCost(startNode, destNode);

            openListP = new List <PathNode> {
                startNode
            };
            closedListP = new List <PathNode>();

            while (openListP.Count > 0)
            {
                PathNode currentNode = GetLowestFCost(openListP);
                if (currentNode == destNode)
                {
                    // path found!
                    return(TracePath(destNode));
                }

                openListP.Remove(currentNode);
                closedListP.Add(currentNode);

                List <PathNode> neighbours = FindNeighbours(currentNode, fourNeighbours);
                foreach (PathNode neighbour in neighbours)
                {
                    if (closedListP.Contains(neighbour))
                    {
                        continue;
                    }

                    int tenGScore = currentNode.GScore + CalulateMoveCost(currentNode, neighbour);
                    if (tenGScore < neighbour.GScore)
                    {
                        neighbour.CameFrom = currentNode;
                        neighbour.GScore   = tenGScore;
                        neighbour.HScore   = CalulateMoveCost(neighbour, destNode);

                        if (!openListP.Contains(neighbour))
                        {
                            openListP.Add(neighbour);
                        }
                    }
                }
            }
            return(null);
        }
        /// <summary>
        /// Calculate A* path between two Cells
        /// </summary>
        /// <param name="startLocation"></param>
        /// <param name="destination"></param>
        /// <returns>A* Path</returns>
        public List <Cell> CalculatePath(Cell startLocation, Cell destination)
        {
            // Reset Game World's pathfinding scores to default
            for (int i = 0; i < _game.X; i++)
            {
                for (int j = 0; j < _game.Y; j++)
                {
                    Cell cell = _game.GetCell(i, j);
                    cell.GScore   = int.MaxValue;
                    cell.CameFrom = null;
                }
            }

            // Get starting Cell, add to open list and begin AStar search
            Cell startCell = _game.GetCell(startLocation);
            Cell destCell  = _game.GetCell(destination);

            startCell.GScore = 0;
            startCell.HScore = CalulateMoveCost(startCell, destCell);

            openList = new List <Cell> {
                startCell
            };
            closedList = new List <Cell>();

            while (openList.Count > 0)
            {
                Cell currentCell = GetLowestFCost(openList);
                if (currentCell == destCell)
                {
                    // path found!
                    return(TracePath(destCell));
                }

                openList.Remove(currentCell);
                closedList.Add(currentCell);

                List <Cell> neighbours = FindNeighbours(currentCell);
                foreach (Cell neighbour in neighbours)
                {
                    if (closedList.Contains(neighbour))
                    {
                        continue;
                    }

                    int tenGScore = currentCell.GScore + CalulateMoveCost(currentCell, neighbour);
                    if (tenGScore < neighbour.GScore)
                    {
                        neighbour.CameFrom = currentCell;
                        neighbour.GScore   = tenGScore;
                        neighbour.HScore   = CalulateMoveCost(neighbour, destCell);

                        if (!openList.Contains(neighbour))
                        {
                            openList.Add(neighbour);
                        }
                    }
                }
            }
            return(null);
        }