Exemplo n.º 1
0
        public void Transition_NodeToSubFSM_WhenPredicateIsTrue()
        {
            var n       = 0;
            var fsm     = new FSM();
            var subFSMA = new SubFSM();
            var subFSMB = new SubFSM();
            var stateA  = new StateA();
            var stateB  = new StateB();
            var stateC  = new StateC();
            var stateD  = new StateD();

            fsm.AddTransition(stateA, subFSMA, () => n == 1);
            subFSMA.AddTransition(stateB, subFSMB, () => n == 2);
            subFSMB.AddTransition(stateC, stateD, () => n == 3);

            fsm.Start();
            n = 1;
            fsm.Update();
            n = 2;
            fsm.Update();
            n = 3;
            fsm.Update();

            Assert.IsTrue(fsm.CurrentNode == subFSMA && subFSMA.CurrentNode == subFSMB && subFSMB.CurrentNode == stateD);
        }
Exemplo n.º 2
0
        public void AddNode_NotAllowDuplicateNode()
        {
            var fsm    = new FSM();
            var stateA = new StateA();

            fsm.AddNode(stateA);
            fsm.AddNode(stateA);

            Assert.IsTrue(fsm.NodeCount == 1);
        }
Exemplo n.º 3
0
        public void AddNode_NotAllowDuplicateNode()
        {
            var subFSM = new FSM();
            var stateA = new StateA();

            subFSM.AddNode(stateA);
            subFSM.AddNode(stateA);

            Assert.IsTrue(subFSM.NodeCount == 1);
        }
Exemplo n.º 4
0
        public void AddNode_NotAllowIntersectSubFSM()
        {
            var subFSMA = new SubFSM();
            var subFSMB = new SubFSM();
            var stateA  = new StateA();

            subFSMA.AddNode(stateA);
            subFSMB.AddNode(stateA);
            subFSMB.AddNode(subFSMA);

            Assert.IsTrue(!subFSMB.Contains(subFSMA));
        }
Exemplo n.º 5
0
        public void AddNode_NotAllowIntersectSubFSM()
        {
            var fsm    = new FSM();
            var subFSM = new SubFSM();
            var stateA = new StateA();

            subFSM.AddNode(stateA);

            fsm.AddNode(stateA);
            fsm.AddNode(subFSM);

            Assert.IsTrue(!fsm.Contains(subFSM));
        }
Exemplo n.º 6
0
        public void AddNode_NotAllowOwnerNode()
        {
            var fsm    = new FSM();
            var subFSM = new SubFSM();
            var stateA = new StateA();

            fsm.AddNode(stateA);
            fsm.AddNode(subFSM);

            subFSM.AddNode(stateA);

            Assert.IsTrue(!subFSM.Contains(stateA));
        }
Exemplo n.º 7
0
        public void RemoveNode_FromFSM()
        {
            var fsm    = new FSM();
            var stateA = new StateA();
            var stateB = new StateB();
            var stateC = new StateC();

            fsm.AddTransition(stateA, stateB, () => true);
            fsm.AddTransition(stateB, stateC, () => true);

            fsm.RemoveNode(stateB);

            Assert.IsTrue(!fsm.Contains(stateB));
        }
Exemplo n.º 8
0
        public void Transition_NodeToNode_WhenPredicateIsTrue()
        {
            var n      = 0;
            var fsm    = new FSM();
            var stateA = new StateA();
            var stateB = new StateB();

            fsm.AddTransition(stateA, stateB, () => n == 1);

            fsm.Start();
            n = 1;
            fsm.Update();

            Assert.IsTrue(fsm.CurrentNode == stateB);
        }
Exemplo n.º 9
0
        public void RemoveNode_FromSubFSM()
        {
            var fsm    = new FSM();
            var subFSM = new SubFSM();
            var stateA = new StateA();
            var stateB = new StateB();
            var stateD = new StateD();

            subFSM.AddNode(stateD);
            fsm.AddTransition(stateA, stateB, () => true);
            fsm.AddTransition(stateB, subFSM, () => true);
            fsm.RemoveNode(stateD);

            Assert.IsTrue(!fsm.Contains(stateD));
        }
Exemplo n.º 10
0
        public void ThrowsExceptionOnInvalidToTransition()
        {
            Game    game    = new Game();
            StateA  stateA  = new StateA();
            StateA1 stateA1 = new StateA1();
            StateB  stateB  = new StateB();

            game.LoadSubState(stateA);
            game.LoadSubState(stateB);
            stateA.LoadSubState(stateA1);

            int TRIGGER = 1;

            Assert.Throws <InvalidTransitionException>(() => {
                stateA.AddTransition(stateA1, stateB, TRIGGER);
            });
        }
Exemplo n.º 11
0
        public void ThrowsExceptionOnDuplicateTransitionAdded()
        {
            Game    game    = new Game();
            StateA  stateA  = new StateA();
            StateA1 stateA1 = new StateA1();
            StateB  stateB  = new StateB();

            game.LoadSubState(stateA);
            game.LoadSubState(stateB);
            game.LoadSubState(stateA1);

            int TRANSITION = 1;

            game.AddTransition(stateA, stateB, TRANSITION);

            Assert.Throws <DuplicateTransitionException>(() => {
                game.AddTransition(stateA, stateA1, TRANSITION);
            });
        }
