static HashSet <Vector3Int> OpenCells(Tilemap map, Vector3Int start, Vector3Int goal) { Dictionary <Vector3Int, int> counts = new Dictionary <Vector3Int, int>(); counts.Add(goal, 0); HashSet <Vector3Int> openCells = new HashSet <Vector3Int>(); float minDist = Mathf.Infinity; int minCount = int.MaxValue; map.BreadthFirstTraversal(goal, Utils.FourDirections, (current, next) => { float dist = Vector3Int.Distance(goal, next); int count = counts[current] + 1; counts[next] = count; if ((map.IsCellEmpty(next) || next == start) && dist <= minDist) { minDist = dist; minCount = count; openCells.Add(next); } return(count <= minCount && map.IsInBounds(next)); }); return(openCells); }
/// <summary> /// A Breadth First Traversal of nodes in a grid. /// </summary> /// <param name="position">Starting position of the traversal</param> /// <param name="isConnected">A function to decide if the next node is connected to the current node</param> public static List <Vector3> BreadthFirstTraversal(this Tilemap tilemap, Vector3 position, Func <Vector3Int, Vector3Int, bool> isConnected) { return(tilemap.BreadthFirstTraversal(position, Utils.FourDirections, isConnected)); }