示例#1
0
        private static void PollingExample()
        {
            Console.WriteLine(nameof(PollingExample));

            //Setup the nodegrid and pathfinder.
            var pathfindaxManager = new PathfindaxManager();
            var factory           = new DefinitionNodeGridFactory();
            var nodeGrid          = factory.GeneratePreFilledArray(GenerateNodeGridConnections.All, 3, 3);
            var nodeNetwork       = new DefinitionNodeGrid(nodeGrid, new Vector2(1, 1));
            var pathfinder        = pathfindaxManager.CreateAstarPathfinder(nodeNetwork, new ManhattanDistance());

            //Request a path.
            var pathRequest = pathfinder.RequestPath(nodeNetwork.NodeGrid.ToIndex(0, 0), nodeNetwork.NodeGrid.ToIndex(2, 0));

            Console.WriteLine($"Solving path from {pathRequest.PathStart} to {pathRequest.PathEnd}...");

            //Poll the status to check when the pathfinder is finished.
            while (pathRequest.Status == PathRequestStatus.Solving)
            {
            }
            switch (pathRequest.Status)
            {
            case PathRequestStatus.Solved:
                Console.WriteLine($"Solved path! {pathRequest.CompletedPath}");
                break;

            case PathRequestStatus.NoPathFound:
                Console.WriteLine("Could not find a path");
                break;
            }
            Console.WriteLine($"{nameof(PollingExample)} completed. Press any key to continue with the next example.");
            Console.ReadKey();
        }
示例#2
0
        private static void CallbackExample()
        {
            Console.WriteLine(nameof(CallbackExample));

            //Setup the nodegrid and pathfinder.
            var pathfindaxManager = new PathfindaxManager();
            var factory           = new DefinitionNodeGridFactory();
            var nodeGrid          = factory.GeneratePreFilledArray(GenerateNodeGridConnections.All, 3, 3);
            var nodeNetwork       = new DefinitionNodeGrid(nodeGrid, new Vector2(1, 1));
            var pathfinder        = pathfindaxManager.CreateAstarPathfinder(nodeNetwork, new ManhattanDistance());

            //Request a path.
            var pathRequest = pathfinder.RequestPath(nodeNetwork.NodeGrid.ToIndex(0, 0), nodeNetwork.NodeGrid.ToIndex(2, 0));

            Console.WriteLine($"Solving path from {pathRequest.PathStart} to {pathRequest.PathEnd}...");

            //Add a callback to the request that will be called then the pathfinder is done.
            var callbackCalled = false;

            pathRequest.AddCallback(request =>
            {
                Console.WriteLine($"Solved path! {request.CompletedPath}");
                callbackCalled = true;
            });

            //Wait till callback is called.
            while (!callbackCalled)
            {
            }
            Console.WriteLine($"{nameof(CallbackExample)} completed. Press any key to continue with the next example.");
            Console.ReadKey();
        }
示例#3
0
        private static void CallbackExample()
        {
            Console.WriteLine(nameof(CallbackExample));

            //Setup the nodegrid and pathfinder.
            var pathfindaxManager = new PathfindaxManager();
            var nodeNetwork       = new DefinitionNodeGrid(GenerateNodeGridConnections.All, 3, 3, new Vector2(1, 1));
            var pathfinder        = pathfindaxManager.CreateAstarPathfinder(nodeNetwork);

            //Request a path.
            var pathRequest = pathfinder.RequestPath(nodeNetwork.NodeGrid[0, 0], nodeNetwork.NodeGrid[2, 0]);

            Console.WriteLine($"Solving path from {pathRequest.PathStart.Position} to {pathRequest.PathEnd.Position}...");

            //Add a callback to the request that will be called then the pathfinder is done.
            var callbackCalled = false;

            pathRequest.AddCallback(request =>
            {
                Console.WriteLine($"Solved path! {request.CompletedPath}");
                callbackCalled = true;
            });

            //Start calling update on the manager. Update will call the callbacks of any completed paths. Note that the callback is called from the thread that calls update.
            //Normally when using pathfindax in a game engine you would wire this up in your game loop.
            while (!callbackCalled)
            {
                pathfindaxManager.Update(1f);
            }
            Console.WriteLine($"{nameof(CallbackExample)} completed. Press any key to continue with the next example.");
            Console.ReadKey();
        }
示例#4
0
        public void RequestPath_SingleThread_NoExceptions()
        {
            var manager = new PathfindaxManager(Substitute.For <IUpdatableSynchronizationContext>());
            var multithreadedPathfinder = PathfinderSetup.Create(manager, 1);

            multithreadedPathfinder.Start();
            var pathRequest = PathRequest.Create(multithreadedPathfinder, -1, -1);

            pathRequest.WaitHandle.WaitOne(1000);

            Assert.AreEqual(PathRequestStatus.Solved, pathRequest.Status);
        }
示例#5
0
        public void Integration_StatusFlowToNoPathFound()
        {
            var manager    = new PathfindaxManager(Substitute.For <IUpdatableSynchronizationContext>());
            var pathfinder = PathfinderSetup.Create(manager, 1, false);
            var request    = PathRequest.Create <IPath>(-1, -1);

            Assert.AreEqual(PathRequestStatus.Created, request.Status);
            pathfinder.RequestPath(request);
            Assert.AreEqual(PathRequestStatus.Solving, request.Status);
            pathfinder.Start();
            request.WaitHandle.WaitOne(1000);
            Assert.AreEqual(PathRequestStatus.NoPathFound, request.Status);
        }
