public void CalculateFill2D(List <Vector2Int> path, Vector2Int from, bool verticalMovement) { Heap open = new Heap(heapSize); HashSet <T> closed = new HashSet <T>(); T start = grid[from.x, from.y], current; int xLength = grid.GetLength(0), yLength = grid.GetLength(1); open.Add(grid[from.x, from.y]); Func <int, int, bool> isOutOfBounds = delegate(int x, int y) { return(x < 0 || x >= xLength || y < 0 || y >= yLength); }; Action <int, int> tryAddNeighbour = delegate(int x, int y) { if (isOutOfBounds(x, y)) { return; } if (!grid[x, y].Walkable) { return; } if (closed.Contains(grid[x, y])) { return; } if (open.Contains(grid[x, y])) { return; } open.Add(grid[x, y]); }; Vector2Int currentPosition; while (open.Count > 0) { current = open.Get(); closed.Add(current); currentPosition = current.Position; tryAddNeighbour(currentPosition.x, currentPosition.y + 1); tryAddNeighbour(currentPosition.x + 1, currentPosition.y); tryAddNeighbour(currentPosition.x, currentPosition.y - 1); tryAddNeighbour(currentPosition.x - 1, currentPosition.y); if (!verticalMovement) { continue; } tryAddNeighbour(currentPosition.x + 1, currentPosition.y + 1); tryAddNeighbour(currentPosition.x + 1, currentPosition.y - 1); tryAddNeighbour(currentPosition.x - 1, currentPosition.y - 1); tryAddNeighbour(currentPosition.x - 1, currentPosition.y + 1); } foreach (T node in closed) { path.Add(node.Position); } }
public void Calculate2D(List <Vector2Int> path, Vector2Int from, Vector2Int to, bool verticalMovement) { Heap open = new Heap(heapSize); HashSet <T> closed = new HashSet <T>(); T start = grid[from.x, from.y], end = grid[to.x, to.y]; int xLength = grid.GetLength(0), yLength = grid.GetLength(1); open.Add(grid[from.x, from.y]); T current = null; Func <int, int, bool> isOutOfBounds = delegate(int x, int y) { return(x < 0 || x >= xLength || y < 0 || y >= yLength); }; Func <T, T, T> setItem = delegate(T item, T parent) { item.Parent = parent.Position; item.G = Vector2.Distance(item.Position, start.Position); item.H = Vector2.Distance(item.Position, end.Position); return(item); }; Action <int, int> TryAddNeighbour = delegate(int x, int y) { x += current.Position.x; y += current.Position.y; if (isOutOfBounds(x, y)) { return; } if (!grid[x, y].Walkable) { return; } if (closed.Contains(grid[x, y])) { return; } if (open.Contains(grid[x, y])) { return; } // Set variables grid[x, y] = setItem(grid[x, y], current); open.Add(grid[x, y]); }; // Keep getting current's parent until the start position has been found while (open.Count > 0) { current = open.Get(); closed.Add(current); if (current.Position == end.Position) { // When the start position is found it means that the complete path has been added while (!current.Equals(start)) { path.Add(current.Position); current = grid[current.Parent.x, current.Parent.y]; } return; } // Top TryAddNeighbour(0, 1); // Right TryAddNeighbour(1, 0); // Bottom TryAddNeighbour(0, -1); //Left TryAddNeighbour(-1, 0); if (verticalMovement) { // Top Right TryAddNeighbour(1, 1); // Right Bottom TryAddNeighbour(1, -1); // Bottom Left TryAddNeighbour(-1, -1); //Left Top TryAddNeighbour(-1, 1); } } return; }