#include "RUMetaState.h" #include "RUStateMachine.h" NS_RU_BEGIN MetaState* MetaState::create(const std::string& stateName, const std::vector& fullStatePath, kStateType stateType, const std::vector& entryActInfos, const std::vector& idleActInfos, const std::vector& exitActInfos, const std::string & stateDelegateKey, void* userObj4StateDelegate, StateMachine* stateMachine) { MetaState * ret = new (std::nothrow) MetaState(); ret->_initData(stateName, fullStatePath, stateType, entryActInfos, idleActInfos, exitActInfos, stateDelegateKey, userObj4StateDelegate); ret->_stateMachine = stateMachine; return ret; } void MetaState::entry(const std::function& entryCb) { CCASSERT(_stateMachine != nullptr, "wtf"); FSMLOG("%s进入分层状态: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str()); State::entry(entryCb); } void MetaState::idle(const std::function& idleCb) { CCASSERT(_stateMachine != nullptr, "wtf"); FSMLOG("%s执行分层状态本身: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str()); auto idleCbT = [=](void){ FSMLOG("%s执行分层状态机: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str()); _stateMachine->runMachine(idleCb); }; State::idle(idleCbT); } void MetaState::exit(const std::function& exitCb) { //先退出分层状态机再退出本身(退出顺序和进入刚好相反) CCASSERT(_stateMachine != nullptr, "wtf"); auto stopCb = [=](void){ FSMLOG("%s退出分层状态机: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str()); State::exit(exitCb); }; FSMLOG("%s退出分层状态本身: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str()); _stateMachine->stopMachine(stopCb); } std::vector MetaState::getBindingStatePath4Blocks() { std::vector bindingStatePath4Blocks = _fullStatePath; std::vector historyRunningStateNames = _stateMachine->getHistoryRunningStateNames(); bindingStatePath4Blocks.insert(bindingStatePath4Blocks.end(), historyRunningStateNames.begin(),historyRunningStateNames.end()); return bindingStatePath4Blocks; } std::vector MetaState::getInitStateNames() { CCASSERT(_stateMachine != nullptr, "wtf"); return _stateMachine->getInitStateNames(); } void MetaState::setRunningStateNames(const std::vector& stateNames) { CCASSERT(_stateMachine != nullptr, "wtf"); _stateMachine->setRunningStateNames(stateNames); } std::vector MetaState::getRunningStateNames() { CCASSERT(_stateMachine != nullptr, "wtf"); return _stateMachine->getRunningStateNames(); } std::vector MetaState::getHistoryRunningStateNames() { CCASSERT(_stateMachine != nullptr, "wtf"); return _stateMachine->getHistoryRunningStateNames(); } bool MetaState::canResponseEvent(const std::string& eventName) { CCASSERT(_stateMachine != nullptr, "wtf"); return _stateMachine->canResponseEvent(eventName); } void MetaState::handleEvent(const std::string& eventName, const std::map& eventParams, const std::function& handleEventCb) { CCASSERT(_stateMachine != nullptr, "wtf"); FSMLOG("%s%s 响应事件: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str(), eventName.c_str()); _stateMachine->handleEvent(eventName, eventParams, handleEventCb); } const std::vector& MetaState::getIdleActInfosByStateFullPath(const std::vector& stateNames) { CCASSERT(_stateMachine != nullptr, "wtf"); return _stateMachine->getIdleActInfosByStateFullPath(stateNames); } void MetaState::stopMachineImmediately() { CCASSERT(_stateMachine != nullptr, "wtf"); return _stateMachine->stopMachineImmediately(); } void MetaState::setParam(const std::string& name, const std::string& v) { _stateMachine->setParam(name, v); } void MetaState::debugStateMachineInfo() { std::string fullStatePathStr; for (int i = 0 ; i < _fullStatePath.size(); i++) { if (i == _fullStatePath.size() - 1) { fullStatePathStr = fullStatePathStr + _fullStatePath[i]; }else{ fullStatePathStr = fullStatePathStr + _fullStatePath[i] + "#"; } } CCLOG("打印分层状态信息: \"%s\"", fullStatePathStr.c_str()); _stateMachine->debugInfo(); } void MetaState::setIndent4Dbg(const std::string& indent) { _indent = indent; _stateMachine->setIndent4Dbg(" " + _indent); } void MetaState::runMachine4Dbg() { _stateMachine->runMachine4Dbg(); } void MetaState::handleEvent4Dbg(const std::string& eventName) { _stateMachine->handleEvent4Dbg(eventName); } bool MetaState::chekcStateNamesValidity(std::vector stateNames) { return _stateMachine->chekcStateNamesValidity(stateNames); } NS_RU_END