示例#1
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();
        }
示例#2
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);
        }
 protected override void OnBeforeUpdate()
 {
     base.OnBeforeUpdate();
     _pathfindaxManager.Update(Time.LastDelta);
 }