private void AddAdjacentCellToOpenList(Node parentNode, int columnOffset, int rowOffset, int gCost)
{
var adjacentCellIndex = _map.GetAdjacentCellIndex(parentNode.CellIndex, columnOffset, rowOffset);
// ignore unwalkable nodes (or nodes outside the grid)
if (adjacentCellIndex == -1)
return;
// ignore nodes on the closed list
if (_closedList.Any(n => n.CellIndex == adjacentCellIndex))
return;
var adjacentNode = _openList.SingleOrDefault(n => n.CellIndex == adjacentCellIndex);
if (adjacentNode != null)
{
if(parentNode.G + gCost < adjacentNode.G)
{
adjacentNode.Parent = parentNode;
adjacentNode.G = parentNode.G + gCost;
adjacentNode.F = adjacentNode.G + adjacentNode.H;
}
return;
}
var node = new Node(adjacentCellIndex, parentNode) { G = gCost, H = _map.GetDistance(adjacentCellIndex, _map.TargetCell) };
node.F = node.G + node.H;
_openList.Add(node);
}