/// <summary>
/// Traces the calculated path from the end node to the start.
/// This will build an array (<see cref="path)"/> of the nodes this path will pass through and also set the <see cref="vectorPath"/> array to the <see cref="path"/> arrays positions.
/// Assumes the <see cref="vectorPath"/> and <see cref="path"/> are empty and not null (which will be the case for a correctly initialized path).
/// </summary>
protected virtual void Trace(PathNode from)
{
// Current node we are processing
PathNode c = from;
int count = 0;
while (c != null)
{
c = c.parent;
count++;
if (count > 2048)
{
Debug.LogWarning("Infinite loop? >2048 node path. Remove this message if you really have that long paths (Path.cs, Trace method)");
break;
}
}
// Ensure capacities for lists
AstarProfiler.StartProfile("Check List Capacities");
if (path.Capacity < count)
{
path.Capacity = count;
}
if (vectorPath.Capacity < count)
{
vectorPath.Capacity = count;
}
AstarProfiler.EndProfile();
c = from;
for (int i = 0; i < count; i++)
{
path.Add(c.node);
c = c.parent;
}
// Reverse
int half = count / 2;
for (int i = 0; i < half; i++)
{
var tmp = path[i];
path[i] = path[count - i - 1];
path[count - i - 1] = tmp;
}
for (int i = 0; i < count; i++)
{
vectorPath.Add((Vector3)path[i].position);
}
}