예제 #1
0
        public void TestAutoForward()
        {
            var stopWatch = new Stopwatch();

            var stateMachine = new StateMachine <GameMan, GameManState, GameManEvent>(
                stateAccessor: man => man.State
                , stateMutator: (man, updatedState) => man.State = updatedState);

            stateMachine.ConfigureState(GameManState.Running)
            .AddTransition(GameManEvent.SeePuddle, GameManState.Jumping)
            .AddEntryAction(man =>
            {
                Console.WriteLine($"[{stopWatch.Elapsed}] starting run...");
                Task.Delay(3000).Wait();
            })
            .AddExitAction(man =>
            {
                Console.WriteLine($"[{stopWatch.Elapsed}] run done.");
            })
            .AddReentryAction(man => Console.WriteLine($"[{stopWatch.Elapsed}] Continuing to run"));

            stateMachine.ConfigureState(GameManState.Jumping)
            .AddAutoForwardTransition(GameManEvent.SeePuddle, GameManState.Walking, _ => true)
            .AddEntryAction(man =>
            {
                Console.WriteLine($"[{stopWatch.Elapsed}] starting jump...");
                Task.Delay(1000).Wait();
            })
            .AddExitAction(man => Console.WriteLine($"[{stopWatch.Elapsed}] Exiting jump"))
            .AddReentryAction(man => Console.WriteLine($"[{stopWatch.Elapsed}] Continuing to jump"));

            stateMachine.ConfigureState(GameManState.Walking)
            .AddEntryAction(man => Console.WriteLine($"[{stopWatch.Elapsed}] Start Walking"))
            .AddExitAction(man => Console.WriteLine($"[{stopWatch.Elapsed}] Exiting walking"))
            .AddReentryAction(man => Console.WriteLine($"[{stopWatch.Elapsed}] Continuing to walk"));

            stopWatch.Start();

            var gameMan = new GameMan {
                State = GameManState.Running
            };
            var result = stateMachine.FireTrigger(gameMan, GameManEvent.Run);

            Console.WriteLine($"[{stopWatch.Elapsed}] After run: last transition = {result.LastTransitionName}");

            result = stateMachine.FireTrigger(gameMan, GameManEvent.SeePuddle);
            Console.WriteLine($"[{stopWatch.Elapsed}] After Seeing Puddle: last transition = {result.LastTransitionName}");

            Console.WriteLine($"[{stopWatch.Elapsed}] TEST COMPLETE");
        }
예제 #2
0
        public void RunWalkJumpTest()
        {
            var stopWatch = new Stopwatch();

            var stateMachine = new StateMachine <GameMan, GameManState, GameManEvent>(
                stateAccessor: man => man.State
                , stateMutator: (man, updatedState) => man.State = updatedState);

            stateMachine.ConfigureState(GameManState.Walking)
            .AddTransition(GameManEvent.Run, GameManState.Running)
            .AddAutoFallbackTransition(GameManEvent.SeePuddle, GameManState.Jumping, _ => true)
            .AddEntryAction(man => Console.WriteLine($"[{stopWatch.Elapsed}] Start Walking"))
            .AddExitAction(man => Console.WriteLine($"[{stopWatch.Elapsed}] Exiting walking"))
            .AddReentryAction(man => Console.WriteLine($"[{stopWatch.Elapsed}] Continuing to walk"));

            stateMachine.ConfigureState(GameManState.Running)
            .AddAutoFallbackTransition(GameManEvent.SeePuddle, GameManState.Jumping, _ => true, priority: 2)
            .AddEntryAction(man => Console.WriteLine($"[{stopWatch.Elapsed}] Start Running"))
            .AddExitAction(man => Console.WriteLine($"[{stopWatch.Elapsed}] Exiting running"))
            .AddReentryAction(man => Console.WriteLine($"[{stopWatch.Elapsed}] Continuing to run"));

            stateMachine.ConfigureState(GameManState.Jumping)
            .AddEntryAction(man =>
            {
                Console.WriteLine($"[{stopWatch.Elapsed}] starting jump...");
                Task.Delay(3000).Wait();
            })
            .AddExitAction(man => { Console.WriteLine($"[{stopWatch.Elapsed}] jump done."); });

            stopWatch.Start();
            var gameMan = new GameMan {
                State = GameManState.Walking
            };

            stateMachine.FireTrigger(gameMan, GameManEvent.Walk);
            stateMachine.FireTrigger(gameMan, GameManEvent.SeePuddle);
            stateMachine.FireTrigger(gameMan, GameManEvent.Run);
            stateMachine.FireTrigger(gameMan, GameManEvent.SeePuddle);
        }
예제 #3
0
        public void TestAutoFallback()
        {
            var stopWatch = new Stopwatch();

            var stateMachine = new StateMachine <GameMan, GameManState, GameManEvent>(
                stateAccessor: man => man.State
                , stateMutator: (man, updatedState) => man.State = updatedState);

            stateMachine.ConfigureState(GameManState.Walking)
            .AddAutoFallbackTransition(GameManEvent.SeePuddle, GameManState.Jumping, _ => true)
            //.AddTransition(GameManEvent.SeePuddle, GameManState.Jumping)
            .AddEntryAction(man => _testOutputHelper.WriteLine($"[{stopWatch.Elapsed}] Start Walking"))
            .AddExitAction(man => _testOutputHelper.WriteLine($"[{stopWatch.Elapsed}] Exiting walking"))
            .AddReentryAction(man => _testOutputHelper.WriteLine($"[{stopWatch.Elapsed}] Continuing to walk"));

            stateMachine.ConfigureState(GameManState.Jumping)
            .AddEntryAction(man =>
            {
                _testOutputHelper.WriteLine($"[{stopWatch.Elapsed}] starting jump...");
                Task.Delay(millisecondsDelay: 3000).Wait();
            })
            .AddExitAction(man =>
            {
                _testOutputHelper.WriteLine($"[{stopWatch.Elapsed}] jump done.");
            });

            stopWatch.Start();

            var gameMan = new GameMan();
            var result  = stateMachine.FireTrigger(gameMan, GameManEvent.Walk);

            _testOutputHelper.WriteLine($"[{stopWatch.Elapsed}] After walk: last transition = {result.LastTransitionName}");

            result = stateMachine.FireTrigger(gameMan, GameManEvent.SeePuddle);
            _testOutputHelper.WriteLine($"[{stopWatch.Elapsed}] After SeePuddle: last transition = {result.LastTransitionName}");

            _testOutputHelper.WriteLine($"[{stopWatch.Elapsed}] TEST COMPLETE");
        }