状态机的实现 (二)

2014-11-24 02:46:54 · 作者: · 浏览: 4
aseState *pNewState)
{
//assert(PNewState && ":trying to change to a null state");

///保留前一个状态记录
m_pPreviousState = m_pCurrentState;
///调用现有状态的退出方法
m_pCurrentState->Exit(m_pOwner);
///改变到一个新状态
m_pCurrentState= pNewState;
///调用新状态的进入方法
m_pCurrentState->Enter(m_pOwner);
}
////////////////////////////////////////////////////////////////////
///@brief 改变到上一状态
///
///@return 无返回值
////////////////////////////////////////////////////////////////////
void RevertToPreviousState()
{
ChangeState(m_pPreviousState);
}
////////////////////////////////////////////////////////////////////
///@brief 查看当前状态
///
///@return BaseState*当前状态
////////////////////////////////////////////////////////////////////
BaseState* CurrentState() const
{
return m_pCurrentState;
}

////////////////////////////////////////////////////////////////////
///@brief 查看全局状态
///
///@return BaseState* 全局状态
////////////////////////////////////////////////////////////////////
BaseState* GlobalState() const
{
return m_pGlobalState;
}
////////////////////////////////////////////////////////////////////
///@brief 查看前一状态
///
///@return BaseState*前一状态
////////////////////////////////////////////////////////////////////
BaseState* PreviousState() const
{
return m_pPreviousState;
}
//class passed as a parameter.
bool isInState(const BaseState& st)const
{
return typeid(*m_pCurrentState) == typeid(st);
}
};


#endif // !BASESTATEMACHINE_H

#ifndef BASESTATEMACHINE_H
#define BASESTATEMACHINE_H
//////////////////////////////////////////////////////////////////////////
//
/// @file 状态机基类
/// @brief 负责状态机的跳转
/// @version 2.0
//////////////////////////////////////////////////////////////////////////
#include "BaseState.h"
#include
//////////////////////////////////////////////////////////////////////////
/// @class BaseStateMachine
/// @brief 状态机基类
///
/// \n本类负责模板状态机的所有处理
template
class BaseStateMachine
{
private:
entity_type *m_pOwner; ///<指向拥有这个了实例的智能体的指针

BaseState *m_pCurrentState; ///<智能体的当前状态

BaseState *m_pPreviousState; ///<智能体的上一个状态

BaseState *m_pGlobalState; ///<每次FSM被更新时,这个状态被调用

public:
BaseStateMachine(entity_type* owner):
m_pOwner(owner),
m_pCurrentState(nullptr),
m_pPreviousState(nullptr),
m_pGlobalState(nullptr)
{

}
////////////////////////////////////////////////////////////////////
///@brief 设置当前状态
///@param [in]s 要设置的状态
///@return 无返回值
////////////////////////////////////////////////////////////////////
void SetCurrentState(BaseState *s)
{
m_pCurrentState = s;
}
////////////////////////////////////////////////////////////////////
///@brief 设置全局状态
///@param [in]s 要设置的状态
///@return 无返回值
////////////////////////////////////////////////////////////////////
void SetGlobalState(BaseState *s)
{
m_pGlobalState = s;
}
////////////////////////////////////////////////////////////////////
///@brief 设置前面的状态
///@param [in]s 要设置的状态
///@return 无返回值
//////////////////////////////////////////////////