示例#1
0
        public Stack <Vector2> GetShortestPath(Vector2 start, Vector2 finish)
        {
            if (NodeMap == null)
            {
                NodeMap = new Pathfinding.NodeMap(1, 1, 1); //TODO: remove these two lines. Need them right now to stop a null ref exception since NodeMap is null
            }
            // Translate start and finish to non-blocked nodes from node map
            Node closestStartNode  = NodeMap.TranslateToNode(start, false);
            Node closestFinishNode = NodeMap.TranslateToNode(finish, false);

            if (closestStartNode == null || closestFinishNode == null) // return null if there is no path
            {
                return(null);
            }

            Stack <Node> shortestNodePath = Astar(closestStartNode, closestFinishNode);


            if (shortestNodePath == null) // there is no path, its blocked completely
            {
                return(null);
            }

            List <Node>     shortestNodePathArray = shortestNodePath.ToList <Node>();
            Stack <Vector2> path = new Stack <Vector2>();

            for (int i = shortestNodePathArray.Count - 1; i >= 0; i--)
            {
                path.Push(shortestNodePathArray[i].ToVector2());
            }

            //DrawDebug.DrawPoints(path.ToList()); // Draw shortest path for debug

            return(path);
        }
示例#2
0
 public AstarPathfinder(NodeMap nodeMap)
 {
     NodeMap = nodeMap;
 }