@@ -13,13 +13,13 @@ public partial class StateMachine
1313
1414 private Dictionary < string , Data > _data = new Dictionary < string , Data > ( ) ;
1515
16- public State CurrentState { get ; internal set ; }
16+ public State CurrentState { get ; private set ; }
1717
18- public Transition CurrentTransition { get ; internal set ; }
18+ public State PreviousState { get ; private set ; }
1919
20- public State StartState { get ; protected set ; }
20+ public Transition CurrentTransition { get ; private set ; }
2121
22- private Action < State , State > _onChangeState ;
22+ public State StartState { get ; private set ; }
2323
2424 public StateMachine OnChangeState ( Action < State , State > actionOnChangeState )
2525 {
@@ -43,41 +43,22 @@ public StateMachine(string xDocumentPath)
4343 FromXDocument ( this , xDocumentPath ) ;
4444 }
4545
46-
4746 private Transition _nextTransition { get ; set ; }
4847
4948 private Dictionary < string , object > _currentParameters { get ; set ; }
5049
5150 private Dictionary < string , object > _nextParameters { get ; set ; }
5251
53- private State _beforeState { get ; set ; }
54-
55-
56- private void CheckStartState ( )
57- {
58- if ( StartState != null )
59- {
60- throw new ArgumentException ( String . Format ( "Start state already set. It's {0} " , StartState . Name ) ) ;
61- }
62- }
63-
64- private void CheckCurrentTransition ( )
65- {
66- if ( CurrentTransition == null )
67- {
68- throw new ArgumentException ( String . Format ( "State with name \" {0}\" doesn't invoke transition" , CurrentState . Name ) ) ;
69- }
70- }
52+ private Action < State , State > _onChangeState ;
7153
7254 public State SetStartState ( State state )
7355 {
74- CheckStartState ( ) ;
75- return StartState = state ;
56+ StartState = state ;
57+ return state ;
7658 }
7759
7860 public State SetStartState ( string stateName )
7961 {
80- CheckStartState ( ) ;
8162 StartState = State ( stateName ) ;
8263 return StartState ;
8364 }
@@ -94,31 +75,49 @@ public void InvokeTransitionWithParameters(string nameTransition, Dictionary<str
9475 _nextParameters = parameters ;
9576 }
9677
97- public void Start ( Dictionary < string , object > startParameters = null )
78+ private StateMachine InvokeTransition ( )
9879 {
80+ //Mark nextParameters as current
81+ _currentParameters = _nextParameters ;
82+ _nextParameters = null ;
83+
84+ //Mark nextTransition as current
85+ CurrentTransition = _nextTransition ;
86+ _nextTransition = null ;
87+
88+ //Mark currentState as previous
89+ PreviousState = CurrentState ;
90+ CurrentState = null ;
91+
92+
93+ CurrentTransition . Invoke ( _currentParameters ) ;
94+ CurrentState = CurrentTransition . StateTo ;
95+ CurrentTransition = null ;
96+
97+ return this ;
98+ }
99+
100+ private StateMachine ChangeState ( )
101+ {
102+ CurrentState . Entry ( _currentParameters ) ;
103+ _onChangeState ? . Invoke ( PreviousState , CurrentState ) ;
104+ CurrentState . Exit ( _currentParameters ) ;
105+
106+ return this ;
107+ }
108+
109+ public void Start ( Dictionary < string , object > startParameters = null )
110+ {
99111 CurrentState = StartState ;
100- CurrentState . Entry ( startParameters ) ;
101- _onChangeState ? . Invoke ( null , CurrentState ) ;
102- CurrentState . Exit ( startParameters ) ;
112+ _currentParameters = startParameters ;
103113
114+ ChangeState ( ) ;
104115
105116 while ( _nextTransition != null )
106117 {
107- _currentParameters = _nextParameters ;
108- _nextParameters = null ;
109-
110- CurrentTransition = _nextTransition ;
111- _nextTransition = null ;
112-
113- _beforeState = CurrentState ;
114- CurrentState = null ;
115- CurrentTransition . Invoke ( _currentParameters ) ;
116- CurrentState = CurrentTransition . StateTo ;
117- CurrentTransition = null ;
118+ InvokeTransition ( ) ;
118119
119- CurrentState . Entry ( _currentParameters ) ;
120- _onChangeState ? . Invoke ( _beforeState , CurrentState ) ;
121- CurrentState . Exit ( _currentParameters ) ;
120+ ChangeState ( ) ;
122121 }
123122
124123 }
0 commit comments