private List<Vector2> FindFinalPath(SearchNode start, SearchNode end) { closedList.Add(end); SearchNode parent = end.Parent; while (parent != start) { closedList.Add(parent); parent = parent.Parent; } List<Vector2> finalPath = new List<Vector2>(); for (int i = closedList.Count - 1; i >= 0; i--) { finalPath.Add(new Vector2(closedList[i].Position.X * 32, closedList[i].Position.Y * 32)); } return finalPath; }
public void InitializeSearchNodes(Level map) { for (int x = 0; x < levelWidth; x++) { for (int y = 0; y < levelHeight; y++) { SearchNode node = new SearchNode() { Position = new Point(x, y), Walkable = !LevelManager.IsWallTile(new Vector2(x * LevelManager.GetCurrentLevel().tileWidth, y * LevelManager.GetCurrentLevel().tileHeight)) }; if (node.Walkable == true) { node.Neighbors = new SearchNode[4]; searchNodes[x, y] = node; } } } for (int x = 0; x < levelWidth; x++) { for (int y = 0; y < levelHeight; y++) { SearchNode node = searchNodes[x, y]; if (node == null || !node.Walkable) continue; Point[] neighbors = new Point[4] { new Point(x, y - 1), new Point(x, y + 1), new Point(x - 1, y), new Point(x + 1, y) }; for (int i = 0; i < neighbors.Length; i++) { Point position = neighbors[i]; if (position.X < 0 || position.X > levelWidth - 1 || position.Y < 0 || position.Y > levelHeight - 1) { continue; } SearchNode neighbor = searchNodes[position.X, position.Y]; if (neighbor == null || !neighbor.Walkable) continue; node.Neighbors[i] = neighbor; } } } }