public override IEnumerable <(GridPosition, GridPosition)> AllConnections(GridSize gridSize) { for (var a = gridSize.minA; a <= gridSize.maxA; a++) { for (var b = gridSize.minB; b <= gridSize.maxB; b++) { var p = new GridPosition(a, b); var even = p.b % 2 == 0; if (even) { if (p.a > gridSize.minA && p.b < gridSize.maxB) { yield return(p, new GridPosition(p.a - 1, p.b + 1)); } if (p.b < gridSize.maxB) { yield return(p, new GridPosition(p.a, p.b + 1)); } if (p.a < gridSize.maxA) { yield return(p, new GridPosition(p.a + 1, p.b)); } } else { if (p.b < gridSize.maxB) { yield return(p, new GridPosition(p.a, p.b + 1)); } if (p.a < gridSize.maxA && p.b < gridSize.maxB) { yield return(p, new GridPosition(p.a + 1, p.b + 1)); } if (p.a < gridSize.maxA) { yield return(p, new GridPosition(p.a + 1, p.b)); } } } } }
public override IEnumerable <(GridPosition, GridPosition)> AllConnections(GridSize gridSize) { for (var a = gridSize.minA; a <= gridSize.maxA; a++) { for (var b = gridSize.minB; b <= gridSize.maxB; b++) { var pos = new GridPosition(a, b); if (a < gridSize.maxA) { yield return(pos, new GridPosition(pos.a + 1, pos.b)); } if (b < gridSize.maxB) { yield return(pos, new GridPosition(pos.a, pos.b + 1)); } } } }
public static GridPosition[] FindPath(GridPosition from, GridPosition to, GridGeometry geometry, GridSize size) { var visitedNodes = new GridNodesData <bool>(size); var traces = new GridNodesData <GridPosition>(size); var counter = 0; visitedNodes[from] = true; var nextSteps = new List <GridPosition> { from }; var currentStep = new List <GridPosition>(); while (nextSteps.Count != 0 && !visitedNodes[to]) { var t = currentStep; currentStep = nextSteps; nextSteps = t; nextSteps.Clear(); foreach (var p in currentStep) { foreach (var n in geometry.NeighborPositions(p, size)) { if (visitedNodes[n]) { continue; } // TODO can check for the walls between p and n here visitedNodes[n] = true; traces[n] = p; nextSteps.Add(n); } } counter++; } if (!visitedNodes[to]) { Debug.LogError("Failed to find a path"); return(null); } var cursor = to; var result = new GridPosition[counter + 1]; result[counter] = cursor; while (cursor != from) { counter--; cursor = traces[cursor]; result[counter] = cursor; } return(result); }
public abstract IEnumerable <(GridPosition, GridPosition)> AllConnections(GridSize gridSize);
public abstract IEnumerable <GridPosition> NeighborPositions(GridPosition p, GridSize gridSize);
public override IEnumerable <GridPosition> NeighborPositions(GridPosition p, GridSize gridSize) { var even = p.b % 2 == 0; if (even) { if (p.a > gridSize.minA && p.b < gridSize.maxB) { yield return(new GridPosition(p.a - 1, p.b + 1)); } if (p.b < gridSize.maxB) { yield return(new GridPosition(p.a, p.b + 1)); } if (p.a > gridSize.minA) { yield return(new GridPosition(p.a - 1, p.b)); } if (p.a < gridSize.maxA) { yield return(new GridPosition(p.a + 1, p.b)); } if (p.a > gridSize.minA && p.b > gridSize.minB) { yield return(new GridPosition(p.a - 1, p.b - 1)); } if (p.b > gridSize.minB) { yield return(new GridPosition(p.a, p.b - 1)); } } else { if (p.b < gridSize.maxB) { yield return(new GridPosition(p.a, p.b + 1)); } if (p.a < gridSize.maxA && p.b < gridSize.maxB) { yield return(new GridPosition(p.a + 1, p.b + 1)); } if (p.a > gridSize.minA) { yield return(new GridPosition(p.a - 1, p.b)); } if (p.a < gridSize.maxA) { yield return(new GridPosition(p.a + 1, p.b)); } if (p.b > gridSize.minB) { yield return(new GridPosition(p.a, p.b - 1)); } if (p.a < gridSize.maxA && p.b > gridSize.minB) { yield return(new GridPosition(p.a + 1, p.b - 1)); } } }
public override IEnumerable <GridPosition> NeighborPositions(GridPosition pos, GridSize gridSize) { if (pos.a > gridSize.minA) { yield return(new GridPosition(pos.a - 1, pos.b)); } if (pos.b > gridSize.minB) { yield return(new GridPosition(pos.a, pos.b - 1)); } if (pos.a < gridSize.maxA) { yield return(new GridPosition(pos.a + 1, pos.b)); } if (pos.b < gridSize.maxB) { yield return(new GridPosition(pos.a, pos.b + 1)); } }