// Use this for initialization void Start() { if (instance != null) { Debug.LogError("Error: Cannot have two GameCubeManagers"); } instance = this; this.grid = new ConceptualGrid(); //Either spawn the cudes here with code //Or place by hand and find the cubes foreach (GameCube gc in transform.Find("GameCubes").GetComponentsInChildren <GameCube>()) { this.grid.AddCube(gc.transform.position, gc); } }
public AStarPath(GameCube start, GameCube end) { ConceptualGrid grid = GameCubeManager.Instance.Grid; List <GameCube> allCubes = grid.AllCubes; List <GameCube> closedSet = new List <GameCube> (); PriorityQueue <float, GameCube> openSet = new PriorityQueue <float, GameCube> (); openSet.Enqueue(0, start); //Essentially a linked list Dictionary <GameCube, GameCube> path = new Dictionary <GameCube, GameCube> (); Dictionary <GameCube, float> g_score = new Dictionary <GameCube, float> (); foreach (GameCube h in allCubes) { g_score [h] = Mathf.Infinity; } g_score [start] = 0; Dictionary <GameCube, float> f_score = new Dictionary <GameCube, float> (); foreach (GameCube h in allCubes) { f_score [h] = Mathf.Infinity; } f_score [start] = heuristicCostEstimate(start, end); while (!openSet.IsEmpty) { GameCube current = openSet.Dequeue().Value; if (current == end) { RecontructPath(path, current); return; } closedSet.Add(current); List <GameCube> neighbours = grid.GetNeighbours(current); foreach (GameCube neighbour in neighbours) { if (neighbour.MoveCost == Mathf.Infinity) { continue; } if (closedSet.Contains(neighbour)) { continue; } float tentative_g_score = g_score [current] + current.MoveCost; if (openSet.Contains(neighbour) && tentative_g_score >= g_score [neighbour]) { continue; } path [neighbour] = current; g_score [neighbour] = tentative_g_score; f_score[neighbour] = g_score [neighbour] + heuristicCostEstimate(neighbour, end); if (openSet.Contains(neighbour) == false) { openSet.Enqueue(f_score [neighbour], neighbour); } } } //if we reach this case, it means all nodes have been closed by final destination wasn't found Debug.Log("Path failed"); }