// // ReboltRedManager.cpp // empty2dx-desktop // // Created by zhu on 2022/8/3. // #include "ReboltRedManager.h" #include "extensions/GUI/CCControlExtension/CCControl.h" #include "AdUtils.h" #include "RedreamAnim.h" #include "BTManger.hpp" #include "RedWise.hpp" using namespace cocos2d::extension; namespace redream { #define TreeType_btn "btn" #define TreeType_msg "msg" #define TreeType_func "func" #define ValueMapMessageKey "messageKey" #define ValueMapMessageValue "messageValue" #define UPDATE_KEY "__rsm_reboltSceneManger__sys" #define EVENT_KEY "__rsm_reboltRedManager__event__sys" ReboltRedManager::ReboltRedManager() : _reboltId("") , _redAnimationManager(nullptr) , _rootNode(nullptr) , _treeIndex(0) , _animIndex(0) , _notifyDevelopmentDelegate(nullptr) , _reboltRunDelegate(nullptr) { _nodeMap.clear(); _customVarStringMap.clear(); _coderVarStringMap.clear(); _buttonNameMap.clear(); _behaviacTreeMap.clear(); _treeTypeNameMap.clear(); _waitDelTreeNameVec.clear(); _pathRedMangerMap.clear(); _rootRedMangers.clear(); _animFileStatusMap.clear(); _waitAddTreeMap.clear(); } ReboltRedManager::~ReboltRedManager(){ CC_SAFE_RELEASE(_redAnimationManager); for(auto it = _buttonNameMap.begin(); it != _buttonNameMap.end(); it++){ auto button = (cocos2d::extension::Control*)it->first; button->removeTargetWithActionForControlEvents(this, cccontrol_selector(ReboltRedManager::onButtonClick), cocos2d::extension::Control::EventType::ALL); } for(auto it = _behaviacTreeMap.begin(); it != _behaviacTreeMap.end(); it++){ auto tree = it->second; behaviac::Agent::Destroy(tree); } for(auto it : _waitAddTreeMap){ auto tree = it.second; behaviac::Agent::Destroy(tree); } _waitAddTreeMap.clear(); cocos2d::Director::getInstance()->getScheduler()->unschedule(UPDATE_KEY, _rootNode); _nodeMap.clear(); _customVarStringMap.clear(); _coderVarStringMap.clear(); _buttonNameMap.clear(); _behaviacTreeMap.clear(); _treeTypeNameMap.clear(); _waitDelTreeNameVec.clear(); _pathRedMangerMap.clear(); _rootRedMangers.clear(); _animFileStatusMap.clear(); Director::getInstance()->getEventDispatcher()->removeEventListener(_eventListener); } void ReboltRedManager::parseTreeMapAndStartUpdate(){ for(auto it = _pathRedMangerMap.begin(); it != _pathRedMangerMap.end(); it++){ string path = it->first; auto subReboltRedManager = it->second; _nodeMap[path] = subReboltRedManager->getRootNode(); } cocos2d::Director::getInstance()->getScheduler()->schedule([=](float dt){ this->update(dt); }, _rootNode, 0.016, !_rootNode->isRunning(), UPDATE_KEY); _eventListener = EventListenerCustom::create(EVENT_KEY, [=](EventCustom* event){ ValueMap message = *(ValueMap*)event->getUserData(); runBehaviacWhitMsg(message); }); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(_eventListener, _rootNode); } red::RedBehaviacTree* ReboltRedManager::createBehaviacTree(std::string treeRandomId, const std::map &boolMap, const std::map &stringMap){ std::string treeName = treeRandomId + "::" + to_string(getTreeIndex()); behaviac::Workspace::GetInstance()->SetFilePath(""); behaviac::Workspace::GetInstance()->SetFileFormat(behaviac::Workspace::EFF_xml); auto redBehaviacTree = behaviac::Agent::Create(); redBehaviacTree->setReboltRedManager(this); redBehaviacTree->setTreeName(treeName); redBehaviacTree->setReboltRunDelegate(_reboltRunDelegate); bool bRet = false; string filePath = "Rebolt_BT/" + treeRandomId; bRet = redBehaviacTree->btload(treeRandomId.c_str()); if(!bRet){ CCLOG("创建树失败 %s, %s", _reboltId.c_str(), treeRandomId.c_str()); CCASSERT(false, "创建树失败"); return nullptr; } { //行为树设置必须的属性 redBehaviacTree->btsetcurrent(treeRandomId.c_str()); redBehaviacTree->setArgumentsBoolMap(boolMap); redBehaviacTree->setArgumentsStringMap(stringMap); } addBehaviacTree(redBehaviacTree, treeName); return redBehaviacTree; } std::string ReboltRedManager::runBehaviacTree(const std::string& treeRandomId, const std::map& boolMap, const std::map& stringMap){ auto redBehaviacTree = createBehaviacTree(treeRandomId, boolMap, stringMap); if(!redBehaviacTree){ return ""; } auto treeName = redBehaviacTree->getTreeName(); redBehaviacTree->runBehaviacTree(); return treeName; } void ReboltRedManager::update(float dt){ std::map::iterator iter1; for (iter1 = _behaviacTreeMap.begin();iter1 != _behaviacTreeMap.end();iter1++) { iter1->second->update(); } for(auto it : _waitAddTreeMap){ _behaviacTreeMap[it.first] = it.second; } _waitAddTreeMap.clear(); for(int i = 0; i < _waitDelTreeNameVec.size(); i++){ std::string delTreeName = _waitDelTreeNameVec[i]; removeBehaviacTree(delTreeName); } _waitDelTreeNameVec.clear(); } void ReboltRedManager::runBehaviacWhitButtonClick(std::string btnName){ auto btnInMainNameIdMapIt = _treeTypeNameMap.find(TreeType_btn); map buttonNameIdMap; if(btnInMainNameIdMapIt != _treeTypeNameMap.end()){ buttonNameIdMap = btnInMainNameIdMapIt->second; auto nameIdIt = buttonNameIdMap.find(btnName); if(nameIdIt != buttonNameIdMap.end()){ runBehaviacTree(nameIdIt->second, {}, {}); } } } void ReboltRedManager::runBehaviacWhitMsg(ValueMap& msg){ auto msgNameIdMapIt = _treeTypeNameMap.find(TreeType_msg); if(msgNameIdMapIt != _treeTypeNameMap.end()){ map msgNameIdMap = msgNameIdMapIt->second; string messageKey = msg.at(ValueMapMessageKey).asString(); auto nameIdIt = msgNameIdMap.find(messageKey); if(nameIdIt != msgNameIdMap.end()){ string messageValue = msg.at(ValueMapMessageValue).asString(); // CCLOG("message [%s]-----[%s]", messageKey.c_str(), messageValue.c_str()); std::map boolMap = {}; std::map stringMap; stringMap["消息参数"] = messageValue; runBehaviacTree(nameIdIt->second, boolMap, stringMap); } else { CCLOG("消息类型存在但是消息名称不存在"); } } else { CCLOG("消息类型不存在"); } } std::string ReboltRedManager::runBehaviacWhitFunName(std::string name, std::map boolMap, std::map stringMap){ auto funNameIdMapIt = _treeTypeNameMap.find(TreeType_func); if(funNameIdMapIt != _treeTypeNameMap.end()){ map funNameIdMap = funNameIdMapIt->second; auto nameIdIt = funNameIdMap.find(name); if(nameIdIt != funNameIdMap.end()){ return runBehaviacTree(nameIdIt->second, boolMap, stringMap); } else { CCASSERT(false, "函数类型存在但是函数名称不存在"); } } else { CCASSERT(false, "函数类型不存在"); } return ""; } void ReboltRedManager::onTreeRunEnd(int endType, std::string treeName){ _waitDelTreeNameVec.push_back(treeName); } void ReboltRedManager::onSubTreeRunEnd(int endType, std::string redPath, std::string treeName){ // 没实现子树调用,需要的话需要在子red的onTreeRunEnd调用这个方法 } void ReboltRedManager::updateTreeWithName(std::string redPath, std::string treeName, std::string subTreeName){ auto it = _behaviacTreeMap.find(treeName); if(it!= _behaviacTreeMap.end()){ auto tree = it->second; tree->updateBySubTree(); } } bool ReboltRedManager::onAssignREDReboltRedManagerReboltName(cocos2d::Ref* target, const char* reboltName, const char* reboltId, cocos2d::Node* pNode){ onResolveREDControlRebolt(target, reboltName, reboltId, pNode); _nodeMap[reboltId] = pNode; return false; } void ReboltRedManager::onResolveREDControlRebolt(cocos2d::Ref* target, const char* reboltName, const char* reboltId, cocos2d::Node* pNode){ auto controlNode = dynamic_cast(pNode); if(controlNode){ _buttonNameMap[controlNode] = reboltId; controlNode->addTargetWithActionForControlEvents(target, cccontrol_selector(ReboltRedManager::onButtonClick), Control::EventType::TOUCH_UP_INSIDE); } } int ReboltRedManager::getTreeIndex(){ _treeIndex++; return _treeIndex; } int ReboltRedManager::getAnimIndex(){ _animIndex++; return _animIndex; } cocos2d::Node* ReboltRedManager::getNodeByKey(std::string nodeName, ReboltErrorInfo& errorInfo){ auto it = _nodeMap.find(nodeName); if(it != _nodeMap.end()){ return it->second; } else { errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = StringUtils::format("没找到【%s】中的【%s】节点", _reboltId.c_str(), nodeName.c_str()); return nullptr; } } cocos2d::Node* ReboltRedManager::getSubredNodeByKey(std::string redPath, std::string key, ReboltErrorInfo& errorInfo){ auto redManger = getReboltRedManagerByPath(redPath, errorInfo); if(redManger){ return redManger->getNodeByKey(key, errorInfo); } else { return nullptr; } } cocos2d::extension::Control* ReboltRedManager::getControlByKey(std::string key, ReboltErrorInfo& errorInfo){ cocos2d::Node* node = getNodeByKey(key, errorInfo); if(node != nullptr){ auto control = static_cast(node); if(control != nullptr){ return control; } else{ errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = StringUtils::format("[%s] 名字对应的节点不是按钮", key.c_str()); return nullptr; } } else { return nullptr; } } cocos2d::extension::Control* ReboltRedManager::getSubredControlByKey(std::string redPath, std::string key, ReboltErrorInfo& errorInfo){ auto redManger = getReboltRedManagerByPath(redPath, errorInfo); if(redManger){ return redManger->getControlByKey(key, errorInfo); } else { errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = StringUtils::format("[%s] 子red不存在", redPath.c_str()); return nullptr; } } //MARK: - 解析过程中的接口 void ReboltRedManager::setREDAnimationManager(REDAnimationManager* pREDAnimationManager){ CC_SAFE_RELEASE(_redAnimationManager); _redAnimationManager = pREDAnimationManager; CC_SAFE_RETAIN(_redAnimationManager); } bool ReboltRedManager::onAssignREDReboltRedManagerReboltPath(cocos2d::Ref* target, const char* reboltPath, ReboltRedManager* pReboltRedManager){ _pathRedMangerMap.insert(reboltPath, pReboltRedManager); return true; } REDAnimationManager* ReboltRedManager::getREDAnimationManager(){ return _redAnimationManager; } REDAnimationManager* ReboltRedManager::getSubREDAnimationManager(std::string redPath){ ReboltErrorInfo errInfo; auto redManger = getReboltRedManagerByPath(redPath, errInfo); if(errInfo.errorType == ReboltErrorTypeError){ CCASSERT(false, errInfo.errorDes); } if(redManger){ return redManger->getREDAnimationManager(); } else { return nullptr; } } void ReboltRedManager::setReboltId(const std::string rId){ _reboltId = rId; } std::string ReboltRedManager::getReboltId(){ return _reboltId; } cocos2d::Node* ReboltRedManager::getRootNode() { return _rootNode; } void ReboltRedManager::setRootNode(cocos2d::Node *pRootNode) { _rootNode = pRootNode; } void ReboltRedManager::addBehaviacTree(red::RedBehaviacTree* tree, std::string treeName){ // _behaviacTreeMap[treeName] = tree; _waitAddTreeMap[treeName] = tree; } void ReboltRedManager::removeBehaviacTree(std::string treeName){ auto it = _behaviacTreeMap.find(treeName); if(it!= _behaviacTreeMap.end()){ // CCLOG("_behaviacTreeMap size %lu", _behaviacTreeMap.size()); auto tree = it->second; behaviac::Agent::Destroy(tree); _behaviacTreeMap.erase(it); // CCLOG("_behaviacTreeMap size %lu", _behaviacTreeMap.size()); // behaviac::Workspace::GetInstance()->Cleanup(); } } void ReboltRedManager::playOneTimeLine(std::string actionId, int tag){ _redAnimationManager->runAnimationsWithTag(std::stoi(actionId), tag); } //MARK: - 各种回调代理接口 //自己监听的自己的按钮回调 void ReboltRedManager::onButtonClick(cocos2d::Ref * sender, cocos2d::extension::Control::EventType pControlEvent){ auto it = _buttonNameMap.find(sender); if(it != _buttonNameMap.end()){ std::string name = it->second; runBehaviacWhitButtonClick(name); for (int i = 0; i < _rootRedMangers.size(); i++) { RootReboltManger rrm = _rootRedMangers[i]; ReboltRedManager* rootRedManger = rrm.rootRedManger; rootRedManger->onClickSubRedButton(sender, pControlEvent, rrm.selfPath, name); } } } // 子文件传递回来的,子red按钮的回调 void ReboltRedManager::onClickSubRedButton(cocos2d::Ref * sender, cocos2d::extension::Control::EventType pControlEvent, std::string redPath, std::string clickName){ runBehaviacWhitButtonClick(redPath + "::" + clickName); } //MARK: - 胶水代码对应接口 void ReboltRedManager::playTimeLine(std::string actionId, ReboltErrorInfo& errorInfo){ int tag = getAnimIndex(); playOneTimeLine(actionId, tag); } void ReboltRedManager::stopTimeLine(ReboltErrorInfo& errorInfo){ _redAnimationManager->stopAllNodeAction(); } void ReboltRedManager::messageSend(std::string messageName, std::string messageValue){ // runBehaviacWhitMsg(messageName); EventCustom event(EVENT_KEY); ValueMap message; message[ValueMapMessageKey] = Value(messageName); message[ValueMapMessageValue] = Value(messageValue); event.setUserData((void *)(&message)); Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); } void ReboltRedManager::showNode(std::string nodeName, ReboltErrorInfo& errorInfo){ Node* node = getNodeByKey(nodeName, errorInfo); if(node != nullptr){ node->setVisible(true); } } std::string ReboltRedManager::getGlobalString(std::string key, ReboltErrorInfo& errorInfo){ return BTManger::getInstance()->getGlobalDataVar(key, errorInfo); } float ReboltRedManager::getGlobalFloat(std::string key, ReboltErrorInfo& errorInfo){ std::string str = getGlobalString(key, errorInfo); float ret; try { ret = std::stof(str); } catch (std::invalid_argument) { ret = 0; } catch (std::out_of_range){ ret = 0; } return ret; } std::string ReboltRedManager::getCoderString(std::string key, ReboltErrorInfo& errorInfo){ auto it = _coderVarStringMap.find(key); if(it != _coderVarStringMap.end()){ std::string str = it->second; return str; } else{ errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = StringUtils::format("[%s]中的没有找到[%s]变量", _reboltId.c_str(), key.c_str()); return ""; } } float ReboltRedManager::getCoderFloat(std::string key, ReboltErrorInfo& errorInfo){ std::string str = getCoderString(key, errorInfo); float ret; try { ret = std::stof(str); } catch (std::invalid_argument) { ret = 0; } catch (std::out_of_range){ ret = 0; } return ret; } std::string ReboltRedManager::getCustomString(std::string key, ReboltErrorInfo& errorInfo){ auto it = _customVarStringMap.find(key); if(it != _customVarStringMap.end()){ std::string str = it->second; return str; } else{ errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = StringUtils::format("[%s]中的没有找到[%s]变量", _reboltId.c_str(), key.c_str()); return ""; } } float ReboltRedManager::getCustomFloat(std::string key, ReboltErrorInfo& errorInfo){ std::string str = getCustomString(key, errorInfo); float ret; try { ret = std::stof(str); } catch (std::invalid_argument) { ret = 0; } catch (std::out_of_range){ ret = 0; } return ret; } vector& ReboltRedManager::getGlobalVector(std::string key){ return _localStringVectorMap[key]; } void ReboltRedManager::setGlobalDataVar(std::string key, std::string content){ BTManger::getInstance()->setGlobalDataVar(key, content); } void ReboltRedManager::setCustomDataVar(std::string key, std::string content){ _customVarStringMap[key] = content; } void ReboltRedManager::setCoderDataVar(std::string key, std::string content){ _coderVarStringMap[key] = content; } void ReboltRedManager::setLabelTitle(std::string labelName, std::string content, ReboltErrorInfo& errorInfo){ cocos2d::Node* node = getNodeByKey(labelName, errorInfo); if(node != nullptr){ cocos2d::Label* label= static_cast(node); if(label != nullptr){ label->setString(content); } else{ errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = StringUtils::format("[%s] 名字对应的节点不是字体", labelName.c_str()); } } } void ReboltRedManager::redProgressBar(std::string barName, float percentage, ReboltErrorInfo& errorInfo){ cocos2d::Node* node = getNodeByKey(barName, errorInfo); if(node != nullptr){ cocos2d::ProgressTimer* pro = static_cast(node); if(pro != nullptr){ pro->setPercentage(percentage); } else{ errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = StringUtils::format("[%s] 名字对应的节点不是进度条", barName.c_str()); } } } float ReboltRedManager::getNodeGlobalPostionX(std::string nodeName, ReboltErrorInfo& errorInfo){ cocos2d::Node* node = getNodeByKey(nodeName, errorInfo); if(node != nullptr){ Node* par = node->getParent(); if(par != nullptr){ return par->convertToWorldSpace(node->getPosition()).x; } else { errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = "没有找到父节点无法转化为世界坐标"; return 0; } } else { return 0;; } } float ReboltRedManager::getNodeGlobalPostionY(std::string nodeName, ReboltErrorInfo& errorInfo){ cocos2d::Node* node = getNodeByKey(nodeName, errorInfo); if(node != nullptr){ Node* par = node->getParent(); if(par != nullptr){ return par->convertToWorldSpace(node->getPosition()).y; } else { errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = "没有找到父节点无法转化为世界坐标"; return 0; } } else { return 0;; } } void ReboltRedManager::setList(std::string dataName,std::vector vec){ _localStringVectorMap[dataName] = vec; } void ReboltRedManager::listVarAdd(std::string dataName, std::string content){ std::vector vec = getGlobalVector(dataName); vec.push_back(content); _localStringVectorMap[dataName] = vec; } float ReboltRedManager::listVarCount(std::string dataName){ return getGlobalVector(dataName).size(); } void ReboltRedManager::listVarDeleteAll(std::string dataName){ _localStringVectorMap[dataName] = {}; } void ReboltRedManager::listVarDeleteOne(std::string dataName, int index, ReboltErrorInfo& errorInfo){ std::vector vec = getGlobalVector(dataName); if(index < vec.size()){ vec.erase(vec.begin() + index); _localStringVectorMap[dataName] = vec; } else { errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = StringUtils::format("删除的下标【%d】超出的全局数组【%s】的上限【%lu】", index, dataName.c_str(), vec.size()); } } float ReboltRedManager::listVarFindValueID(std::string dataName, std::string content, ReboltErrorInfo& errorInfo){ std::vector vec = getGlobalVector(dataName); for(int i = 0; i < vec.size(); i++){ string str = vec[i]; if(content == str){ return i; } } errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = StringUtils::format("数组【%s】中没有找到【%s】", dataName.c_str(), content.c_str()); return 0; } bool ReboltRedManager::listVarHasValue(std::string dataName, std::string content){ std::vector vec = getGlobalVector(dataName); for(int i = 0; i < vec.size(); i++){ string str = vec[i]; if(content == str){ return true; } } return false; } string ReboltRedManager::listVarIndexValue(std::string dataName, int index, ReboltErrorInfo& errorInfo){ std::vector vec = getGlobalVector(dataName); if(index < vec.size()){ return vec[index]; } else { errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = StringUtils::format("取值时下标【%d】超出的全局数组【%s】的上限【%lu】", index, dataName.c_str(), vec.size()); return ""; } } void ReboltRedManager::listVarInsertBefore(std::string dataName, int index, std::string content, ReboltErrorInfo& errorInfo){ std::vector vec = getGlobalVector(dataName); if(index < vec.size()){ vec.insert(vec.begin() + index, content); _localStringVectorMap[dataName] = vec; } else { errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = StringUtils::format("插入时,下标【%d】超出的全局数组【%s】的上限【%lu】", index, dataName.c_str(), vec.size()); } } void ReboltRedManager::listVarReplace(std::string dataName, int index, std::string content, ReboltErrorInfo& errorInfo){ std::vector vec = getGlobalVector(dataName); if(index < vec.size()){ vec.insert(vec.begin() + index, content); vec.erase(vec.begin() + index + 1); _localStringVectorMap[dataName] = vec; } else { errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = StringUtils::format("替换时,下标【%d】超出的全局数组【%s】的上限【%lu】", index, dataName.c_str(),vec.size()); } } //MARK: - AD bool ReboltRedManager::rewardVideoIsLoad(std::string adName){ return ad::AdUtils::shared()->getVideoUtils()->hasVideo(adName); } // 控制子节点的内容 void ReboltRedManager::playSubredTimeLine(std::string redPath, std::string actionName, ReboltErrorInfo& errorInfo){ auto redManger = getReboltRedManagerByPath(redPath, errorInfo); if(redManger){ redManger->playTimeLine(actionName, errorInfo); } } void ReboltRedManager::stopSubredTimeLine(std::string redPath, ReboltErrorInfo& errorInfo){ auto redManger = getReboltRedManagerByPath(redPath, errorInfo); if(redManger){ redManger->stopTimeLine(errorInfo); } } ReboltRedManager* ReboltRedManager::getReboltRedManagerByPath(std::string path, ReboltErrorInfo& errorInfo){ auto redMangerIt = _pathRedMangerMap.find(path); if (redMangerIt != _pathRedMangerMap.end()){ return redMangerIt->second; } else { errorInfo.errorType = ReboltErrorTypeError; std::string errorDec = StringUtils::format("找不到这个路径[%s]对应的red文件", path.c_str()); errorInfo.errorDes = errorDec; return nullptr; } } void ReboltRedManager::subredProgressBar(std::string redPath, std::string barName, float percentage, ReboltErrorInfo& errorInfo){ auto redManger = getReboltRedManagerByPath(redPath, errorInfo); if(redManger){ redManger->redProgressBar(barName, percentage, errorInfo); } } float ReboltRedManager::getSubredNodeGlobalPostionX(std::string redPath, std::string nodeName, ReboltErrorInfo& errorInfo){ auto redManger = getReboltRedManagerByPath(redPath, errorInfo); if(redManger){ return redManger->getNodeGlobalPostionX(nodeName, errorInfo); } else { return 0; } } float ReboltRedManager::getSubredNodeGlobalPostionY(std::string redPath, std::string nodeName, ReboltErrorInfo& errorInfo){ auto redManger = getReboltRedManagerByPath(redPath, errorInfo); if(redManger){ return redManger->getNodeGlobalPostionY(nodeName, errorInfo); } else { return 0; } } void ReboltRedManager::setSubredLabelTitle(std::string redPath, std::string labelName, std::string content, ReboltErrorInfo& errorInfo){ auto redManger = getReboltRedManagerByPath(redPath, errorInfo); if(redManger){ redManger->setLabelTitle(labelName, content, errorInfo); } } void ReboltRedManager::setSpriteImage(std::string nodeId, std::string imagePath, ReboltErrorInfo& errorInfo){ cocos2d::Node* node = getNodeByKey(nodeId, errorInfo); if(node != nullptr){ cocos2d::Sprite* spr = static_cast(node); if(spr != nullptr){ Texture2D * tex = Director::getInstance()->getTextureCache()->addImage(imagePath); if(tex){ spr->setTexture(imagePath); } else { errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = StringUtils::format("[%s] 没有找到这个纹理", imagePath.c_str()); } } else{ errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = StringUtils::format("[%s] 名字对应的节点不是Sprite", nodeId.c_str()); } } } void ReboltRedManager::setSpritePlist(std::string nodeId, std::string plistPath, std::string frameName, ReboltErrorInfo& errorInfo){ cocos2d::Node* node = getNodeByKey(nodeId, errorInfo); if(node != nullptr){ cocos2d::Sprite* spr = static_cast(node); if(spr != nullptr){ SpriteFrameCache *cache = SpriteFrameCache::getInstance(); SpriteFrame *spriteFrame = cache->getSpriteFrameByName(frameName,{plistPath}); if(spriteFrame){ spr->setSpriteFrame(spriteFrame); } else { cache->addSpriteFramesWithFile(plistPath); spriteFrame = cache->getSpriteFrameByName(frameName,{plistPath}); if(spriteFrame){ spr->setSpriteFrame(spriteFrame); } else { errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = StringUtils::format("[%s] [%s] 没有找到这个纹理",plistPath.c_str(), frameName.c_str()); } } } else{ errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = StringUtils::format("[%s] 名字对应的节点不是Sprite", nodeId.c_str()); } } } void ReboltRedManager::setSubredSpriteImage(std::string redPath, std::string nodeId, std::string imagePath, ReboltErrorInfo& errorInfo){ auto redManger = getReboltRedManagerByPath(redPath, errorInfo); if(redManger){ redManger->setSpriteImage(nodeId, imagePath, errorInfo); } } void ReboltRedManager::setSubredSpritePlist(std::string redPath, std::string nodeId, std::string plistPath, std::string frameName, ReboltErrorInfo& errorInfo){ auto redManger = getReboltRedManagerByPath(redPath, errorInfo); if(redManger){ redManger->setSpritePlist(nodeId, plistPath, frameName, errorInfo); } } std::string ReboltRedManager::getAnimFileKey(std::string nodeId, std::string animPath){ return nodeId + animPath; } void ReboltRedManager::runAnimFile(cocos2d::Node* node, std::string animPath, cocos2d::Vec2 startPos, cocos2d::Vec2 endPos, const std::function &func, ReboltErrorInfo& errorInfo){ Node* par = node->getParent(); if(par){ Vec2 localStartPos = par->convertToNodeSpace(startPos); Vec2 localEndPos = par->convertToNodeSpace(endPos); auto anim = redream::RedreamAnim::createFromAnimFile(animPath); anim->setStartPosition(localStartPos); anim->setEndPosition(localEndPos); cocos2d::CallFunc* callfun = cocos2d::CallFunc::create(func); auto seq = cocos2d::Sequence::create((cocos2d::ActionInterval*)anim->getAction(), callfun, NULL); node->runAction(seq); } else { errorInfo.errorType = ReboltErrorTypeError; errorInfo.errorDes = StringUtils::format("播放动画文件的节点,没有父节点,所以他不能播放"); } } void ReboltRedManager::nodeSetAnimAction(std::string nodeId, std::string animPath, cocos2d::Vec2 startPos, cocos2d::Vec2 endPos, ReboltErrorInfo& errorInfo){ std::string key = getAnimFileKey(nodeId, animPath); cocos2d::Node* node = getNodeByKey(nodeId, errorInfo); if(node){ runAnimFile(node, animPath, startPos, endPos, [=]{ auto asIt = _animFileStatusMap.find(key); if(asIt != _animFileStatusMap.end()){ _animFileStatusMap.erase(asIt); } }, errorInfo); _animFileStatusMap[key] = ReboltRedManagerBT_RUNNING; } } void ReboltRedManager::playWiseSound(std::string& bnkPath, std::string& eventName, ReboltErrorInfo& errorInfo){ // CCLOG("ReboltRedManager::playWiseSound bnkPath %s, eventName %s", bnkPath.c_str(), eventName.c_str()); RWReturn rwReturn = RedWise::getInstance()->postEventByBnk(bnkPath, eventName); if(rwReturn.error != ""){ errorInfo.errorType = ReboltErrorTypeError; string err = "播放Wise声音失败【" + bnkPath + "】【" + eventName + "】,错误原因 : " + rwReturn.error; errorInfo.errorDes = err; } else if (rwReturn.warn != ""){ errorInfo.errorType = ReboltErrorTypeWarn; string err = "播放Wise声音异常【" + bnkPath + "】【" + eventName + "】,异常原因 : " + rwReturn.warn; errorInfo.errorDes = err; } } void ReboltRedManager::setNotifyDevelopmentDelegate4AllChildren(NotifyDevelopmentDelegate* ndd) { setNotifyDevelopmentDelegate(ndd); for (auto& rmm : _pathRedMangerMap) { rmm.second->setNotifyDevelopmentDelegate4AllChildren(ndd); } } };