ReboltRedManager.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. //
  2. // ReboltRedManager.cpp
  3. // empty2dx-desktop
  4. //
  5. // Created by zhu on 2022/8/3.
  6. //
  7. #include "ReboltRedManager.h"
  8. #include "extensions/GUI/CCControlExtension/CCControl.h"
  9. #include "AdUtils.h"
  10. #include "RedreamAnim.h"
  11. #include "BTManger.hpp"
  12. #include "RedWise.hpp"
  13. using namespace cocos2d::extension;
  14. namespace redream {
  15. #define TreeType_btn "btn"
  16. #define TreeType_msg "msg"
  17. #define TreeType_func "func"
  18. #define ValueMapMessageKey "messageKey"
  19. #define ValueMapMessageValue "messageValue"
  20. #define UPDATE_KEY "__rsm_reboltSceneManger__sys"
  21. #define EVENT_KEY "__rsm_reboltRedManager__event__sys"
  22. ReboltRedManager::ReboltRedManager()
  23. : _reboltId("")
  24. , _redAnimationManager(nullptr)
  25. , _rootNode(nullptr)
  26. , _treeIndex(0)
  27. , _animIndex(0)
  28. , _notifyDevelopmentDelegate(nullptr)
  29. , _reboltRunDelegate(nullptr)
  30. {
  31. _nodeMap.clear();
  32. _customVarStringMap.clear();
  33. _coderVarStringMap.clear();
  34. _buttonNameMap.clear();
  35. _behaviacTreeMap.clear();
  36. _treeTypeNameMap.clear();
  37. _waitDelTreeNameVec.clear();
  38. _pathRedMangerMap.clear();
  39. _rootRedMangers.clear();
  40. _animFileStatusMap.clear();
  41. _waitAddTreeMap.clear();
  42. }
  43. ReboltRedManager::~ReboltRedManager(){
  44. CC_SAFE_RELEASE(_redAnimationManager);
  45. for(auto it = _buttonNameMap.begin(); it != _buttonNameMap.end(); it++){
  46. auto button = (cocos2d::extension::Control*)it->first;
  47. button->removeTargetWithActionForControlEvents(this, cccontrol_selector(ReboltRedManager::onButtonClick), cocos2d::extension::Control::EventType::ALL);
  48. }
  49. for(auto it = _behaviacTreeMap.begin(); it != _behaviacTreeMap.end(); it++){
  50. auto tree = it->second;
  51. behaviac::Agent::Destroy(tree);
  52. }
  53. for(auto it : _waitAddTreeMap){
  54. auto tree = it.second;
  55. behaviac::Agent::Destroy(tree);
  56. }
  57. _waitAddTreeMap.clear();
  58. cocos2d::Director::getInstance()->getScheduler()->unschedule(UPDATE_KEY, _rootNode);
  59. _nodeMap.clear();
  60. _customVarStringMap.clear();
  61. _coderVarStringMap.clear();
  62. _buttonNameMap.clear();
  63. _behaviacTreeMap.clear();
  64. _treeTypeNameMap.clear();
  65. _waitDelTreeNameVec.clear();
  66. _pathRedMangerMap.clear();
  67. _rootRedMangers.clear();
  68. _animFileStatusMap.clear();
  69. Director::getInstance()->getEventDispatcher()->removeEventListener(_eventListener);
  70. }
  71. void ReboltRedManager::parseTreeMapAndStartUpdate(){
  72. for(auto it = _pathRedMangerMap.begin(); it != _pathRedMangerMap.end(); it++){
  73. string path = it->first;
  74. auto subReboltRedManager = it->second;
  75. _nodeMap[path] = subReboltRedManager->getRootNode();
  76. }
  77. cocos2d::Director::getInstance()->getScheduler()->schedule([=](float dt){
  78. this->update(dt);
  79. }, _rootNode, 0.016, !_rootNode->isRunning(), UPDATE_KEY);
  80. _eventListener = EventListenerCustom::create(EVENT_KEY, [=](EventCustom* event){
  81. ValueMap message = *(ValueMap*)event->getUserData();
  82. runBehaviacWhitMsg(message);
  83. });
  84. Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(_eventListener, _rootNode);
  85. }
  86. red::RedBehaviacTree* ReboltRedManager::createBehaviacTree(std::string treeRandomId, const std::map<std::string, bool> &boolMap, const std::map<std::string, std::string> &stringMap){
  87. std::string treeName = treeRandomId + "::" + to_string(getTreeIndex());
  88. behaviac::Workspace::GetInstance()->SetFilePath("");
  89. behaviac::Workspace::GetInstance()->SetFileFormat(behaviac::Workspace::EFF_xml);
  90. auto redBehaviacTree = behaviac::Agent::Create<red::RedBehaviacTree>();
  91. redBehaviacTree->setReboltRedManager(this);
  92. redBehaviacTree->setTreeName(treeName);
  93. redBehaviacTree->setReboltRunDelegate(_reboltRunDelegate);
  94. bool bRet = false;
  95. string filePath = "Rebolt_BT/" + treeRandomId;
  96. bRet = redBehaviacTree->btload(treeRandomId.c_str());
  97. if(!bRet){
  98. CCLOG("创建树失败 %s, %s", _reboltId.c_str(), treeRandomId.c_str());
  99. CCASSERT(false, "创建树失败");
  100. return nullptr;
  101. }
  102. {
  103. //行为树设置必须的属性
  104. redBehaviacTree->btsetcurrent(treeRandomId.c_str());
  105. redBehaviacTree->setArgumentsBoolMap(boolMap);
  106. redBehaviacTree->setArgumentsStringMap(stringMap);
  107. }
  108. addBehaviacTree(redBehaviacTree, treeName);
  109. return redBehaviacTree;
  110. }
  111. std::string ReboltRedManager::runBehaviacTree(const std::string& treeRandomId, const std::map<std::string, bool>& boolMap, const std::map<std::string, std::string>& stringMap){
  112. auto redBehaviacTree = createBehaviacTree(treeRandomId, boolMap, stringMap);
  113. if(!redBehaviacTree){
  114. return "";
  115. }
  116. auto treeName = redBehaviacTree->getTreeName();
  117. redBehaviacTree->runBehaviacTree();
  118. return treeName;
  119. }
  120. void ReboltRedManager::update(float dt){
  121. std::map<std::string, red::RedBehaviacTree*>::iterator iter1;
  122. for (iter1 = _behaviacTreeMap.begin();iter1 != _behaviacTreeMap.end();iter1++)
  123. {
  124. iter1->second->update();
  125. }
  126. for(auto it : _waitAddTreeMap){
  127. _behaviacTreeMap[it.first] = it.second;
  128. }
  129. _waitAddTreeMap.clear();
  130. for(int i = 0; i < _waitDelTreeNameVec.size(); i++){
  131. std::string delTreeName = _waitDelTreeNameVec[i];
  132. removeBehaviacTree(delTreeName);
  133. }
  134. _waitDelTreeNameVec.clear();
  135. }
  136. void ReboltRedManager::runBehaviacWhitButtonClick(std::string btnName){
  137. auto btnInMainNameIdMapIt = _treeTypeNameMap.find(TreeType_btn);
  138. map<std::string, std::string> buttonNameIdMap;
  139. if(btnInMainNameIdMapIt != _treeTypeNameMap.end()){
  140. buttonNameIdMap = btnInMainNameIdMapIt->second;
  141. auto nameIdIt = buttonNameIdMap.find(btnName);
  142. if(nameIdIt != buttonNameIdMap.end()){
  143. runBehaviacTree(nameIdIt->second, {}, {});
  144. }
  145. }
  146. }
  147. void ReboltRedManager::runBehaviacWhitMsg(ValueMap& msg){
  148. auto msgNameIdMapIt = _treeTypeNameMap.find(TreeType_msg);
  149. if(msgNameIdMapIt != _treeTypeNameMap.end()){
  150. map<std::string, std::string> msgNameIdMap = msgNameIdMapIt->second;
  151. string messageKey = msg.at(ValueMapMessageKey).asString();
  152. auto nameIdIt = msgNameIdMap.find(messageKey);
  153. if(nameIdIt != msgNameIdMap.end()){
  154. string messageValue = msg.at(ValueMapMessageValue).asString();
  155. // CCLOG("message [%s]-----[%s]", messageKey.c_str(), messageValue.c_str());
  156. std::map<std::string, bool> boolMap = {};
  157. std::map<std::string, std::string> stringMap;
  158. stringMap["消息参数"] = messageValue;
  159. runBehaviacTree(nameIdIt->second, boolMap, stringMap);
  160. } else {
  161. CCLOG("消息类型存在但是消息名称不存在");
  162. }
  163. } else {
  164. CCLOG("消息类型不存在");
  165. }
  166. }
  167. std::string ReboltRedManager::runBehaviacWhitFunName(std::string name, std::map<std::string, bool> boolMap, std::map<std::string, std::string> stringMap){
  168. auto funNameIdMapIt = _treeTypeNameMap.find(TreeType_func);
  169. if(funNameIdMapIt != _treeTypeNameMap.end()){
  170. map<std::string, std::string> funNameIdMap = funNameIdMapIt->second;
  171. auto nameIdIt = funNameIdMap.find(name);
  172. if(nameIdIt != funNameIdMap.end()){
  173. return runBehaviacTree(nameIdIt->second, boolMap, stringMap);
  174. } else {
  175. CCASSERT(false, "函数类型存在但是函数名称不存在");
  176. }
  177. } else {
  178. CCASSERT(false, "函数类型不存在");
  179. }
  180. return "";
  181. }
  182. void ReboltRedManager::onTreeRunEnd(int endType, std::string treeName){
  183. _waitDelTreeNameVec.push_back(treeName);
  184. }
  185. void ReboltRedManager::onSubTreeRunEnd(int endType, std::string redPath, std::string treeName){
  186. // 没实现子树调用,需要的话需要在子red的onTreeRunEnd调用这个方法
  187. }
  188. void ReboltRedManager::updateTreeWithName(std::string redPath, std::string treeName, std::string subTreeName){
  189. auto it = _behaviacTreeMap.find(treeName);
  190. if(it!= _behaviacTreeMap.end()){
  191. auto tree = it->second;
  192. tree->updateBySubTree();
  193. }
  194. }
  195. bool ReboltRedManager::onAssignREDReboltRedManagerReboltName(cocos2d::Ref* target, const char* reboltName, const char* reboltId, cocos2d::Node* pNode){
  196. onResolveREDControlRebolt(target, reboltName, reboltId, pNode);
  197. _nodeMap[reboltId] = pNode;
  198. return false;
  199. }
  200. void ReboltRedManager::onResolveREDControlRebolt(cocos2d::Ref* target, const char* reboltName, const char* reboltId, cocos2d::Node* pNode){
  201. auto controlNode = dynamic_cast<Control*>(pNode);
  202. if(controlNode){
  203. _buttonNameMap[controlNode] = reboltId;
  204. controlNode->addTargetWithActionForControlEvents(target, cccontrol_selector(ReboltRedManager::onButtonClick), Control::EventType::TOUCH_UP_INSIDE);
  205. }
  206. }
  207. int ReboltRedManager::getTreeIndex(){
  208. _treeIndex++;
  209. return _treeIndex;
  210. }
  211. int ReboltRedManager::getAnimIndex(){
  212. _animIndex++;
  213. return _animIndex;
  214. }
  215. cocos2d::Node* ReboltRedManager::getNodeByKey(std::string nodeName, ReboltErrorInfo& errorInfo){
  216. auto it = _nodeMap.find(nodeName);
  217. if(it != _nodeMap.end()){
  218. return it->second;
  219. } else {
  220. errorInfo.errorType = ReboltErrorTypeError;
  221. errorInfo.errorDes = StringUtils::format("没找到【%s】中的【%s】节点", _reboltId.c_str(), nodeName.c_str());
  222. return nullptr;
  223. }
  224. }
  225. cocos2d::Node* ReboltRedManager::getSubredNodeByKey(std::string redPath, std::string key, ReboltErrorInfo& errorInfo){
  226. auto redManger = getReboltRedManagerByPath(redPath, errorInfo);
  227. if(redManger){
  228. return redManger->getNodeByKey(key, errorInfo);
  229. } else {
  230. return nullptr;
  231. }
  232. }
  233. cocos2d::extension::Control* ReboltRedManager::getControlByKey(std::string key, ReboltErrorInfo& errorInfo){
  234. cocos2d::Node* node = getNodeByKey(key, errorInfo);
  235. if(node != nullptr){
  236. auto control = static_cast<cocos2d::extension::Control*>(node);
  237. if(control != nullptr){
  238. return control;
  239. } else{
  240. errorInfo.errorType = ReboltErrorTypeError;
  241. errorInfo.errorDes = StringUtils::format("[%s] 名字对应的节点不是按钮", key.c_str());
  242. return nullptr;
  243. }
  244. } else {
  245. return nullptr;
  246. }
  247. }
  248. cocos2d::extension::Control* ReboltRedManager::getSubredControlByKey(std::string redPath, std::string key, ReboltErrorInfo& errorInfo){
  249. auto redManger = getReboltRedManagerByPath(redPath, errorInfo);
  250. if(redManger){
  251. return redManger->getControlByKey(key, errorInfo);
  252. } else {
  253. errorInfo.errorType = ReboltErrorTypeError;
  254. errorInfo.errorDes = StringUtils::format("[%s] 子red不存在", redPath.c_str());
  255. return nullptr;
  256. }
  257. }
  258. //MARK: - 解析过程中的接口
  259. void ReboltRedManager::setREDAnimationManager(REDAnimationManager* pREDAnimationManager){
  260. CC_SAFE_RELEASE(_redAnimationManager);
  261. _redAnimationManager = pREDAnimationManager;
  262. CC_SAFE_RETAIN(_redAnimationManager);
  263. }
  264. bool ReboltRedManager::onAssignREDReboltRedManagerReboltPath(cocos2d::Ref* target, const char* reboltPath, ReboltRedManager* pReboltRedManager){
  265. _pathRedMangerMap.insert(reboltPath, pReboltRedManager);
  266. return true;
  267. }
  268. REDAnimationManager* ReboltRedManager::getREDAnimationManager(){
  269. return _redAnimationManager;
  270. }
  271. REDAnimationManager* ReboltRedManager::getSubREDAnimationManager(std::string redPath){
  272. ReboltErrorInfo errInfo;
  273. auto redManger = getReboltRedManagerByPath(redPath, errInfo);
  274. if(errInfo.errorType == ReboltErrorTypeError){
  275. CCASSERT(false, errInfo.errorDes);
  276. }
  277. if(redManger){
  278. return redManger->getREDAnimationManager();
  279. } else {
  280. return nullptr;
  281. }
  282. }
  283. void ReboltRedManager::setReboltId(const std::string rId){
  284. _reboltId = rId;
  285. }
  286. std::string ReboltRedManager::getReboltId(){
  287. return _reboltId;
  288. }
  289. cocos2d::Node* ReboltRedManager::getRootNode()
  290. {
  291. return _rootNode;
  292. }
  293. void ReboltRedManager::setRootNode(cocos2d::Node *pRootNode)
  294. {
  295. _rootNode = pRootNode;
  296. }
  297. void ReboltRedManager::addBehaviacTree(red::RedBehaviacTree* tree, std::string treeName){
  298. // _behaviacTreeMap[treeName] = tree;
  299. _waitAddTreeMap[treeName] = tree;
  300. }
  301. void ReboltRedManager::removeBehaviacTree(std::string treeName){
  302. auto it = _behaviacTreeMap.find(treeName);
  303. if(it!= _behaviacTreeMap.end()){
  304. // CCLOG("_behaviacTreeMap size %lu", _behaviacTreeMap.size());
  305. auto tree = it->second;
  306. behaviac::Agent::Destroy(tree);
  307. _behaviacTreeMap.erase(it);
  308. // CCLOG("_behaviacTreeMap size %lu", _behaviacTreeMap.size());
  309. // behaviac::Workspace::GetInstance()->Cleanup();
  310. }
  311. }
  312. void ReboltRedManager::playOneTimeLine(std::string actionId, int tag){
  313. _redAnimationManager->runAnimationsWithTag(std::stoi(actionId), tag);
  314. }
  315. //MARK: - 各种回调代理接口
  316. //自己监听的自己的按钮回调
  317. void ReboltRedManager::onButtonClick(cocos2d::Ref * sender, cocos2d::extension::Control::EventType pControlEvent){
  318. auto it = _buttonNameMap.find(sender);
  319. if(it != _buttonNameMap.end()){
  320. std::string name = it->second;
  321. runBehaviacWhitButtonClick(name);
  322. for (int i = 0; i < _rootRedMangers.size(); i++) {
  323. RootReboltManger rrm = _rootRedMangers[i];
  324. ReboltRedManager* rootRedManger = rrm.rootRedManger;
  325. rootRedManger->onClickSubRedButton(sender, pControlEvent, rrm.selfPath, name);
  326. }
  327. }
  328. }
  329. // 子文件传递回来的,子red按钮的回调
  330. void ReboltRedManager::onClickSubRedButton(cocos2d::Ref * sender, cocos2d::extension::Control::EventType pControlEvent, std::string redPath, std::string clickName){
  331. runBehaviacWhitButtonClick(redPath + "::" + clickName);
  332. }
  333. //MARK: - 胶水代码对应接口
  334. void ReboltRedManager::playTimeLine(std::string actionId, ReboltErrorInfo& errorInfo){
  335. int tag = getAnimIndex();
  336. playOneTimeLine(actionId, tag);
  337. }
  338. void ReboltRedManager::stopTimeLine(ReboltErrorInfo& errorInfo){
  339. _redAnimationManager->stopAllNodeAction();
  340. }
  341. void ReboltRedManager::messageSend(std::string messageName, std::string messageValue){
  342. // runBehaviacWhitMsg(messageName);
  343. EventCustom event(EVENT_KEY);
  344. ValueMap message;
  345. message[ValueMapMessageKey] = Value(messageName);
  346. message[ValueMapMessageValue] = Value(messageValue);
  347. event.setUserData((void *)(&message));
  348. Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
  349. }
  350. void ReboltRedManager::showNode(std::string nodeName, ReboltErrorInfo& errorInfo){
  351. Node* node = getNodeByKey(nodeName, errorInfo);
  352. if(node != nullptr){
  353. node->setVisible(true);
  354. }
  355. }
  356. std::string ReboltRedManager::getGlobalString(std::string key, ReboltErrorInfo& errorInfo){
  357. return BTManger::getInstance()->getGlobalDataVar(key, errorInfo);
  358. }
  359. float ReboltRedManager::getGlobalFloat(std::string key, ReboltErrorInfo& errorInfo){
  360. std::string str = getGlobalString(key, errorInfo);
  361. float ret;
  362. try {
  363. ret = std::stof(str);
  364. } catch (std::invalid_argument) {
  365. ret = 0;
  366. } catch (std::out_of_range){
  367. ret = 0;
  368. }
  369. return ret;
  370. }
  371. std::string ReboltRedManager::getCoderString(std::string key, ReboltErrorInfo& errorInfo){
  372. auto it = _coderVarStringMap.find(key);
  373. if(it != _coderVarStringMap.end()){
  374. std::string str = it->second;
  375. return str;
  376. } else{
  377. errorInfo.errorType = ReboltErrorTypeError;
  378. errorInfo.errorDes = StringUtils::format("[%s]中的没有找到[%s]变量", _reboltId.c_str(), key.c_str());
  379. return "";
  380. }
  381. }
  382. float ReboltRedManager::getCoderFloat(std::string key, ReboltErrorInfo& errorInfo){
  383. std::string str = getCoderString(key, errorInfo);
  384. float ret;
  385. try {
  386. ret = std::stof(str);
  387. } catch (std::invalid_argument) {
  388. ret = 0;
  389. } catch (std::out_of_range){
  390. ret = 0;
  391. }
  392. return ret;
  393. }
  394. std::string ReboltRedManager::getCustomString(std::string key, ReboltErrorInfo& errorInfo){
  395. auto it = _customVarStringMap.find(key);
  396. if(it != _customVarStringMap.end()){
  397. std::string str = it->second;
  398. return str;
  399. } else{
  400. errorInfo.errorType = ReboltErrorTypeError;
  401. errorInfo.errorDes = StringUtils::format("[%s]中的没有找到[%s]变量", _reboltId.c_str(), key.c_str());
  402. return "";
  403. }
  404. }
  405. float ReboltRedManager::getCustomFloat(std::string key, ReboltErrorInfo& errorInfo){
  406. std::string str = getCustomString(key, errorInfo);
  407. float ret;
  408. try {
  409. ret = std::stof(str);
  410. } catch (std::invalid_argument) {
  411. ret = 0;
  412. } catch (std::out_of_range){
  413. ret = 0;
  414. }
  415. return ret;
  416. }
  417. vector<std::string>& ReboltRedManager::getGlobalVector(std::string key){
  418. return _localStringVectorMap[key];
  419. }
  420. void ReboltRedManager::setGlobalDataVar(std::string key, std::string content){
  421. BTManger::getInstance()->setGlobalDataVar(key, content);
  422. }
  423. void ReboltRedManager::setCustomDataVar(std::string key, std::string content){
  424. _customVarStringMap[key] = content;
  425. }
  426. void ReboltRedManager::setCoderDataVar(std::string key, std::string content){
  427. _coderVarStringMap[key] = content;
  428. }
  429. void ReboltRedManager::setLabelTitle(std::string labelName, std::string content, ReboltErrorInfo& errorInfo){
  430. cocos2d::Node* node = getNodeByKey(labelName, errorInfo);
  431. if(node != nullptr){
  432. cocos2d::Label* label= static_cast<cocos2d::Label*>(node);
  433. if(label != nullptr){
  434. label->setString(content);
  435. } else{
  436. errorInfo.errorType = ReboltErrorTypeError;
  437. errorInfo.errorDes = StringUtils::format("[%s] 名字对应的节点不是字体", labelName.c_str());
  438. }
  439. }
  440. }
  441. void ReboltRedManager::redProgressBar(std::string barName, float percentage, ReboltErrorInfo& errorInfo){
  442. cocos2d::Node* node = getNodeByKey(barName, errorInfo);
  443. if(node != nullptr){
  444. cocos2d::ProgressTimer* pro = static_cast<cocos2d::ProgressTimer*>(node);
  445. if(pro != nullptr){
  446. pro->setPercentage(percentage);
  447. } else{
  448. errorInfo.errorType = ReboltErrorTypeError;
  449. errorInfo.errorDes = StringUtils::format("[%s] 名字对应的节点不是进度条", barName.c_str());
  450. }
  451. }
  452. }
  453. float ReboltRedManager::getNodeGlobalPostionX(std::string nodeName, ReboltErrorInfo& errorInfo){
  454. cocos2d::Node* node = getNodeByKey(nodeName, errorInfo);
  455. if(node != nullptr){
  456. Node* par = node->getParent();
  457. if(par != nullptr){
  458. return par->convertToWorldSpace(node->getPosition()).x;
  459. } else {
  460. errorInfo.errorType = ReboltErrorTypeError;
  461. errorInfo.errorDes = "没有找到父节点无法转化为世界坐标";
  462. return 0;
  463. }
  464. } else {
  465. return 0;;
  466. }
  467. }
  468. float ReboltRedManager::getNodeGlobalPostionY(std::string nodeName, ReboltErrorInfo& errorInfo){
  469. cocos2d::Node* node = getNodeByKey(nodeName, errorInfo);
  470. if(node != nullptr){
  471. Node* par = node->getParent();
  472. if(par != nullptr){
  473. return par->convertToWorldSpace(node->getPosition()).y;
  474. } else {
  475. errorInfo.errorType = ReboltErrorTypeError;
  476. errorInfo.errorDes = "没有找到父节点无法转化为世界坐标";
  477. return 0;
  478. }
  479. } else {
  480. return 0;;
  481. }
  482. }
  483. void ReboltRedManager::setList(std::string dataName,std::vector<std::string> vec){
  484. _localStringVectorMap[dataName] = vec;
  485. }
  486. void ReboltRedManager::listVarAdd(std::string dataName, std::string content){
  487. std::vector<std::string> vec = getGlobalVector(dataName);
  488. vec.push_back(content);
  489. _localStringVectorMap[dataName] = vec;
  490. }
  491. float ReboltRedManager::listVarCount(std::string dataName){
  492. return getGlobalVector(dataName).size();
  493. }
  494. void ReboltRedManager::listVarDeleteAll(std::string dataName){
  495. _localStringVectorMap[dataName] = {};
  496. }
  497. void ReboltRedManager::listVarDeleteOne(std::string dataName, int index, ReboltErrorInfo& errorInfo){
  498. std::vector<std::string> vec = getGlobalVector(dataName);
  499. if(index < vec.size()){
  500. vec.erase(vec.begin() + index);
  501. _localStringVectorMap[dataName] = vec;
  502. } else {
  503. errorInfo.errorType = ReboltErrorTypeError;
  504. errorInfo.errorDes = StringUtils::format("删除的下标【%d】超出的全局数组【%s】的上限【%lu】", index, dataName.c_str(), vec.size());
  505. }
  506. }
  507. float ReboltRedManager::listVarFindValueID(std::string dataName, std::string content, ReboltErrorInfo& errorInfo){
  508. std::vector<std::string> vec = getGlobalVector(dataName);
  509. for(int i = 0; i < vec.size(); i++){
  510. string str = vec[i];
  511. if(content == str){
  512. return i;
  513. }
  514. }
  515. errorInfo.errorType = ReboltErrorTypeError;
  516. errorInfo.errorDes = StringUtils::format("数组【%s】中没有找到【%s】", dataName.c_str(), content.c_str());
  517. return 0;
  518. }
  519. bool ReboltRedManager::listVarHasValue(std::string dataName, std::string content){
  520. std::vector<std::string> vec = getGlobalVector(dataName);
  521. for(int i = 0; i < vec.size(); i++){
  522. string str = vec[i];
  523. if(content == str){
  524. return true;
  525. }
  526. }
  527. return false;
  528. }
  529. string ReboltRedManager::listVarIndexValue(std::string dataName, int index, ReboltErrorInfo& errorInfo){
  530. std::vector<std::string> vec = getGlobalVector(dataName);
  531. if(index < vec.size()){
  532. return vec[index];
  533. } else {
  534. errorInfo.errorType = ReboltErrorTypeError;
  535. errorInfo.errorDes = StringUtils::format("取值时下标【%d】超出的全局数组【%s】的上限【%lu】", index, dataName.c_str(), vec.size());
  536. return "";
  537. }
  538. }
  539. void ReboltRedManager::listVarInsertBefore(std::string dataName, int index, std::string content, ReboltErrorInfo& errorInfo){
  540. std::vector<std::string> vec = getGlobalVector(dataName);
  541. if(index < vec.size()){
  542. vec.insert(vec.begin() + index, content);
  543. _localStringVectorMap[dataName] = vec;
  544. } else {
  545. errorInfo.errorType = ReboltErrorTypeError;
  546. errorInfo.errorDes = StringUtils::format("插入时,下标【%d】超出的全局数组【%s】的上限【%lu】", index, dataName.c_str(), vec.size());
  547. }
  548. }
  549. void ReboltRedManager::listVarReplace(std::string dataName, int index, std::string content, ReboltErrorInfo& errorInfo){
  550. std::vector<std::string> vec = getGlobalVector(dataName);
  551. if(index < vec.size()){
  552. vec.insert(vec.begin() + index, content);
  553. vec.erase(vec.begin() + index + 1);
  554. _localStringVectorMap[dataName] = vec;
  555. } else {
  556. errorInfo.errorType = ReboltErrorTypeError;
  557. errorInfo.errorDes = StringUtils::format("替换时,下标【%d】超出的全局数组【%s】的上限【%lu】", index, dataName.c_str(),vec.size());
  558. }
  559. }
  560. //MARK: - AD
  561. bool ReboltRedManager::rewardVideoIsLoad(std::string adName){
  562. return ad::AdUtils::shared()->getVideoUtils()->hasVideo(adName);
  563. }
  564. // 控制子节点的内容
  565. void ReboltRedManager::playSubredTimeLine(std::string redPath, std::string actionName, ReboltErrorInfo& errorInfo){
  566. auto redManger = getReboltRedManagerByPath(redPath, errorInfo);
  567. if(redManger){
  568. redManger->playTimeLine(actionName, errorInfo);
  569. }
  570. }
  571. void ReboltRedManager::stopSubredTimeLine(std::string redPath, ReboltErrorInfo& errorInfo){
  572. auto redManger = getReboltRedManagerByPath(redPath, errorInfo);
  573. if(redManger){
  574. redManger->stopTimeLine(errorInfo);
  575. }
  576. }
  577. ReboltRedManager* ReboltRedManager::getReboltRedManagerByPath(std::string path, ReboltErrorInfo& errorInfo){
  578. auto redMangerIt = _pathRedMangerMap.find(path);
  579. if (redMangerIt != _pathRedMangerMap.end()){
  580. return redMangerIt->second;
  581. } else {
  582. errorInfo.errorType = ReboltErrorTypeError;
  583. std::string errorDec = StringUtils::format("找不到这个路径[%s]对应的red文件", path.c_str());
  584. errorInfo.errorDes = errorDec;
  585. return nullptr;
  586. }
  587. }
  588. void ReboltRedManager::subredProgressBar(std::string redPath, std::string barName, float percentage, ReboltErrorInfo& errorInfo){
  589. auto redManger = getReboltRedManagerByPath(redPath, errorInfo);
  590. if(redManger){
  591. redManger->redProgressBar(barName, percentage, errorInfo);
  592. }
  593. }
  594. float ReboltRedManager::getSubredNodeGlobalPostionX(std::string redPath, std::string nodeName, ReboltErrorInfo& errorInfo){
  595. auto redManger = getReboltRedManagerByPath(redPath, errorInfo);
  596. if(redManger){
  597. return redManger->getNodeGlobalPostionX(nodeName, errorInfo);
  598. } else {
  599. return 0;
  600. }
  601. }
  602. float ReboltRedManager::getSubredNodeGlobalPostionY(std::string redPath, std::string nodeName, ReboltErrorInfo& errorInfo){
  603. auto redManger = getReboltRedManagerByPath(redPath, errorInfo);
  604. if(redManger){
  605. return redManger->getNodeGlobalPostionY(nodeName, errorInfo);
  606. } else {
  607. return 0;
  608. }
  609. }
  610. void ReboltRedManager::setSubredLabelTitle(std::string redPath, std::string labelName, std::string content, ReboltErrorInfo& errorInfo){
  611. auto redManger = getReboltRedManagerByPath(redPath, errorInfo);
  612. if(redManger){
  613. redManger->setLabelTitle(labelName, content, errorInfo);
  614. }
  615. }
  616. void ReboltRedManager::setSpriteImage(std::string nodeId, std::string imagePath, ReboltErrorInfo& errorInfo){
  617. cocos2d::Node* node = getNodeByKey(nodeId, errorInfo);
  618. if(node != nullptr){
  619. cocos2d::Sprite* spr = static_cast<cocos2d::Sprite*>(node);
  620. if(spr != nullptr){
  621. Texture2D * tex = Director::getInstance()->getTextureCache()->addImage(imagePath);
  622. if(tex){
  623. spr->setTexture(imagePath);
  624. } else {
  625. errorInfo.errorType = ReboltErrorTypeError;
  626. errorInfo.errorDes = StringUtils::format("[%s] 没有找到这个纹理", imagePath.c_str());
  627. }
  628. } else{
  629. errorInfo.errorType = ReboltErrorTypeError;
  630. errorInfo.errorDes = StringUtils::format("[%s] 名字对应的节点不是Sprite", nodeId.c_str());
  631. }
  632. }
  633. }
  634. void ReboltRedManager::setSpritePlist(std::string nodeId, std::string plistPath, std::string frameName, ReboltErrorInfo& errorInfo){
  635. cocos2d::Node* node = getNodeByKey(nodeId, errorInfo);
  636. if(node != nullptr){
  637. cocos2d::Sprite* spr = static_cast<cocos2d::Sprite*>(node);
  638. if(spr != nullptr){
  639. SpriteFrameCache *cache = SpriteFrameCache::getInstance();
  640. SpriteFrame *spriteFrame = cache->getSpriteFrameByName(frameName,{plistPath});
  641. if(spriteFrame){
  642. spr->setSpriteFrame(spriteFrame);
  643. } else {
  644. cache->addSpriteFramesWithFile(plistPath);
  645. spriteFrame = cache->getSpriteFrameByName(frameName,{plistPath});
  646. if(spriteFrame){
  647. spr->setSpriteFrame(spriteFrame);
  648. } else {
  649. errorInfo.errorType = ReboltErrorTypeError;
  650. errorInfo.errorDes = StringUtils::format("[%s] [%s] 没有找到这个纹理",plistPath.c_str(), frameName.c_str());
  651. }
  652. }
  653. } else{
  654. errorInfo.errorType = ReboltErrorTypeError;
  655. errorInfo.errorDes = StringUtils::format("[%s] 名字对应的节点不是Sprite", nodeId.c_str());
  656. }
  657. }
  658. }
  659. void ReboltRedManager::setSubredSpriteImage(std::string redPath, std::string nodeId, std::string imagePath, ReboltErrorInfo& errorInfo){
  660. auto redManger = getReboltRedManagerByPath(redPath, errorInfo);
  661. if(redManger){
  662. redManger->setSpriteImage(nodeId, imagePath, errorInfo);
  663. }
  664. }
  665. void ReboltRedManager::setSubredSpritePlist(std::string redPath, std::string nodeId, std::string plistPath, std::string frameName, ReboltErrorInfo& errorInfo){
  666. auto redManger = getReboltRedManagerByPath(redPath, errorInfo);
  667. if(redManger){
  668. redManger->setSpritePlist(nodeId, plistPath, frameName, errorInfo);
  669. }
  670. }
  671. std::string ReboltRedManager::getAnimFileKey(std::string nodeId, std::string animPath){
  672. return nodeId + animPath;
  673. }
  674. void ReboltRedManager::runAnimFile(cocos2d::Node* node, std::string animPath, cocos2d::Vec2 startPos, cocos2d::Vec2 endPos, const std::function<void()> &func, ReboltErrorInfo& errorInfo){
  675. Node* par = node->getParent();
  676. if(par){
  677. Vec2 localStartPos = par->convertToNodeSpace(startPos);
  678. Vec2 localEndPos = par->convertToNodeSpace(endPos);
  679. auto anim = redream::RedreamAnim::createFromAnimFile(animPath);
  680. anim->setStartPosition(localStartPos);
  681. anim->setEndPosition(localEndPos);
  682. cocos2d::CallFunc* callfun = cocos2d::CallFunc::create(func);
  683. auto seq = cocos2d::Sequence::create((cocos2d::ActionInterval*)anim->getAction(), callfun, NULL);
  684. node->runAction(seq);
  685. } else {
  686. errorInfo.errorType = ReboltErrorTypeError;
  687. errorInfo.errorDes = StringUtils::format("播放动画文件的节点,没有父节点,所以他不能播放");
  688. }
  689. }
  690. void ReboltRedManager::nodeSetAnimAction(std::string nodeId, std::string animPath, cocos2d::Vec2 startPos, cocos2d::Vec2 endPos, ReboltErrorInfo& errorInfo){
  691. std::string key = getAnimFileKey(nodeId, animPath);
  692. cocos2d::Node* node = getNodeByKey(nodeId, errorInfo);
  693. if(node){
  694. runAnimFile(node, animPath, startPos, endPos, [=]{
  695. auto asIt = _animFileStatusMap.find(key);
  696. if(asIt != _animFileStatusMap.end()){
  697. _animFileStatusMap.erase(asIt);
  698. }
  699. }, errorInfo);
  700. _animFileStatusMap[key] = ReboltRedManagerBT_RUNNING;
  701. }
  702. }
  703. void ReboltRedManager::playWiseSound(std::string& bnkPath, std::string& eventName, ReboltErrorInfo& errorInfo){
  704. // CCLOG("ReboltRedManager::playWiseSound bnkPath %s, eventName %s", bnkPath.c_str(), eventName.c_str());
  705. RWReturn rwReturn = RedWise::getInstance()->postEventByBnk(bnkPath, eventName);
  706. if(rwReturn.error != ""){
  707. errorInfo.errorType = ReboltErrorTypeError;
  708. string err = "播放Wise声音失败【" + bnkPath + "】【" + eventName + "】,错误原因 : " + rwReturn.error;
  709. errorInfo.errorDes = err;
  710. } else if (rwReturn.warn != ""){
  711. errorInfo.errorType = ReboltErrorTypeWarn;
  712. string err = "播放Wise声音异常【" + bnkPath + "】【" + eventName + "】,异常原因 : " + rwReturn.warn;
  713. errorInfo.errorDes = err;
  714. }
  715. }
  716. void ReboltRedManager::setNotifyDevelopmentDelegate4AllChildren(NotifyDevelopmentDelegate* ndd) {
  717. setNotifyDevelopmentDelegate(ndd);
  718. for (auto& rmm : _pathRedMangerMap) {
  719. rmm.second->setNotifyDevelopmentDelegate4AllChildren(ndd);
  720. }
  721. }
  722. };