- class State
- {
- public:
- virtual void Execute(Troll* troll);
- };
- class Troll// 巨魔类
- {
- State* m_pCurrentState;
- public :
- void Update()
- {
- m_pCurrentState->Execute(this);
- }
- void ChangeState(const State* pNewState)
- {
- delete m_pCurrentState;
- m_pCurrentState = pNewState;
- }
- };
- class State_RunAway:public State//逃跑(当前状态的逻辑为逃跑)
- {
- public :
- void Execute(Troll* troll)
- {
- if(troll->isSafe())
- {
- troll->ChangeState(new State_Sleep);
- }
- else
- {
- troll->MoveAwayFromEnemy();
- }
- }
- };
- class State_Sleep: public State//睡觉(当前状态的逻辑为睡觉)
- {
- public :
- void Execute(Troll* troll)
- {
- if(troll->isThreatened())
- {
- troll->ChangeState(new State_RunAway);
- }
- else
- {
- troll->Snore();
- }
- }
- };
心得:现在感觉设计真的很重要。用好C++和系统方面,其它都是概念性的东西了。