private void DrawRoute(Point end)
        {
            //DRAW MAP
            //IXamlDirect xd = XamlDirect.GetDefault();
            //var rec = xd.CreateInstance(XamlTypeIndex.Rectangle);
            //var col = xd.GetXamlDirectObject(cvLayout);


            for (int y = 0; y < Map.Instance.MAP_HEIGHT; y++)
            {
                for (int x = 0; x < Map.Instance.MAP_WIDTH; x++)
                {
                    var wh = 40d;
                    var pt = new Point(x * wh, y * wh);

                    VisualNode vn = new VisualNode();
                    vn.Name = $"vnx{x}y{y}";
                    vn.SetValue(Canvas.LeftProperty, pt.X);
                    //xd.SetObjectProperty(rec, XamlPropertyIndex.Canvas_Left, pt.X);
                    vn.SetValue(Canvas.TopProperty, pt.Y);
                    //xd.SetObjectProperty(rec, XamlPropertyIndex.Canvas_Top, pt.Y);
                    vn.Width = wh;
                    //xd.SetObjectProperty(rec, XamlPropertyIndex.FrameworkElement_Width, wh);
                    vn.Height = wh;
                    //xd.SetObjectProperty(rec, XamlPropertyIndex.FrameworkElement_Height, wh);
                    vn.DrawPosition(x, y);
                    vn.SetDot(false);
                    vn.SetWall(Map.Instance.GetMap(x, y));
                    vn.SetPosition(x, y);
                    //vn.NodeClicked += Vn_NodeClicked;
                    cvLayout.Children.Add(vn);

                    //xd.AddToCollection(col, rec);
                }
            }



            //START STAR SEARCH THROUGH MAP
            AStarExample.Start(cvLayout, end);
        }
Exemple #2
0
        static bool Pathfind(NodePosition startPos, NodePosition goalPos, AStarPathfinder pathfinder)
        {
            // Reset the allocated MapSearchNode pointer
            pathfinder.InitiatePathfind();

            // Create a start state
            MapSearchNode nodeStart = pathfinder.AllocateMapSearchNode(startPos);

            // Define the goal state
            MapSearchNode nodeEnd = pathfinder.AllocateMapSearchNode(goalPos);

            // Set Start and goal states
            pathfinder.SetStartAndGoalStates(nodeStart, nodeEnd);

            // Set state to Searching and perform the search
            AStarPathfinder.SearchState searchState = AStarPathfinder.SearchState.Searching;
            uint searchSteps = 0;

            do
            {
                searchState = pathfinder.SearchStep();
                searchSteps++;
            }while (searchState == AStarPathfinder.SearchState.Searching);

            // Search complete
            bool pathfindSucceeded = (searchState == AStarPathfinder.SearchState.Succeeded);

            if (pathfindSucceeded)
            {
                // Success
                Path newPath          = new Path();
                int  numSolutionNodes = 0; // Don't count the starting cell in the path length

                // Get the start node
                MapSearchNode node = pathfinder.GetSolutionStart();
                newPath.Add(node.position);
                ++numSolutionNodes;

                // Get all remaining solution nodes
                for (; ;)
                {
                    node = pathfinder.GetSolutionNext();

                    if (node == null)
                    {
                        break;
                    }

                    ++numSolutionNodes;
                    newPath.Add(node.position);

                    // DRAW PATH TO GOAL
                    var        vnName          = $"vnx{node.position.x}y{node.position.y}";
                    VisualNode foundVisualNode = _visualGrid.FindName(vnName) as VisualNode;
                    foundVisualNode?.SetDot(true);
                }
                ;

                // Once you're done with the solution we can free the nodes up
                pathfinder.FreeSolutionNodes();


                Debug.WriteLine("Solution path length: " + numSolutionNodes);
                Debug.WriteLine("Solution: " + newPath.ToString());
            }
            else if (searchState == AStarPathfinder.SearchState.Failed)
            {
                // FAILED, no path to goal
                Debug.WriteLine("Pathfind FAILED!");
            }

            return(pathfindSucceeded);
        }