public List<Node> GetNeighbours(Node node)
{
List<Node> neighbours = new List<Node>();
bool rightBlocked = true;
bool topBlocked = true;
bool leftBlocked = true;
bool bottomBlocked = true;
//add adjacent neighbors
for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
if (x == 0 && y == 0)
continue;
if (x == 0 || y == 0)
{
int checkX = node.gridX + x;
int checkY = node.gridY + y;
if (checkX >= 0 && checkX < gridSizeX && checkY >= 0 && checkY < gridSizeY)
{
// If directly adjacent, always add as neighbor
neighbours.Add(grid[checkX,checkY]);
// Test for blocked nodes
if (x == 1)
rightBlocked = !grid[checkX,checkY].walkable;
else if (y == 1)
topBlocked = !grid[checkX,checkY].walkable;
else if (x == -1)
leftBlocked = !grid[checkX,checkY].walkable;
else if (y == -1)
bottomBlocked = !grid[checkX,checkY].walkable;
}
}
}
}
// Diagonal bools are derived from adjacent
bool topRightBlocked = rightBlocked || topBlocked;
bool topLeftBlocked = topBlocked || leftBlocked;
bool bottomLeftBlocked = leftBlocked || bottomBlocked;
bool bottomRightBlocked = bottomBlocked || rightBlocked;
//add diagonal neighbors
for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
if (x == 0 && y == 0)
continue;
if (x == 0 || y == 0)
continue;
int checkX = node.gridX + x;
int checkY = node.gridY + y;
if (checkX >= 0 && checkX < gridSizeX && checkY >= 0 && checkY < gridSizeY)
{
// if not a diagonal or is a non-blocked diagonal, add to neighbor list
if ( (x == 1 && y == 1 && !topRightBlocked)
|| (x == -1 && y == 1 && !topLeftBlocked)
|| (x == -1 && y == -1 && !bottomLeftBlocked)
|| (x == 1 && y == -1 && !bottomRightBlocked) )
{
neighbours.Add(grid[checkX,checkY]);
}
}
}
}
return neighbours;
}