This commit is contained in:
aozhiwei 2023-06-23 19:43:29 +08:00
parent 07e56b7e59
commit 884c978019
4 changed files with 8 additions and 8 deletions

View File

@ -7,8 +7,8 @@
#define MAKE_BTCONTEXT(...) \ #define MAKE_BTCONTEXT(...) \
[] (CreatureWeakPtr owner, BaseAgent* agent) \ [] (CreatureWeakPtr owner, BaseAgent* agent) \
{ \ { \
struct Context : public BtContext \ class Context : public BtContext \
{ \ {public: \
__VA_ARGS__; \ __VA_ARGS__; \
std::function<void()> _destory_cb; \ std::function<void()> _destory_cb; \
~Context() { if (_destory_cb) { _destory_cb(); };}; \ ~Context() { if (_destory_cb) { _destory_cb(); };}; \

View File

@ -6,12 +6,12 @@
void BtContext::AddHandler(CreatureWeakPtr target, std::weak_ptr<EventHandlerPtr> handler) void BtContext::AddHandler(CreatureWeakPtr target, std::weak_ptr<EventHandlerPtr> handler)
{ {
handlers.push_back(std::make_tuple(target, handler)); handlers_.push_back(std::make_tuple(target, handler));
} }
BtContext::~BtContext() BtContext::~BtContext()
{ {
for (auto& tuple : handlers) { for (auto& tuple : handlers_) {
if (std::get<0>(tuple).Get() && !std::get<1>(tuple).expired()) { if (std::get<0>(tuple).Get() && !std::get<1>(tuple).expired()) {
std::get<0>(tuple).Get()->GetTrigger()->RemoveEventHandler(std::get<1>(tuple)); std::get<0>(tuple).Get()->GetTrigger()->RemoveEventHandler(std::get<1>(tuple));
} }

View File

@ -3,16 +3,16 @@
class BaseAgent; class BaseAgent;
class BtEvent; class BtEvent;
struct EventHandlerPtr; struct EventHandlerPtr;
struct BtContext : public std::enable_shared_from_this<BtContext> class BtContext : public std::enable_shared_from_this<BtContext>
{ {
public: public:
CreatureWeakPtr owner; CreatureWeakPtr owner;
std::vector<std::shared_ptr<BtEvent>> events; std::vector<std::shared_ptr<BtEvent>> events;
std::vector<std::tuple<CreatureWeakPtr, std::weak_ptr<EventHandlerPtr>>> handlers;
virtual ~BtContext(); virtual ~BtContext();
void AddHandler(CreatureWeakPtr target, std::weak_ptr<EventHandlerPtr> handler); void AddHandler(CreatureWeakPtr target, std::weak_ptr<EventHandlerPtr> handler);
private:
private:
std::vector<std::tuple<CreatureWeakPtr, std::weak_ptr<EventHandlerPtr>>> handlers_;
}; };

View File

@ -3,7 +3,7 @@
#include "behaviac/behaviac.h" #include "behaviac/behaviac.h"
#include "behaviac_customized_types.h" #include "behaviac_customized_types.h"
struct BtContext; class BtContext;
class BaseAgent; class BaseAgent;
class BtCoroutine class BtCoroutine
{ {