RUState.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "RUState.h"
  2. #include "RUDelegateFactory.h"
  3. NS_RU_BEGIN
  4. State* State::create(const std::string& stateName,
  5. const std::vector<std::string>& fullStatePath,
  6. kStateType stateType,
  7. const std::vector<StateActInfo>& entryActInfos,
  8. const std::vector<StateActInfo>& idleActInfos,
  9. const std::vector<StateActInfo>& exitActInfos,
  10. const std::string & stateDelegateKey,
  11. void* userObj4StateDelegate)
  12. {
  13. State * ret = new (std::nothrow) State();
  14. ret->_initData(stateName,
  15. fullStatePath,
  16. stateType,
  17. entryActInfos,
  18. idleActInfos,
  19. exitActInfos,
  20. stateDelegateKey,
  21. userObj4StateDelegate);
  22. return ret;
  23. }
  24. void State::_initData(const std::string& stateName,
  25. const std::vector<std::string>& fullStatePath,
  26. kStateType stateType,
  27. const std::vector<StateActInfo>& entryActInfos,
  28. const std::vector<StateActInfo>& idleActInfos,
  29. const std::vector<StateActInfo>& exitActInfos,
  30. const std::string& stateDelegateKey,
  31. void* userObj4StateDelegate)
  32. {
  33. _stateName = stateName;
  34. _fullStatePath = fullStatePath;
  35. _stateType = stateType;
  36. _entryActInfos = entryActInfos;
  37. _idleActInfos = idleActInfos;
  38. _exitActInfos = exitActInfos;
  39. _stateDelegateKey = stateDelegateKey;
  40. _initStateDelegate(stateDelegateKey, userObj4StateDelegate);
  41. }
  42. void State::_initStateDelegate(const std::string& stateDelegateKey, void* userObj4StateDelegate)
  43. {
  44. _stateDelegate = DelegateFactory::getInstance()->createStateDelegate(stateDelegateKey);
  45. _stateDelegate->setUserObject(userObj4StateDelegate);
  46. }
  47. const std::string& State::getStateName()
  48. {
  49. return _stateName;
  50. }
  51. const std::vector<std::string>& State::getFullStatePath()
  52. {
  53. return _fullStatePath;
  54. }
  55. std::vector<std::string> State::getBindingStatePath4Blocks()
  56. {
  57. return _fullStatePath;
  58. }
  59. kStateType State::getStateType()
  60. {
  61. return _stateType;
  62. }
  63. void State::entry(const std::function<void(void)>& entryCb)
  64. {
  65. FSMLOG("%s进入状态: %s ", _indent.c_str(), _getFullStatePathStr4Debug().c_str());
  66. auto execIdle = [=](void){
  67. idle(entryCb);
  68. };
  69. if (_stateDelegate) {
  70. _stateDelegate->entry(this, _entryActInfos, execIdle);
  71. } else {
  72. execIdle();
  73. }
  74. }
  75. void State::idle(const std::function<void(void)>& idleCb)
  76. {
  77. FSMLOG("%s执行状态: %s", _indent.c_str(), _getFullStatePathStr4Debug().c_str());
  78. if (_stateDelegate) {
  79. _stateDelegate->idle(this, _idleActInfos, idleCb);
  80. } else if (idleCb) {
  81. idleCb();
  82. }
  83. }
  84. void State::exit(const std::function<void(void)>& exitCb)
  85. {
  86. FSMLOG("%s退出状态:%s", _indent.c_str(), _getFullStatePathStr4Debug().c_str());
  87. if (_stateDelegate) {
  88. _stateDelegate->exit(this, _exitActInfos, exitCb);
  89. } else if (exitCb) {
  90. exitCb();
  91. }
  92. }
  93. void State::beforeSwitch(std::vector<StateActInfo> actInfos, StateActInfo evt, std::function<void(void)> actsFinishCb) {
  94. if (_stateDelegate) {
  95. _stateDelegate->beforeSwitch(this, actInfos, evt, actsFinishCb);
  96. } else if (actsFinishCb) {
  97. actsFinishCb();
  98. }
  99. }
  100. void State::afterSwitch(std::vector<StateActInfo> actInfos, StateActInfo evt, std::function<void(void)> actsFinishCb) {
  101. if (_stateDelegate) {
  102. _stateDelegate->afterSwitch(this, actInfos, evt, actsFinishCb);
  103. } else if (actsFinishCb) {
  104. actsFinishCb();
  105. }
  106. }
  107. const std::vector<StateActInfo>& State::getIdleActInfos()
  108. {
  109. return _idleActInfos;
  110. }
  111. void State::setIndent4Dbg(const std::string& indent) {
  112. _indent = indent;
  113. }
  114. void State::debugInfo()
  115. {
  116. CCLOG("dump \"%s\":{",_stateName.c_str());
  117. auto getActionsInfo = [=](std::vector<StateActInfo> actinfos){
  118. std::string actions_str = "[";
  119. for(StateActInfo action : actinfos){
  120. actions_str = actions_str + "{\"type\":\""+ action.actType + "\",\"params\":{";
  121. for(auto paramIter : action.actParams){
  122. actions_str = actions_str + "\""+ paramIter.first + "\":\"" + paramIter.second + "\",";
  123. }
  124. actions_str = actions_str + "}";
  125. }
  126. actions_str = actions_str + "]";
  127. return actions_str;
  128. };
  129. std::string stateTypeStr = "普通状态";
  130. if (_stateType == kStateType::META) {
  131. stateTypeStr = "分层状态";
  132. }
  133. CCLOG(" \"stateType\":\"%s\",",stateTypeStr.c_str());
  134. CCLOG(" \"entry\":%s,",getActionsInfo(_entryActInfos).c_str());
  135. CCLOG(" \"idle\":%s,",getActionsInfo(_idleActInfos).c_str());
  136. CCLOG(" \"exit\":%s,",getActionsInfo(_exitActInfos).c_str());
  137. CCLOG(" \"stateDelegateKey\":\"%s\",",_stateDelegateKey.c_str());
  138. CCLOG(" },");
  139. }
  140. std::string State::_getFullStatePathStr4Debug()
  141. {
  142. std::string str;
  143. for (int i = 0; i < _fullStatePath.size(); i++) {
  144. if (i == _fullStatePath.size() - 1) {
  145. str = str + _fullStatePath[i];
  146. }else{
  147. str = str + _fullStatePath[i] + "#";
  148. }
  149. }
  150. return str;
  151. }
  152. NS_RU_END