分享:大家有没有状态机的简单+实用的封装? 

2007-03-07 00:54 发布

引擎学习交流 /[各编程语言]
2819 0 0
(转帖)最近写的游戏老是有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;
}

B Color Smilies

你可能喜欢

分享:大家有没有状态机的简单+实用的封装? 
联系
我们
快速回复 返回顶部 返回列表