// This co-routine performs the path finding and invokes another coroutine // to perform player movement animation private IEnumerator FindPath() { // Coroutine will be called again in moveDuration seconds YieldInstruction wfs = new WaitForSeconds(moveDuration); // The graph representation of our tile world TileWorldGraph graph = new TileWorldGraph(world); // Start player movement loop while (PlayerPos != GoalPos) { // Perform path finding if (pathFindingType == PathFindingType.Dijkstra) { // Use Dijkstra algorithm path = Dijkstra.GetShortestPath( graph, Vec2Ind(PlayerPos, world.GetLength(0)), Vec2Ind(GoalPos, world.GetLength(0))); } else { // Use A* algorithm path = AStar.GetPath( graph, Vec2Ind(PlayerPos, world.GetLength(0)), Vec2Ind(GoalPos, world.GetLength(0)), HeuristicForAStar); } // Was a path found? if (path == null) { // If not, update flag ValidPath = false; } else { // A valid path was found, update flag ValidPath = true; // Get an enumerator for the connection enumerable (it must be // disposed of, as such we put it in a using block) using (IEnumerator <IConnection> conns = path.GetEnumerator()) { // Did the path finder return any connection at all? if (conns.MoveNext()) { // If so, move player towards the destination node in the // first connection StartCoroutine(MovePlayer( conns.Current.ToNode, moveDuration / 2)); } } } // Return again after some fixed amount of time yield return(wfs); } // If we got here, it means the goal was reached! GoalReached = true; }
// This co-routine performs the path finding and invokes another coroutine // to perform player movement animation private IEnumerator FindPath() { // Coroutine will be called again in moveDuration seconds YieldInstruction wfs = new WaitForSeconds(moveDuration); // The graph representation of our tile world TileWorldGraph graph = new TileWorldGraph(world); // Start player movement loop while (PlayerPos != GoalPos) { // Pathfinder to use IPathFinder pathFinder = knownPathFinders[(int)pathFindingType]; // Perform path finding path = pathFinder.FindPath( graph, Vec2Ind(PlayerPos, world.GetLength(0)), Vec2Ind(GoalPos, world.GetLength(0))); // Show fill? if (showFill) { foreach (TileBehaviour tile in world) { tile.UsedForFill = false; } foreach (int ind in pathFinder.FillOpen()) { Vector2Int tilePos = Ind2Vec(ind, world.GetLength(0)); world[tilePos.x, tilePos.y].UsedForFill = true; } foreach (int ind in pathFinder.FillClosed()) { Vector2Int tilePos = Ind2Vec(ind, world.GetLength(0)); world[tilePos.x, tilePos.y].UsedForFill = true; } } // Was a path found? if (path == null) { // If not, update flag ValidPath = false; } else { // A valid path was found, update flag ValidPath = true; // Get an enumerator for the connection enumerable (it must be // disposed of, as such we put it in a using block) using (IEnumerator <IConnection> conns = path.GetEnumerator()) { // Did the path finder return any connection at all? if (conns.MoveNext()) { // If so, move player towards the destination node in the // first connection StartCoroutine(MovePlayer( conns.Current.ToNode, moveDuration / 2)); } } } // Return again after some fixed amount of time yield return(wfs); } // If we got here, it means the goal was reached! GoalReached = true; }