@@ 85,10 85,15 @@ public class Path
/// <returns>A position to walk to.</returns>
public (short X, short Y) TakeForwardPath()
{
- if (ReachedEnd || CurrentPartIndex + 2 >= Parts.Count)
+ if (ReachedEnd)
{
return (TargetX, TargetY);
}
+ if (CurrentPartIndex + 2 >= Parts.Count)
+ {
+ CurrentPartIndex++;
+ return (TargetX, TargetY);
+ }
var zeroTile = (CurrentX, CurrentY);
var firstTile = Parts[++CurrentPartIndex];
@@ 129,11 134,11 @@ public class Path
if (xFirstDiff != 0)
{
var xRatio = xCurrentDiff / (float)xFirstDiff;
- return (yFirstDiff * xRatio) - yCurrentDiff < float.Epsilon * 10;
+ return Math.Abs((yFirstDiff * xRatio) - yCurrentDiff) < float.Epsilon;
}
var yRatio = yCurrentDiff / (float)yFirstDiff;
- return (xFirstDiff * yRatio) - xCurrentDiff < float.Epsilon * 10;
+ return Math.Abs((xFirstDiff * yRatio) - xCurrentDiff) < float.Epsilon;
}
/// <summary>
@@ 90,11 90,16 @@ public class Pathfinder
short targetY
)
{
- if (!mapInfo.IsWalkable((short)targetX, (short)targetY))
+ if (!mapInfo.IsWalkable(targetX, targetY))
{
return new NotFoundError("The requested target is not walkable, path cannot be found.");
}
+ if (x == targetX && y == targetY)
+ {
+ return new Path(mapInfo.Id, x, y, targetX, targetY, Array.Empty<(short X, short Y)>());
+ }
+
var target = (targetX, targetY);
var visited = new HashSet<(short X, short Y)>();
var offsets = new (short X, short Y)[] { (0, 1), (1, 0), (1, 1), (0, -1), (-1, 0), (-1, -1), (1, -1), (-1, 1) };
@@ 53,6 53,11 @@ public class WalkManager
return Result.FromError(pathResult);
}
+ if (pathResult.Entity.Parts.Count == 0)
+ {
+ return Result.FromSuccess();
+ }
+
var path = pathResult.Entity;
while (!path.ReachedEnd)
{
@@ 71,6 76,11 @@ public class WalkManager
return Result.FromSuccess();
}
+ if (_state.X == x && _state.Y == y)
+ {
+ return Result.FromSuccess();
+ }
+
return walkResult;
}
}