public void FindPath(Cell startCell, Cell goalCell, List<List<Cell>> map, bool targetCellMustBeFree)
{
_map = map.Select(a => a.ToArray()).ToArray(); ;
_mapWidth = _map.GetLength(0);
_mapHeight = _map.GetLength(1);
_start = new Node((int)startCell.gridPosition.x, (int)startCell.gridPosition.y, 0, 0, 0, null, startCell);
end = new Node((int)goalCell.gridPosition.x, (int)goalCell.gridPosition.y, 0, 0, 0, null, goalCell);
_openList.Add(_start);
bool keepSearching = true;
bool pathExists = true;
while ((keepSearching) && (pathExists))
{
Node currentNode = ExtractBestNodeFromOpenList();
if (currentNode == null)
{
pathExists = false;
break;
}
_closeList.Add(currentNode);
if (NodeIsGoal(currentNode))
keepSearching = false;
else
{
if (targetCellMustBeFree)
FindValidFourNeighbours(currentNode);
else
FindValidFourNeighboursIgnoreTargetCell(currentNode);
foreach (Node neighbour in _neighbours)
{
if (FindInCloseList(neighbour) != null)
continue;
Node inOpenList = FindInOpenList(neighbour);
if (inOpenList == null)
{
_openList.Add(neighbour);
}
else
{
if (neighbour.G < inOpenList.G)
{
inOpenList.G = neighbour.G;
inOpenList.MoveCost = inOpenList.G + inOpenList.H;
inOpenList.Parent = currentNode;
}
}
}
}
}
if (pathExists)
{
Node n = FindInCloseList(end);
while (n != null)
{
FinalPath.Add(n);
n = n.Parent;
}
}
}