|
|
最近写的游戏老是有switch + call function.
于是使用表格把它们包起来,但是写得多了,还是做了个简单的封装:
class CTestState
{
public:
friend class CStateMachine<CTestState>;
enum MYSTATE
{
EM_S1 = 0,
EM_S2 = 1
};
CTestState () { m_state.SetCaller(this); }//: m_state(this) {}
CStateMachine<CTestState> m_state; //状态机对象
void Run();
void OnEvent();
private:
void MainState ( const EM_MACHINE_STATE &state ) {}
void MainState2( const EM_MACHINE_STATE &state );
};
CStateMachine<CTestState>::MYDATA CStateMachine<CTestState>::s_info[] = {
{CTestState::EM_S1, &CTestState::MainState, false},
{CTestState::EM_S2, &CTestState::MainState2,true}
};
void CTestState::MainState2 (const EM_MACHINE_STATE &state )
{
switch (state)
{
case EM_MS_INIT:
cout << "MainState Init!" << endl;
break;
case EM_MS_RUN:
cout << "MainState Running!" << endl;
break;
case EM_MS_DESTORY:
cout << "MainState Destory!" << endl;
break;
}
}
int main(int argc, char* argv[])
{
CTestState test;
test.m_state.SafeSwitch( CTestState::EM_S1 );
test.m_state.SafeSwitch( CTestState::EM_S2 );
test.m_state.SafeSwitch( CTestState::EM_S1 );
test.m_state.Call();
return 0;
}
|
|