当没有障碍物移动或从障碍物的顶部移动到障碍物的侧面时,我的寻路工作正常,但是当它需要找到从障碍物顶部到底部的路径时,它太慢了。
我相当确定这是我对每个循环进行排序或通过封闭集循环的方式,但我不确定如何避免这种情况,因为 Windows phone 7 上没有 SortedLists
//This Vivid Destination means it doesn't have to find the exact location
//which is helpful as it is continuous environment
Rectangle vividDestination = new Rectangle((int)character.destination.X - 10, (int)character.destination.Y - 10, 20, 20);
while (!vividDestination.Contains(Maths.vectorToPoint(OpenNodes[0].position)))
{
Node Current = OpenNodes[0];
OpenNodes.RemoveAt(0);
ClosedNodes.Add(Current);
Current.calculateNeightbours();
foreach (Node neighbour in Current.neighbours)
{
neighbour.parents = Current.parents + 1;
//The cost of the node is the amount of parent nodes + the distance to destination
neighbour.cost = calculateCost(neighbour.position, character.destination, neighbour.parents);
for (int i = 0; i < OpenNodes.Count; i++)
{
//Round Vector rounds the floats in the Vector to the nearest integer
if (Maths.roundVector(OpenNodes[i].position) == Maths.roundVector(neighbour.position) && OpenNodes[i].parents < neighbour.parents)
{
break;
}
else
{
OpenNodes.RemoveAt(i);
OpenNodes.Add(neighbour);
i--;
break;
}
}
bool closedNode = false;
for (int i = 0; i < ClosedNodes.Count; i++)
{
if (Maths.roundVector(ClosedNodes[i].position) == Maths.roundVector(neighbour.position))
{
closedNode = true;
break;
}
}
if (!closedNode)
{
OpenNodes.Add(neighbour);
}
}
OpenNodes = OpenNodes.OrderBy(item => item.cost).ToList();
}