private IEnumerator GeneratePaths(List <Vector2Int> nodes, Action <int> callback) { if (nodes.Count <= 0) { Debug.LogWarning("There are no nodes for which generate paths"); yield break; } int attempts = 0; int maxAttempts = nodes.Count * 2; List <LevelNavigation.PathNode> newPath = new List <LevelNavigation.PathNode>(); List <bool> nodesUsed = Enumerable.Repeat(false, nodes.Count).ToList(); int path = 0; int nodesUnusable = 0; Status = LevelGenerationStatus.Paths; yield return(null); for (path = 0; path + nodesUnusable + 1 < nodes.Count && attempts < maxAttempts; path++, attempts++) { newPath = LevelNavigation.FindPath(nodes[path], nodes[path + nodesUnusable + 1], newLevel); if (newPath == null) { path--; nodesUnusable += (path + nodesUnusable < nodes.Count - 2) ? 1 : 0; } else { ApplyPath(newPath); foreach (LevelNavigation.PathNode p in newPath.Where(tmpP => nodes.Contains(tmpP.GetCell())).ToList()) { nodesUsed[nodes.IndexOf(p.GetCell())] = true; } path += nodesUnusable; nodesUnusable = 0; } genProgression = Mathf.Max((float)path / nodes.Count, (float)attempts / maxAttempts); yield return(null); } Debug.Log($"Generated {path}/{nodes.Count - 1} paths in {attempts}/{maxAttempts} attempts"); nodes = RemoveUnusedNodes(nodes, nodesUsed); newLevel.SetTile(nodes[0], Element.Start); newLevel.SetTile(nodes[nodes.Count - 1], Element.End); callback?.Invoke(nodes.Count); yield break; }
/// <summary> /// Update the attached text to the distance between the specified cell and the level end. /// </summary> /// <param name="startX">Start cell X.</param> /// <param name="startY">Start cell Y.</param> public void UpdateValue(int startX, int startY) { if (text == null) { return; } if (!LevelNavigation.IsReady) { return; } List <LevelNavigation.PathNode> path = LevelNavigation.FindPath(new Vector2Int(startX, startY), LevelManager.Main.EndCell); if (path != null) { text.text = (path.Where(p => LevelManager.Main.Grid.GetTile(p.GetCell()).ToDirection() == Direction.All).ToList().Count - 1).ToString(); } else { text.text = "N/A"; } }