LoopWaiter.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // LoopWaiter.cpp
  3. // redream_runtime_mac
  4. //
  5. // Created by zhu on 2023/5/23.
  6. //
  7. #include "LoopWaiter.hpp"
  8. #include "ReboltRedManager.h"
  9. namespace redream {
  10. LoopWaiter::LoopWaiter(ReboltRedManager* reboltManager, red::RedBehaviacTree* tree, std::map<std::string, bool> &boolMap, std::map<std::string, std::string> &stringMap, std::string treeName)
  11. : _boolMap(boolMap)
  12. , _stringMap(stringMap)
  13. , _treeName(treeName)
  14. , _isLoopEnd(false)
  15. , _loopTimes(0)
  16. {
  17. _reboltManager = reboltManager;
  18. _fatherTree = tree;
  19. }
  20. LoopWaiter::~LoopWaiter(){
  21. }
  22. LoopWaiter* LoopWaiter::create(ReboltRedManager* reboltManager, red::RedBehaviacTree* tree, std::map<std::string, bool> &boolMap, std::map<std::string, std::string> &stringMap, std::string treeName){
  23. LoopWaiter * ret = new (std::nothrow) LoopWaiter(reboltManager, tree, boolMap, stringMap, treeName);
  24. if (ret && ret->init())
  25. {
  26. ret->autorelease();
  27. }
  28. else
  29. {
  30. CC_SAFE_DELETE(ret);
  31. }
  32. return ret;
  33. }
  34. bool LoopWaiter::init(){
  35. return true;
  36. }
  37. void LoopWaiter::run(){
  38. _runningState.btState = RUNNING;
  39. createTreeAndRun();
  40. }
  41. void LoopWaiter::createTreeAndRun(){
  42. _loopTimes++;
  43. auto redBehaviacTree = _reboltManager->createBehaviacTree(_treeName, _boolMap, _stringMap);
  44. if(redBehaviacTree){
  45. _tree = redBehaviacTree;
  46. _tree->addRedBehaviacTreeDelegate(this);
  47. _tree->setReboltLoopManager(this);
  48. redBehaviacTree->runBehaviacTree();
  49. } else {
  50. _runningState.btState = FAILURE;
  51. }
  52. }
  53. int LoopWaiter::getLoopTimes(){
  54. return _loopTimes;
  55. }
  56. void LoopWaiter::onLoopEnd(){
  57. // 结束后立即调用父树的刷新,父树的对象直接调用
  58. _isLoopEnd = true;
  59. _tree->removeRedBehaviacTreeDelegate(this);
  60. onSuccessEnd();
  61. }
  62. void LoopWaiter::onTreeRunningEnd(red::RedBehaviacTree* tree, int endType, std::string treeName){
  63. // 数播放完毕之后,就需要播放一个新的树
  64. if(!_isLoopEnd){
  65. createTreeAndRun();
  66. }
  67. }
  68. };