AI_StateMachine.h
Go to the documentation of this file.00001 #ifndef AI_STATE_MACHINE_H 00002 #define AI_STATE_MACHINE_H 00003 00004 #include "AI_Global.h" 00005 #include "utils/AI_Array.h" 00006 00007 class AI_State; 00008 00025 class AI_StateMachine 00026 { 00027 //*** Embedded 00028 public: 00034 class Session 00035 { 00036 protected: 00038 ai_index m_i_root_state; 00040 ai_index m_i_actual_state; 00041 00042 public: 00043 Session(); 00044 void setRootState( ai_index i_index ); 00045 void setActualState( ai_index i_index ); 00046 ai_index getRootState( void ) const; 00047 ai_index getActualState( void ) const; 00048 void reset(void); 00049 }; 00050 00051 //*** Attributes 00052 private: 00054 AI_Array<AI_State*> m_arr_states; 00055 00056 //*** Methods 00057 public: 00058 //*** Constructor & destructor 00059 AI_StateMachine(void); 00060 ~AI_StateMachine(void); 00061 00062 //*** Building 00063 void beginStates( int i_number ); 00064 void setState( int i_index, AI_State *ptr_state ); 00065 void endStates( void ); 00066 void clear( void ); 00067 00068 //*** Running 00069 void work( Session &session ); 00070 const bool isClear(void) const; 00071 00072 //*** Gets 00073 AI_State *getState( int i_index ); 00074 }; 00075 00076 00079 inline 00080 AI_StateMachine::Session::Session() : 00081 m_i_root_state(0), 00082 m_i_actual_state(0) 00083 { 00084 } 00085 00086 00089 inline 00090 void AI_StateMachine::Session::setActualState( ai_index i_index ) 00091 { 00092 m_i_actual_state = i_index; 00093 } 00094 00095 00098 inline 00099 ai_index AI_StateMachine::Session::getActualState( void ) const 00100 { 00101 return m_i_actual_state; 00102 } 00103 00104 00107 inline 00108 void AI_StateMachine::Session::setRootState( ai_index i_index ) 00109 { 00110 m_i_root_state = i_index; 00111 } 00112 00113 00116 inline 00117 ai_index AI_StateMachine::Session::getRootState( void ) const 00118 { 00119 return m_i_root_state; 00120 } 00121 00122 00125 inline 00126 void AI_StateMachine::Session::reset(void) 00127 { 00128 m_i_actual_state = m_i_root_state; 00129 } 00130 00131 00134 inline 00135 AI_StateMachine::AI_StateMachine(void) 00136 { 00137 } 00138 00139 00142 inline 00143 AI_StateMachine::~AI_StateMachine(void) 00144 { 00145 clear(); 00146 } 00147 00148 00151 inline 00152 void AI_StateMachine::beginStates( int i_number ) 00153 { 00154 m_arr_states.SetFixedSize( i_number ); 00155 m_arr_states.Fill(0, i_number, 0); 00156 } 00157 00158 00161 inline 00162 void AI_StateMachine::setState( int i_index, AI_State *ptr_state ) 00163 { 00164 if (i_index >= m_arr_states.Size()) 00165 ai_log(AI_STR_TOO_HIGH_STATE_INDEX); 00166 00167 if (m_arr_states[i_index]) 00168 ai_log(AI_STR_FILLED_INDEX); 00169 00170 m_arr_states[i_index] = ptr_state; 00171 } 00172 00173 00176 inline 00177 void AI_StateMachine::endStates( void ) 00178 { 00179 } 00180 00181 00184 inline 00185 AI_State *AI_StateMachine::getState( int i_index ) 00186 { 00187 return m_arr_states[i_index]; 00188 } 00189 00190 00193 inline 00194 const bool AI_StateMachine::isClear(void) const 00195 { 00196 return (m_arr_states.Size() == 0); 00197 } 00198 00199 #endif