123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- #include "RUState.h"
- #include "RUDelegateFactory.h"
- NS_RU_BEGIN
- State* State::create(const std::string& stateName,
- const std::vector<std::string>& fullStatePath,
- kStateType stateType,
- const std::vector<StateActInfo>& entryActInfos,
- const std::vector<StateActInfo>& idleActInfos,
- const std::vector<StateActInfo>& exitActInfos,
- const std::string & stateDelegateKey,
- void* userObj4StateDelegate)
- {
- State * ret = new (std::nothrow) State();
- ret->_initData(stateName,
- fullStatePath,
- stateType,
- entryActInfos,
- idleActInfos,
- exitActInfos,
- stateDelegateKey,
- userObj4StateDelegate);
- return ret;
- }
- void State::_initData(const std::string& stateName,
- const std::vector<std::string>& fullStatePath,
- kStateType stateType,
- const std::vector<StateActInfo>& entryActInfos,
- const std::vector<StateActInfo>& idleActInfos,
- const std::vector<StateActInfo>& exitActInfos,
- const std::string& stateDelegateKey,
- void* userObj4StateDelegate)
- {
- _stateName = stateName;
- _fullStatePath = fullStatePath;
- _stateType = stateType;
- _entryActInfos = entryActInfos;
- _idleActInfos = idleActInfos;
- _exitActInfos = exitActInfos;
- _stateDelegateKey = stateDelegateKey;
- _initStateDelegate(stateDelegateKey, userObj4StateDelegate);
- }
- void State::_initStateDelegate(const std::string& stateDelegateKey, void* userObj4StateDelegate)
- {
- _stateDelegate = DelegateFactory::getInstance()->createStateDelegate(stateDelegateKey);
- _stateDelegate->setUserObject(userObj4StateDelegate);
- }
- const std::string& State::getStateName()
- {
- return _stateName;
- }
- const std::vector<std::string>& State::getFullStatePath()
- {
- return _fullStatePath;
- }
- std::vector<std::string> State::getBindingStatePath4Blocks()
- {
- return _fullStatePath;
- }
- kStateType State::getStateType()
- {
- return _stateType;
- }
- void State::entry(const std::function<void(void)>& entryCb)
- {
- FSMLOG("%s进入状态: %s ", _indent.c_str(), _getFullStatePathStr4Debug().c_str());
- auto execIdle = [=](void){
- idle(entryCb);
- };
- if (_stateDelegate) {
- _stateDelegate->entry(this, _entryActInfos, execIdle);
- } else {
- execIdle();
- }
- }
- void State::idle(const std::function<void(void)>& idleCb)
- {
- FSMLOG("%s执行状态: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str());
- if (_stateDelegate) {
- _stateDelegate->idle(this, _idleActInfos, idleCb);
- } else if (idleCb) {
- idleCb();
- }
- }
- void State::exit(const std::function<void(void)>& exitCb)
- {
- FSMLOG("%s退出状态:%s", _indent.c_str(), _getFullStatePathStr4Debug().c_str());
- if (_stateDelegate) {
- _stateDelegate->exit(this, _exitActInfos, exitCb);
- } else if (exitCb) {
- exitCb();
- }
- }
- void State::beforeSwitch(std::vector<StateActInfo> actInfos, StateActInfo evt, std::function<void(void)> actsFinishCb) {
- if (_stateDelegate) {
- _stateDelegate->beforeSwitch(this, actInfos, evt, actsFinishCb);
- } else if (actsFinishCb) {
- actsFinishCb();
- }
- }
- void State::afterSwitch(std::vector<StateActInfo> actInfos, StateActInfo evt, std::function<void(void)> actsFinishCb) {
- if (_stateDelegate) {
- _stateDelegate->afterSwitch(this, actInfos, evt, actsFinishCb);
- } else if (actsFinishCb) {
- actsFinishCb();
- }
- }
- const std::vector<StateActInfo>& State::getIdleActInfos()
- {
- return _idleActInfos;
- }
- void State::setIndent4Dbg(const std::string& indent) {
- _indent = indent;
- }
- void State::debugInfo()
- {
- CCLOG("dump \"%s\":{",_stateName.c_str());
- auto getActionsInfo = [=](std::vector<StateActInfo> actinfos){
- std::string actions_str = "[";
- for(StateActInfo action : actinfos){
- actions_str = actions_str + "{\"type\":\""+ action.actType + "\",\"params\":{";
- for(auto paramIter : action.actParams){
- actions_str = actions_str + "\""+ paramIter.first + "\":\"" + paramIter.second + "\",";
- }
- actions_str = actions_str + "}";
- }
- actions_str = actions_str + "]";
- return actions_str;
- };
- std::string stateTypeStr = "普通状态";
- if (_stateType == kStateType::META) {
- stateTypeStr = "分层状态";
- }
- CCLOG(" \"stateType\":\"%s\",",stateTypeStr.c_str());
- CCLOG(" \"entry\":%s,",getActionsInfo(_entryActInfos).c_str());
- CCLOG(" \"idle\":%s,",getActionsInfo(_idleActInfos).c_str());
- CCLOG(" \"exit\":%s,",getActionsInfo(_exitActInfos).c_str());
- CCLOG(" \"stateDelegateKey\":\"%s\",",_stateDelegateKey.c_str());
- CCLOG(" },");
- }
- std::string State::_getFullStatePathStr4Debug()
- {
- std::string str;
- for (int i = 0; i < _fullStatePath.size(); i++) {
- if (i == _fullStatePath.size() - 1) {
- str = str + _fullStatePath[i];
- }else{
- str = str + _fullStatePath[i] + "#";
- }
- }
- return str;
- }
- NS_RU_END
|