/// <summary> /// STEP 4 - connect all remaing borders internally, completing /// the border mesh /// </summary> public async Task AssembleInternalBorderMesh() { var pathTasks = new List <Task>(); foreach (var b1 in borders) { foreach (var b2 in borders.Where(b => !b.Equals(b1))) { pathTasks.Add( Task.Run(() => { // get border's central location and search from it var loc1 = b1.Center; var loc2 = b2.Center; var aStar = new AStarSearch(); // perform the search, and record the cost with the neighbors aStar.ComputePath(loc1, loc2, this, (successful, path, cost) => { if (successful) { b1.AddNeighbor(b2, cost, path); b2.AddNeighbor(b1, cost, path); } }); }) ); } } // wait for the pathfinding to complete await Task.WhenAll(pathTasks); }
public async Task FindConnectedPortals( Location location, MapTile tile, Dictionary <Portal, float> seeds ) { var portals = mesh.Values.Where(portal => portal.tile1 == tile || portal.tile2 == tile); // track path tasks for awaiting var pathTasks = new List <Task>(); // determine which portals are connected int tot = portals.Count(); foreach (var portal in portals) { // compute the path cost to each neighbor border var newTask = Task.Run(() => { var aStar = new AStarSearch(); // perform the search, and record the cost with the neighbors aStar.ComputePath(location, portal.Center, tile, (success, path, cost) => { // path finding was a success, store this portal if (success) { seeds[portal] = cost; } }); }); // track the pathfinding tasks pathTasks.Add(newTask); } await Task.WhenAll(pathTasks); }
public async void FindPath() { if (!Location.Equals(startLocation, endLocation)) { timer.Reset(); timer.Start(); //simplified pathfinding, point to point if (startTile == endTile) { await Task.Run(() => { var search = new AStarSearch(); search.ComputePath(startLocation, endLocation, startTile, storeLocationPath); }); } // complex pathfinding, navigate the tile mesh through nav system else { await NavSystem.GetPathThroughMesh(startLocation, endLocation, storePathablePath); } } }
/// <summary> /// Provided start and end locations, this will plot an A* path /// </summary> /// <param name="start"></param> /// <param name="end"></param> public async Task GetPathThroughMesh( Location start, Location end, Action <bool, List <IPathable>, float> onComplete) { var startTile = GetTileForLocation(start); var endTile = GetTileForLocation(end); // create dictionaries for task returns var startPortals = new Dictionary <Portal, float>(); var endPortals = new Dictionary <Portal, float>(); // populate the portal dictionaries var startTask = mesh.FindConnectedPortals(start, startTile, startPortals); var endTask = mesh.FindConnectedPortals(end, endTile, endPortals); // await the tasks await Task.WhenAll(startTask, endTask); // create "dummy" portals to represent start and end locations var startPortal = new Portal(start, startTile); var endPortal = new Portal(end, endTile); // add connections for IPathable foreach (var portal in startPortals) { startPortal.AddConnection(portal.Key, portal.Value); } foreach (var portal in endPortals) { portal.Key.AddConnection(endPortal, portal.Value); } // start pathfinding var aStar = new AStarSearch(); // don't await this final astar search, we don't need to hold computation aStar.ComputePath(startPortal, endPortal, onComplete); }