1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-31 20:33:38 +02:00

added FindPathAsync

This commit is contained in:
Ellpeck 2020-02-06 17:43:34 +01:00
parent d823064979
commit ed5114d089
2 changed files with 10 additions and 3 deletions

View file

@ -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

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace MLEM.Pathfinding {
public abstract class AStar<T> {
@ -22,6 +23,10 @@ namespace MLEM.Pathfinding {
this.DefaultAllowDiagonals = defaultAllowDiagonals;
}
public Task<Stack<T>> 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<T> 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;