ReboltRedManager.cpp 30 KB

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