diff --git a/src/core/pathfinding/PathFinding.ts b/src/core/pathfinding/PathFinding.ts index 8d5193ed4..2050cc162 100644 --- a/src/core/pathfinding/PathFinding.ts +++ b/src/core/pathfinding/PathFinding.ts @@ -106,6 +106,7 @@ export class PathFinder { private curr: TileRef | null = null; private dst: TileRef | null = null; private path: TileRef[] | null = null; + private path_idx: number = 0; private aStar: AStar; private computeFinished = true; @@ -148,6 +149,7 @@ export class PathFinder { } if (this.game.manhattanDist(curr, dst) < dist) { + this.path = null; return { type: PathFindResultType.Completed, node: curr }; } @@ -156,11 +158,12 @@ export class PathFinder { this.curr = curr; this.dst = dst; this.path = null; + this.path_idx = 0; this.aStar = this.newAStar(curr, dst); this.computeFinished = false; return this.nextTile(curr, dst); } else { - const tile = this.path?.shift(); + const tile = this.path?.[this.path_idx++]; if (tile === undefined) { throw new Error("missing tile"); } @@ -172,8 +175,9 @@ export class PathFinder { case PathFindResultType.Completed: this.computeFinished = true; this.path = this.aStar.reconstructPath(); - // Remove the start tile - this.path.shift(); + + // exclude first tile + this.path_idx = 1; return this.nextTile(curr, dst); case PathFindResultType.Pending: