I am using a coroutine to break up a path finding algorithm (A*) I am using. The algorithm runs fine when not in a coroutine. The function is:
private IEnumerator C_CreatePath(PathVert startVert, PathVert endVert)
{
float startTime = Time.time;
List open = new List();
List closed = new List();
open.Add(startVert);
//Set deafult values before starting algorithm
for (int row = 0; row < verts.GetLength(0); row++)
{
for (int col = 0; col < verts.GetLength(1); col++)
{
PathVert vert = verts[row, col];
vert.gCost = int.MaxValue;
vert.CalcFCost();
vert.parentVert = null;
}
}
startVert.gCost = 0;
startVert.hCost = CalcDistanceCost(startVert, endVert);
startVert.CalcFCost();
while (open.Count > 0)
{
PathVert currVert = GetLowestFCostVert(open);
if (!currVert.isWalkable){ continue; }
if (currVert == endVert)
{
List vertList = CalcPath(endVert);
path = new Path(vertList);
isDone = true;
break;
}
open.Remove(currVert);
closed.Add(currVert);
List neighbors = GetVertNeighbors(currVert);
foreach (PathVert neighbor in neighbors)
{
if (closed.Contains(neighbor) || !neighbor.isWalkable){ continue; }
int tentativeGCost = currVert.gCost + CalcDistanceCost(currVert, neighbor);
if (tentativeGCost < neighbor.gCost)
{
neighbor.parentVert = currVert;
neighbor.gCost = tentativeGCost;
neighbor.hCost = CalcDistanceCost(neighbor, endVert);
neighbor.CalcFCost();
if (!open.Contains(neighbor)){
open.Add(neighbor);
}
}
}
yield return null;
}
}
↧