private void AddAdjacentCellToOpenList(Node parentNode, int columnOffset, int rowOffset, int gCost) { var adjacentCellIndex = _map.GetAdjacentCellIndex(parentNode.CellIndex, columnOffset, rowOffset); // ignore unwalkable nodes (or nodes outside the grid) if (adjacentCellIndex == -1) return; // ignore nodes on the closed list if (_closedList.Any(n => n.CellIndex == adjacentCellIndex)) return; var adjacentNode = _openList.SingleOrDefault(n => n.CellIndex == adjacentCellIndex); if (adjacentNode != null) { if(parentNode.G + gCost < adjacentNode.G) { adjacentNode.Parent = parentNode; adjacentNode.G = parentNode.G + gCost; adjacentNode.F = adjacentNode.G + adjacentNode.H; } return; } var node = new Node(adjacentCellIndex, parentNode) { G = gCost, H = _map.GetDistance(adjacentCellIndex, _map.TargetCell) }; node.F = node.G + node.H; _openList.Add(node); }
public void Reinizialize() { pathfound = false; if (pathfinder != null) { pathfinder.PathFound -= pathfinder_PathFound; pathfinder.Abort(); pathfinder.Stop(); } Random rand = new Random((int)DateTime.Now.Ticks); mapnodes = new Node[mapsize, mapsize]; for (int y = 0; y < mapsize; y++) { for (int x = 0; x < mapsize; x++) { mapnodes[y, x] = new Node(x, y, size, rand.Next(0, 100) < percentage ? Walkable.Blocked : Walkable.Walkable); } } mapnodes[yaim, xaim] = new Node(xaim, yaim, size, Walkable.Walkable); mapnodes[5, 2] = new Node(2, 5,size, Walkable.Walkable); AStarMap.InitializeMap(mapnodes); pathfinder = new AStar(); pathfinder.PathFound += new PathFoundEventHandler(pathfinder_PathFound); pathfinder.FindWay(mapnodes[5, 2], mapnodes[yaim, xaim]); }
public static void InitializeMapFromLevel(this AStarMap map, Tile[,] maptiles) { int yLength = maptiles.GetLength(0); int xLength = maptiles.GetLength(1); Node[,] aStarNodes; aStarNodes = new Node[yLength, xLength]; for (int y = 0; y < yLength; y++) { for (int x = 0; x < xLength; x++) { Walkable walkable = Walkable.Walkable; switch (maptiles[y, x].TileType) { case TileType.Wall: case TileType.Spike: walkable = Walkable.Blocked; break; case TileType.Platform: walkable = Walkable.Platform; break; } if (walkable == Walkable.Walkable) { if (CheckForUnreachable(maptiles, y, x)) walkable = Walkable.NotReachable; } aStarNodes[y, x] = new Node(x, y, Tile.TILE_SIZE, walkable); } } AStarMap.InitializeMap(aStarNodes); }
public void CalculatePath() { var firstNode = new Node(_map.StartCell, null); _openList.Add(firstNode); _currentNode = firstNode; while(true) { if (_openList.Count == 0) { break; } _currentNode = FindSmallestF(); if (_currentNode.CellIndex == _map.TargetCell) { break; } _openList.Remove(_currentNode); _closedList.Add(_currentNode); AddAdjacentCellToOpenList(_currentNode, -1, -1, 14); AddAdjacentCellToOpenList(_currentNode, 0, -1, 10); AddAdjacentCellToOpenList(_currentNode, 1, -1, 14); AddAdjacentCellToOpenList(_currentNode, -1, 0, 10); AddAdjacentCellToOpenList(_currentNode, 1, 0, 10); AddAdjacentCellToOpenList(_currentNode, -1, 1, 14); AddAdjacentCellToOpenList(_currentNode, 0, 1, 10); AddAdjacentCellToOpenList(_currentNode, 1, 1, 14); } _map.HighlightPath(_currentNode); }
public AStarMap() { if (mapnodes == null) { mapnodes = new Node[1, 1]; mapnodes[0, 0] = new Node(0,0,Walkable.Walkable); } }
public void FindWay(Node start, Node aim) { //Only run a new PathSearch if we don't running currently one. if (myThread.ThreadState != ThreadState.Running) { this.start = start; this.aim = aim; aimglobal = aim.ToPathNode(); //openList.Clear(); openLinkedList.Clear(); //If thread is never Started, start it if (myThread.ThreadState == ThreadState.Unstarted) myThread.Start(); //If thread is Suspended from an older search, resume it //with the new parameters specified above. if (myThread.ThreadState == ThreadState.Suspended) myThread.Resume(); abort = false; } //FindWay(); }
private void StartNewPathSearch(Vector2 playerPos) { int startX, starty, aimX, aimY; startX = (int)Math.Round((pos.X) / (float)Tile.TILE_SIZE); starty = (int)Math.Round(pos.Y / (float)Tile.TILE_SIZE); aimX = (int)playerPos.X / Tile.TILE_SIZE; aimY = (int)playerPos.Y / Tile.TILE_SIZE; start = new Node(startX, starty, Tile.TILE_SIZE, Walkable.Walkable); end = new Node(aimX, aimY, Tile.TILE_SIZE, Walkable.Walkable); astar.Abort(); astar.FindWay(startX, starty, aimX, aimY); pathSearching = true; }
public void HighlightPath(Node node) { var currentNode = node; while(currentNode != null) { _path.Add(currentNode.CellIndex); currentNode = currentNode.Parent; } }
public static void InitializeMap(Node[,] nodes) { if (nodes != null) mapnodes = nodes; }