Example #1
0
        public TennisRoundRules()
        {
            _fsm = new FiniteStateMachine();
            _hitLimit = 1;
            _bounceLimit = 1;
            _hitCount = 0;
            _bounceCount = 0;

            State to1 = new State("To1");
            State to2 = new State("To2");
            State win1 = new State("Win1");
            State win2 = new State("Win2");
            ConditionalState bounce1 = new ConditionalState("Bounce1", IsBounceLegal);
            ConditionalState hit1 = new ConditionalState("Hit1", IsHitLegal);
            ConditionalState bounce2 = new ConditionalState("Bounce2", IsBounceLegal);
            ConditionalState hit2 = new ConditionalState("Hit2", IsHitLegal);
            ConditionalState outside1 = new ConditionalState("Outside1", HasBounced);
            ConditionalState outside2 = new ConditionalState("Outside2", HasBounced);

            win1.OnEnter += Winner;
            win2.OnEnter += Winner;

            to1.AddEdge("Bounce2", win1);
            to1.AddEdge("Outside", outside1);
            to1.AddEdge("Hit2", hit2);
            to1.AddEdge("Bounce1", bounce1);
            to1.AddEdge("Hit1", to2);
            bounce1.AddEdge("true", to1);
            bounce1.AddEdge("false", win2);
            hit1.AddEdge("true", to2);
            hit1.AddEdge("false", win2);
            outside1.AddEdge("true", win2);
            outside1.AddEdge("false", win1);

            to2.AddEdge("Bounce1", win2);
            to2.AddEdge("Outside", outside2);
            to2.AddEdge("Hit1", hit1);
            to2.AddEdge("Bounce2", bounce2);
            to2.AddEdge("Hit2", to1);
            bounce2.AddEdge("true", to2);
            bounce2.AddEdge("false", win1);
            hit2.AddEdge("true", to1);
            hit2.AddEdge("false", win1);
            outside2.AddEdge("true", win1);
            outside2.AddEdge("false", win2);

            _fsm.Add(to1);
            _fsm.Add(to2);
            _fsm.Add(win1);
            _fsm.Add(win2);
            _fsm.Add(bounce1);
            _fsm.Add(hit1);
            _fsm.Add(bounce2);
            _fsm.Add(hit2);
            _fsm.Add(outside1);
            _fsm.Add(outside2);
        }
Example #2
0
        public bool TryPerformTransition(string edge)
        {
            if (Current == null)
                return false;

            State state = Current.Step(edge);
            if (state == null)
                return false;

            Current = state;
               // Debug.Log("Enter(" + edge + "):  " + Current.Id);
            Current.NotifyOnEnter();
            return true;
        }
Example #3
0
        public bool TrySetStateById(string id)
        {
            foreach(State s in _states)
            {
                if (s.Id == id)
                {
                    Current = s;
                    //Debug.Log("Enter:  " + Current.Id);
                    Current.NotifyOnEnter();
                    return true;
                }
            }

            return false;
        }
Example #4
0
 private bool IsHitLegal(State state)
 {
     return _hitCount <= _hitLimit;
 }
Example #5
0
 private bool HasBounced(State state)
 {
     return _bounceCount != 0;
 }
Example #6
0
 private bool IsBounceLegal(State state)
 {
     return _bounceCount <= _bounceLimit;
 }
Example #7
0
        void Winner(State state)
        {
            PlayerId winner = PlayerId.Neutral;

            if (state.Id == "Win1")
                winner = PlayerId.Player1;
            else if (state.Id == "Win2")
                winner = PlayerId.Player2;

            OnWinner(winner);
        }
Example #8
0
        public static void Test1()
        {
            int value = 5;
            State s1 = new State("s1");
            State s2 = new State("s2");
            State s3 = new State("s3");
            State s4 = new State("s4");
            State s5 = new ConditionalState("s5", (state) => value < 10);

            s1.AddEdge("e1", s2);
            s1.AddEdge("e2", s3);
            s1.AddEdge("e3", s5);

            s2.AddEdge("e5", s4);

            s3.AddEdge("e3", s2);
            s3.AddEdge("e4", s4);

            s4.AddEdge("e1", s1);

            s5.AddEdge("true", s2);
            s5.AddEdge("false", s4);

            verbose = true;
            result = true;
            if (verbose)
                Debug.Log("Start test 1");
            State cur = s1;
            cur = cur.Step("e2");
            print("s1 =e2=> s3", cur == s3);

            cur = cur.Step("e3");
            print("s3 =e3=> s2", cur == s2);

            cur = cur.Step("e5");
            print("s2 =e5=> s4", cur == s4);

            cur = cur.Step("e1");
            print("s4 =e1=> s1", cur == s1);

            cur = cur.Step("e2");
            print("s1 =e2=> s3", cur == s3);

            cur = cur.Step("e4");
            print("s3 =e4=> s4", cur == s4);

            Debug.Log("Result test 1 : " + result);

            result = true;
            if (verbose)
                Debug.Log("Start test 2");

            value = 5;
            cur = s1;
            cur = cur.Step("e3");
            print("s1 =e3/if(5<10)=> s2 ", cur == s2);

            value = 15;
            cur = s1;
            cur = cur.Step("e3");
            print("s1 =e3/if(15<10)=> s4", cur == s4);

            Debug.Log("Result test 2 : " + result);
        }
Example #9
0
 public void Add(State state)
 {
     _states.Add(state);
 }
Example #10
0
 public FiniteStateMachine()
 {
     _states = new List<State>();
     _current = null;
 }
Example #11
0
 public void AddEdge(string edge, State state)
 {
     Edges.Add(edge, state);
 }