示例#6
0
        public static Pathfinder <IDefinitionNodeNetwork, IPathfindNodeNetwork, IPath> Create(int threads, bool succesTrue = true)
        {
            var manager = new PathfindaxManager();

            return(manager.CreatePathfinder(Substitute.For <IDefinitionNodeNetwork>(), Substitute.For <IPathFindAlgorithm <IPathfindNodeNetwork, IPath> >(), (definitionNodeNetwork, algorithm) =>
            {
                var nodeGrid = Substitute.For <IPathfindNodeNetwork>();
                algorithm.FindPath(null, null, out var _).ReturnsForAnyArgs(x =>
                {
                    x[2] = succesTrue;
                    return new NodePath(new[] { new DefinitionNode(0, Vector2.Zero) }, new Transformer(new Vector2(1, 1)));
                });
                return PathfinderFactory.CreateRequestProcesser(nodeGrid, algorithm);
            }, threads));
        }
示例#7
0
        public void Integration_AddCallbackAfterPathIsSolved_CallbackIsCalled()
        {
            var manager    = new PathfindaxManager(Substitute.For <IUpdatableSynchronizationContext>());
            var pathfinder = PathfinderSetup.Create(manager, 1);

            pathfinder.Start();
            var request = PathRequest.Create(pathfinder, -1, -1);

            request.WaitHandle.WaitOne(1000);

            var done = false;

            request.AddCallback(x => done = true);

            Assert.AreEqual(true, done);
        }
示例#8
0
        public void Integration_AddCallbackBeforePathIsSolved_CallbackIsCalled()
        {
            var manager    = new PathfindaxManager(new UpdatableSynchronizationContext());
            var pathfinder = PathfinderSetup.Create(manager, 1);

            pathfinder.Start();
            var request = PathRequest.Create <IPath>(-1, -1);

            var done = false;

            request.AddCallback(x => done = true);

            pathfinder.RequestPath(request);
            request.WaitHandle.WaitOne(1000);
            manager.Update(1f);             //This should call the callback if the path is finished.
            Assert.AreEqual(true, done);
        }
示例#9
0
        private static void AsyncExample()
        {
            Console.WriteLine(nameof(AsyncExample));

            //Setup the nodegrid and pathfinder.
            var pathfindaxManager  = new PathfindaxManager();
            var factory            = new DefinitionNodeGridFactory();
            var nodeGrid           = factory.GeneratePreFilledArray(GenerateNodeGridConnections.All, 3, 3);
            var definitionNodeGrid = new DefinitionNodeGrid(nodeGrid, new Vector2(1, 1));
            var pathfinder         = pathfindaxManager.CreateAstarPathfinder(definitionNodeGrid, new ManhattanDistance());

            var exampleGameObject = new ExampleAsyncGameObject(pathfinder);

            //Wait till callback is called.
            while (!exampleGameObject.CallBackCalled)
            {
                exampleGameObject.Update();
            }
            Console.ReadKey();
        }
示例#10
0
        public void RequestPath_MultipleThreads_NoExceptions()
        {
            var manager = new PathfindaxManager(Substitute.For <IUpdatableSynchronizationContext>());
            var multithreadedPathfinder = PathfinderSetup.Create(manager, 4);

            multithreadedPathfinder.Start();

            var pathRequests = new PathRequest <IPath> [64];

            for (var i = 0; i < pathRequests.Length; i++)
            {
                pathRequests[i] = PathRequest.Create(multithreadedPathfinder, -1, -1);;
            }

            WaitHandle.WaitAll(pathRequests.Select(x => x.WaitHandle).ToArray(), 2000);

            foreach (var pathRequest in pathRequests)
            {
                Assert.AreEqual(PathRequestStatus.Solved, pathRequest.Status);
            }
        }
示例#11
0
        public static Pathfinder <IDefinitionNodeNetwork, IPathfindNodeNetwork, IPath> Create(PathfindaxManager manager, int threads, bool succesTrue = true)
        {
            return(manager.CreatePathfinder(Substitute.For <IDefinitionNodeNetwork>(), Substitute.For <IPathFindAlgorithm <IPathfindNodeNetwork, IPath> >(), (definitionNodeNetwork, algorithm, cache) =>
            {
                var nodeGrid = Substitute.For <IPathfindNodeNetwork>();
                var nodeArray = new[] { new DefinitionNode(), };
                algorithm.FindPath(null, null).ReturnsForAnyArgs(x =>
                {
                    if (succesTrue)
                    {
                        return new WaypointPath(nodeArray, new[] { 0 }, new Transformer(new Vector2(1, 1)));
                    }
                    else
                    {
                        return null;
                    }
                });

                algorithm.ValidatePath(null, null, null).ReturnsForAnyArgs(x =>
                {
                    return true;
                });
                return PathfinderFactory.CreateRequestProcesser(nodeGrid, algorithm, cache);
            }, Substitute.For <ICache <IPathRequest, IPath> >(), threads));
        }