public List <GridTile> FindPath(GridTile[,] _grid, GridTile startTile, GridTile endTile) { grid = _grid; if (grid == null || startTile == null || endTile == null) { return(null);// Invalid Path } openList = new List <GridTile> { startTile }; closedList = new List <GridTile>(); for (int x = 0; x < grid.GetLength(0); x++) { for (int y = 0; y < grid.GetLength(1); y++) { GridTile gridTile = grid[x, y]; gridTile.GCost = int.MaxValue; gridTile.CalculateFCost(); gridTile.CameFromTile = null; } } startTile.GCost = 0; startTile.HCost = CalculateDistanceCost(startTile, endTile); startTile.CalculateFCost(); while (openList.Count > 0) { GridTile currentTile = GetLowestFCostTile(openList); if (currentTile == endTile) { // Reached final node //visual return(CalculatePath(endTile)); } openList.Remove(currentTile); closedList.Add(currentTile); foreach (GridTile neighbourTile in GetNeighbourList(currentTile)) { if (closedList.Contains(neighbourTile)) { continue; } if (!neighbourTile.IsWalkable) { closedList.Add(neighbourTile); continue; } int tentativeGCost = currentTile.GCost + CalculateDistanceCost(currentTile, neighbourTile); if (tentativeGCost < neighbourTile.GCost) { neighbourTile.CameFromTile = currentTile; neighbourTile.GCost = tentativeGCost; neighbourTile.HCost = CalculateDistanceCost(neighbourTile, endTile); neighbourTile.CalculateFCost(); if (!openList.Contains(neighbourTile)) { openList.Add(neighbourTile); } } //visual //PathfindingDebugStepVisual.Instance.TakeSnapshot(grid, currentTile, openList, closedList); } } // Out of nodes on the openList return(null); }