Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public List<Node> GetPossibleDestinations(Graph graph, Node start, int distanceLimit)
- {
- var frontier = new Queue<Node>();
- frontier.Enqueue(start);
- var visited = new List<Node>();
- visited.Add(start);
- while (frontier.Count > 0)
- {
- var currentNode = frontier.Dequeue();
- if (currentNode.distance >= distanceLimit)
- {
- continue;
- }
- var neighbors = graph.GetNeighbours(currentNode);
- foreach (var neighbor in neighbors)
- {
- if (neighbor.nodeType == NodeType.Open && !visited.Contains(neighbor))
- {
- frontier.Enqueue(neighbor);
- visited.Add(neighbor);
- neighbor.distance = 1 + currentNode.distance;
- }
- }
- }
- return visited;
- }
- public List<Node> GetNeighbours(Node node)
- {
- List<Node> neighbours = new List<Node>();
- for (int x = -1; x <= 1; x++)
- {
- for (int y = -1; y <= 1; y++)
- {
- if (x == 0 && y == 0)
- continue;
- int checkX = node.gridX + x;
- int checkY = node.gridY + y;
- if (checkX >= 0 && checkX < graphSizeX && checkY >= 0 && checkY < graphSizeY)
- {
- neighbours.Add(nodes[checkX, checkY]);
- }
- }
- }
- return neighbours;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement