diff --git a/Demos/PathfindingDemo.cs b/Demos/PathfindingDemo.cs index 42b0ba2..9895532 100644 --- a/Demos/PathfindingDemo.cs +++ b/Demos/PathfindingDemo.cs @@ -20,7 +20,9 @@ namespace Demos { public PathfindingDemo(MlemGame game) : base(game) { } - private void Init() { + private async void Init() { + this.path = null; + // generate a simple random world for testing, where true is walkable area, and false is a wall var random = new Random(); this.world = new bool[50, 50]; @@ -48,7 +50,7 @@ namespace Demos { // Now find a path from the top left to the bottom right corner and store it in a variable // If no path can be found after the maximum amount of tries (10000 by default), the pathfinder will abort and return no path (null) - var foundPath = this.pathfinder.FindPath(Point.Zero, new Point(49, 49)); + var foundPath = await this.pathfinder.FindPathAsync(Point.Zero, new Point(49, 49)); this.path = foundPath != null ? foundPath.ToList() : null; // print out some info diff --git a/MLEM/Pathfinding/AStar.cs b/MLEM/Pathfinding/AStar.cs index 529a169..8ab88c4 100644 --- a/MLEM/Pathfinding/AStar.cs +++ b/MLEM/Pathfinding/AStar.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; namespace MLEM.Pathfinding { public abstract class AStar { @@ -22,6 +23,10 @@ namespace MLEM.Pathfinding { this.DefaultAllowDiagonals = defaultAllowDiagonals; } + public Task> FindPathAsync(T start, T goal, GetCost costFunction = null, float? defaultCost = null, int? maxTries = null, bool? allowDiagonals = null) { + return Task.Run(() => this.FindPath(start, goal, costFunction, defaultCost, maxTries, allowDiagonals)); + } + public Stack FindPath(T start, T goal, GetCost costFunction = null, float? defaultCost = null, int? maxTries = null, bool? allowDiagonals = null) { var startTime = DateTime.UtcNow; @@ -80,7 +85,7 @@ namespace MLEM.Pathfinding { if (count >= tries) break; } - + this.LastTriesNeeded = count; this.LastTimeNeeded = DateTime.UtcNow - startTime; return ret;