12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- #include "RUBehaviacTree.h"
- #include "RUBehaviacTreeManger.h"
- NS_RU_BEGIN
- RUBehaviacTree::RUBehaviacTree()
- {
-
- }
- RUBehaviacTree::~RUBehaviacTree()
- {
- }
- bool RUBehaviacTree::loadBt(const std::string& btName)
- {
- std::string btFile = btName.substr(0, btName.find(".xml"));
- bool isOk = btload(btFile.c_str());
- CCASSERT(isOk, "加载行为树失败");
- if(isOk) {
- btsetcurrent(btFile.c_str());
- }
- return isOk;
- }
- void RUBehaviacTree::setData(const std::map<std::string, std::string>& stringMap)
- {
-
- }
- void RUBehaviacTree::runBehaviacTree(){
- runBehaviacTree({},nullptr,false);
- }
- void RUBehaviacTree::runBehaviacTree(const std::map<std::string, std::string>& stringMap, std::function<void(bool isSuccess)> treeRunEndCb, bool removeSelf){
- _isRunning = true;
- _removeSelf = removeSelf;
- setData(stringMap);
- _treeRunEndCb = treeRunEndCb;
- RUBehaviacTreeManger::getInstance()->_addBehaviacTree(this);
- _update();
- }
- void RUBehaviacTree::_update(){
- if(_isRunning){
- behaviac::EBTStatus status = btexec();
- if(status != behaviac::EBTStatus::BT_RUNNING){
- _isRunning = false;
- RUBehaviacTreeManger::getInstance()->_removeBehaviacTree(this);
- if (_treeRunEndCb != nullptr) {
- ///因为同一个物件连续执行多个事件时,runBehaviacTree函数中会把_treeRunEndCb覆盖,导致地址错误。
- std::function<void(bool isSuccess)> tTreeRunEndCb = _treeRunEndCb;
- _treeRunEndCb = nullptr;
- tTreeRunEndCb(status == behaviac::EBTStatus::BT_SUCCESS);
- }
- if (_removeSelf) {
- behaviac::Agent::Destroy(this);
- }
- }
- }
- }
- behaviac::EBTStatus RUBehaviacTree::_runIntervalAction(const std::string& actKey, const std::function<void(std::function<void(bool isSuccess)>)>& actFunc)
- {
- if (actKey != _actKey) {
- _actKey = actKey;
- _actStatus = behaviac::EBTStatus::BT_INVALID;
- }
- behaviac::EBTStatus status = _actStatus;
- if (status == behaviac::EBTStatus::BT_INVALID) {
- _actStatus = behaviac::EBTStatus::BT_RUNNING;
- status = _actStatus;
- if(actFunc) {
- actFunc([=](bool isSuccess){
- if(isSuccess) {
- _actStatus = behaviac::EBTStatus::BT_SUCCESS;
- } else {
- _actStatus = behaviac::EBTStatus::BT_FAILURE;
- }
- });
- }
- }else if(status == behaviac::EBTStatus::BT_SUCCESS || status == behaviac::EBTStatus::BT_FAILURE){
- _actStatus = behaviac::EBTStatus::BT_INVALID;
- _actKey = "";
- }
- return status;
-
- }
- bool RUBehaviacTree::isRunning()
- {
- return _isRunning;
- }
- NS_RU_END
|