Exemplo n.º 12
0
        public void Transition_NodeToSubFSM_WhenPredicateIsTrue()
        {
            var n      = 0;
            var fsm    = new FSM();
            var subFSM = new SubFSM();
            var stateA = new StateA();
            var stateB = new StateB();
            var stateC = new StateC();

            subFSM.AddTransition(stateB, stateC, () => n == 2);
            fsm.AddTransition(stateA, subFSM, () => n == 1);

            fsm.Start();
            n = 1;
            fsm.Update();
            n = 2;
            fsm.Update();

            Assert.IsTrue(fsm.CurrentNode == subFSM && subFSM.CurrentNode == stateC);
        }
Exemplo n.º 13
0
        public void EnterSM()
        {
            Game    game    = new Game();
            StateA  stateA  = new StateA();
            StateA1 stateA1 = new StateA1();
            StateA2 stateA2 = new StateA2();
            StateA3 stateA3 = new StateA3();

            game.LoadSubState(stateA);
            stateA.LoadSubState(stateA1);
            stateA.LoadSubState(stateA2);
            stateA.LoadSubState(stateA3);

            game.EnterStateMachine();

            Assert.AreEqual(3, callChain.Count);
            Assert.AreEqual("game_enter", callChain[0]);
            Assert.AreEqual("stateA_enter", callChain[1]);
            Assert.AreEqual("stateA1_enter", callChain[2]);
        }
Exemplo n.º 14
0
        public void ThrowsExceptionOnDuplicateStateAdded()
        {
            Game    game             = new Game();
            StateA  stateA           = new StateA();
            StateA1 stateA1          = new StateA1();
            StateA2 stateA2          = new StateA2();
            StateB  stateB           = new StateB();
            StateB1 stateB1          = new StateB1();
            StateB2 stateB2          = new StateB2();
            StateB2 stateB2Duplicate = new StateB2();

            Assert.Throws <DuplicateSubStateException>(() => {
                game.LoadSubState(stateA);
                game.LoadSubState(stateB);
                stateA.LoadSubState(stateA1);
                stateB.LoadSubState(stateB1);
                stateA1.LoadSubState(stateA2);
                stateB1.LoadSubState(stateB2);
                stateB1.LoadSubState(stateB2Duplicate);
            });
        }
Exemplo n.º 15
0
        public void TransitionSimple()
        {
            Game   game   = new Game();
            StateA stateA = new StateA();
            StateB stateB = new StateB();

            int AtoB = 1;

            game.LoadSubState(stateA);
            game.LoadSubState(stateB);
            game.AddTransition(stateA, stateB, AtoB);

            game.EnterStateMachine();
            game.SendTrigger(AtoB);

            Assert.AreEqual(4, callChain.Count);
            Assert.AreEqual("game_enter", callChain[0]);
            Assert.AreEqual("stateA_enter", callChain[1]);
            Assert.AreEqual("stateA_exit", callChain[2]);
            Assert.AreEqual("stateB_enter", callChain[3]);
        }
Exemplo n.º 16
0
        public void Transition_ToPreviousNode_WhenPredicateIsTrue()
        {
            var n      = 0;
            var fsm    = new FSM();
            var stateA = new StateA();
            var stateB = new StateB();
            var stateC = new StateC();

            fsm.AddTransition(stateA, stateB, () => n == 1);
            fsm.AddTransition(stateB, stateC, () => n == 2);
            fsm.AddTransitionToPreviousNode(stateC, () => n == 3);

            fsm.Start();
            n = 1;
            fsm.Update();
            n = 2;
            fsm.Update();
            n = 3;
            fsm.Update();

            Assert.IsTrue(fsm.CurrentNode == stateB);
        }
Exemplo n.º 17
0
        public void ThrowsExceptionOnNeglectedTrigger()
        {
            Game    game    = new Game();
            StateA  stateA  = new StateA();
            StateA1 stateA1 = new StateA1();
            StateB  stateB  = new StateB();

            game.LoadSubState(stateA);
            game.LoadSubState(stateB);
            game.LoadSubState(stateA1);

            int TRIGGER_1             = 1;
            int TRIGGER_2_NEVER_ADDED = 2;

            game.AddTransition(stateA, stateB, TRIGGER_1);

            game.EnterStateMachine();

            Assert.Throws <NeglectedTriggerException>(() => {
                game.SendTrigger(TRIGGER_2_NEVER_ADDED);
            });
        }
Exemplo n.º 18
0
        public void TransitionTriggerFromSubState()
        {
            Game    game    = new Game();
            StateA  stateA  = new StateA();
            StateA1 stateA1 = new StateA1();
            StateA2 stateA2 = new StateA2();
            StateB  stateB  = new StateB();
            StateB1 stateB1 = new StateB1();
            StateB2 stateB2 = new StateB2();

            int AtoB = 1;

            game.LoadSubState(stateA);
            game.LoadSubState(stateB);
            stateA.LoadSubState(stateA1);
            stateB.LoadSubState(stateB1);
            stateA1.LoadSubState(stateA2);
            stateB1.LoadSubState(stateB2);

            game.AddTransition(stateA, stateB, AtoB);

            game.EnterStateMachine();
            stateA2.SendTrigger(AtoB);

            Assert.AreEqual(10, callChain.Count);
            Assert.AreEqual("game_enter", callChain[0]);
            Assert.AreEqual("stateA_enter", callChain[1]);
            Assert.AreEqual("stateA1_enter", callChain[2]);
            Assert.AreEqual("stateA2_enter", callChain[3]);
            Assert.AreEqual("stateA2_exit", callChain[4]);
            Assert.AreEqual("stateA1_exit", callChain[5]);
            Assert.AreEqual("stateA_exit", callChain[6]);
            Assert.AreEqual("stateB_enter", callChain[7]);
            Assert.AreEqual("stateB1_enter", callChain[8]);
            Assert.AreEqual("stateB2_enter", callChain[9]);
        }