Skip to content

Instantly share code, notes, and snippets.

@tacoe
Created July 22, 2018 21:44
Show Gist options
  • Save tacoe/0c76b13f263037a7a3f58eb28b4bd495 to your computer and use it in GitHub Desktop.
Save tacoe/0c76b13f263037a7a3f58eb28b4bd495 to your computer and use it in GitHub Desktop.
public static IEnumerable<GridPoint> Neighbors(TileType[,] graph, GridPoint center)
{
// NESW
GridPoint pt1 = new GridPoint(center.x, center.y - 1);
if (IsValidNeighbor(graph, pt1))
yield return pt1;
GridPoint pt2 = new GridPoint(center.x - 1, center.y);
if (IsValidNeighbor(graph, pt2))
yield return pt2;
GridPoint pt3 = new GridPoint(center.x, center.y + 1);
if (IsValidNeighbor(graph, pt3))
yield return pt3;
GridPoint pt4 = new GridPoint(center.x + 1, center.y);
if (IsValidNeighbor(graph, pt4))
yield return pt4;
// diagonals: doublecheck we're not shifting through diagonal walls
GridPoint pt = new GridPoint(center.x - 1, center.y - 1);
if (IsValidNeighbor(graph, pt) && (IsValidNeighbor(graph, pt1) || IsValidNeighbor(graph, pt2)))
yield return pt;
pt = new GridPoint(center.x - 1, center.y + 1);
if (IsValidNeighbor(graph, pt) && (IsValidNeighbor(graph, pt2) || IsValidNeighbor(graph, pt3)))
yield return pt;
pt = new GridPoint(center.x + 1, center.y + 1);
if (IsValidNeighbor(graph, pt) && (IsValidNeighbor(graph, pt3) || IsValidNeighbor(graph, pt4)))
yield return pt;
pt = new GridPoint(center.x + 1, center.y - 1);
if (IsValidNeighbor(graph, pt) && (IsValidNeighbor(graph, pt4) || IsValidNeighbor(graph, pt1)))
yield return pt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment