Optimized MultilevelAStarEx::generate_path()

This commit is contained in:
2024-06-05 04:12:16 +02:00
parent 100c66991b
commit d3d7a0013f

View File

@@ -222,11 +222,19 @@ bool MultilevelAStarEx::can_move(const Node *current, int x, int y) const
TypedArray<Vector2i> MultilevelAStarEx::generate_path(const Node *current) const
{
TypedArray<Vector2i> arr;
// count the number of nodes in the path
int cnt = 0;
for (const Node *n = current; n != nullptr; n = n->parent) cnt++;
cnt--; // because we're not adding the starting node
TypedArray<Vector2i> arr;
arr.resize(cnt);
int i = cnt;
while (current->parent != nullptr)
{
arr.insert(0, Vector2i(current->x, current->y) + _trans);
arr[--cnt] = Vector2i(current->x, current->y) + _trans;
current = current->parent;
}