This commit is contained in:
aozhiwei 2023-06-21 20:08:08 +08:00
parent e883cd3b07
commit fe635a39e0
4 changed files with 157 additions and 156 deletions

View File

@ -58,30 +58,33 @@ behaviac::EBTStatus AndroidAgent::DoRandomWalk()
long long last_attacked_frameno = 0;
);
context->AddHandler(GetOwner()->GetTrigger()->AddListener
context->AddHandler
(
kAttacked,
[context_wp = SpToWp(context)] (const a8::Args& args)
{
if (!context_wp.expired()) {
auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0);
context->events.push_back
(
BtEvent::Create
GetOwner()->GetWeakPtrRef(),
GetOwner()->GetTrigger()->AddListener
(
kAttacked,
[context_wp = SpToWp(context)] (const a8::Args& args)
{
if (!context_wp.expired()) {
auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0);
context->events.push_back
(
kBetOnAttack,
a8::Args({
c->GetUniId(),
kBetOnAttack
}),
[c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable
{
return c_wp.Get() && !c_wp.Get()->dead;
})
);
}
}));
BtEvent::Create
(
kBetOnAttack,
a8::Args({
c->GetUniId(),
kBetOnAttack
}),
[c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable
{
return c_wp.Get() && !c_wp.Get()->dead;
})
);
}
}));
auto co = std::make_shared<BtCoroutine>(context, "CoRandomWalk");
co->runing_cb =
@ -122,30 +125,33 @@ behaviac::EBTStatus AndroidAgent::DoRandomShot()
);
context->last_frameno = GetOwner()->room->GetFrameNo();
context->AddHandler(GetOwner()->GetTrigger()->AddListener
context->AddHandler
(
kAttacked,
[context_wp = context->GetWp()] (const a8::Args& args)
{
if (!context_wp.expired()) {
auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0);
context->events.push_back
(
BtEvent::Create
GetOwner()->GetWeakPtrRef(),
GetOwner()->GetTrigger()->AddListener
(
kAttacked,
[context_wp = context->GetWp()] (const a8::Args& args)
{
if (!context_wp.expired()) {
auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0);
context->events.push_back
(
kBetOnAttack,
a8::Args({
c->GetUniId(),
kBetOnAttack
}),
[c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable
{
return c_wp.Get() && !c_wp.Get()->dead;
})
);
}
}));
BtEvent::Create
(
kBetOnAttack,
a8::Args({
c->GetUniId(),
kBetOnAttack
}),
[c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable
{
return c_wp.Get() && !c_wp.Get()->dead;
})
);
}
}));
auto co = std::make_shared<BtCoroutine>(context, "CoRandomShot");
co->runing_cb =

View File

@ -22,16 +22,16 @@ static auto SpToWp(std::shared_ptr<T> sp)
return std::weak_ptr<T>(sp);
}
void BtContext::AddHandler(std::weak_ptr<EventHandlerPtr> handler)
void BtContext::AddHandler(CreatureWeakPtr target, std::weak_ptr<EventHandlerPtr> handler)
{
handlers.push_back(handler);
handlers.push_back(std::make_tuple(target, handler));
}
BtContext::~BtContext()
{
if (owner.Get()) {
for (auto& handler : handlers) {
owner.Get()->GetTrigger()->RemoveEventHandler(handler);
for (auto& tuple : handlers) {
if (std::get<0>(tuple).Get()) {
std::get<0>(tuple).Get()->GetTrigger()->RemoveEventHandler(std::get<1>(tuple));
}
}
}
@ -323,30 +323,33 @@ behaviac::EBTStatus BaseAgent::DoIdle(int min_time, int max_time)
{
},
&GetOwner()->xtimer_attacher);
context->AddHandler(GetOwner()->GetTrigger()->AddListener
context->AddHandler
(
kAttacked,
[context_wp = SpToWp(context)] (const a8::Args& args)
{
if (!context_wp.expired()) {
auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0);
context->events.push_back
(
BtEvent::Create
GetOwner()->GetWeakPtrRef(),
GetOwner()->GetTrigger()->AddListener
(
kAttacked,
[context_wp = SpToWp(context)] (const a8::Args& args)
{
if (!context_wp.expired()) {
auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0);
context->events.push_back
(
kBetOnAttack,
a8::Args({
c->GetUniId(),
kBetOnAttack
}),
[c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable
{
return c_wp.Get() && !c_wp.Get()->dead;
})
);
}
}));
BtEvent::Create
(
kBetOnAttack,
a8::Args({
c->GetUniId(),
kBetOnAttack
}),
[c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable
{
return c_wp.Get() && !c_wp.Get()->dead;
})
);
}
}));
auto co = std::make_shared<BtCoroutine>(context, "CoIdle");
co->runing_cb =

View File

@ -14,10 +14,10 @@ public:
CreatureWeakPtr owner;
std::vector<std::shared_ptr<BtEvent>> events;
std::vector<std::weak_ptr<EventHandlerPtr>> handlers;
std::vector<std::tuple<CreatureWeakPtr, std::weak_ptr<EventHandlerPtr>>> handlers;
virtual ~BtContext();
void AddHandler(std::weak_ptr<EventHandlerPtr> handler);
void AddHandler(CreatureWeakPtr target, std::weak_ptr<EventHandlerPtr> handler);
std::weak_ptr<BtContext> GetWp() { return shared_from_this();};
private:

View File

@ -49,30 +49,33 @@ behaviac::EBTStatus HeroAgent::DoRandomWalk()
(
);
context->AddHandler(GetOwner()->GetTrigger()->AddListener
context->AddHandler
(
kAttacked,
[context_wp = context->GetWp()] (const a8::Args& args)
{
if (!context_wp.expired()) {
auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0);
context->events.push_back
(
BtEvent::Create
GetOwner()->GetWeakPtrRef(),
GetOwner()->GetTrigger()->AddListener
(
kAttacked,
[context_wp = context->GetWp()] (const a8::Args& args)
{
if (!context_wp.expired()) {
auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0);
context->events.push_back
(
kBetOnAttack,
a8::Args({
c->GetUniId(),
kBetOnAttack
}),
[c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable
{
return c_wp.Get() && !c_wp.Get()->dead;
})
);
}
}));
BtEvent::Create
(
kBetOnAttack,
a8::Args({
c->GetUniId(),
kBetOnAttack
}),
[c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable
{
return c_wp.Get() && !c_wp.Get()->dead;
})
);
}
}));
auto co = std::make_shared<BtCoroutine>(context, "CoRandomWalk");
co->runing_cb =
@ -109,30 +112,32 @@ behaviac::EBTStatus HeroAgent::DoRandomShot()
);
context->last_frameno = GetOwner()->room->GetFrameNo();
context->AddHandler(GetOwner()->GetTrigger()->AddListener
(
kAttacked,
[context_wp = context->GetWp()] (const a8::Args& args)
{
if (!context_wp.expired()) {
auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0);
context->events.push_back
(
BtEvent::Create
context->AddHandler
(GetOwner()->GetWeakPtrRef(),
GetOwner()->GetTrigger()->AddListener
(
kAttacked,
[context_wp = context->GetWp()] (const a8::Args& args)
{
if (!context_wp.expired()) {
auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0);
context->events.push_back
(
kBetOnAttack,
a8::Args({
c->GetUniId(),
kBetOnAttack
}),
[c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable
{
return c_wp.Get() && !c_wp.Get()->dead;
})
);
}
}));
BtEvent::Create
(
kBetOnAttack,
a8::Args({
c->GetUniId(),
kBetOnAttack
}),
[c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable
{
return c_wp.Get() && !c_wp.Get()->dead;
})
);
}
}));
auto co = std::make_shared<BtCoroutine>(context, "CoRandomShot");
co->runing_cb =
@ -189,46 +194,33 @@ behaviac::EBTStatus HeroAgent::DoAttack()
context->last_frameno = GetOwner()->room->GetFrameNo();
if (GetOwner()->AsHero()->master.Get()) {
context->AddHandler(GetOwner()->AsHero()->master.Get()->GetTrigger()->AddListener
(
kBulletHitEvent,
[context_wp = context->GetWp()] (const a8::Args& args)
{
if (!context_wp.expired()) {
auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0);
context->events.push_back
(
BtEvent::Create
context->AddHandler
(GetOwner()->AsHero()->master,
GetOwner()->AsHero()->master.Get()->GetTrigger()->AddListener
(
kBulletHitEvent,
[context_wp = context->GetWp()] (const a8::Args& args)
{
if (!context_wp.expired()) {
auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0);
context->events.push_back
(
kBetOnAttack,
a8::Args({
c->GetUniId(),
kBetOnAttack
}),
[c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable
{
return c_wp.Get() && !c_wp.Get()->dead;
})
);
}
}));
BtEvent::Create
(
kBetOnAttack,
a8::Args({
c->GetUniId(),
kBetOnAttack
}),
[c_wp = c->GetWeakPtrRef(), frameno = c->room->GetFrameNo()] () mutable
{
return c_wp.Get() && !c_wp.Get()->dead;
})
);
}
}));
}
#if 0
context->_destory_cb =
(
[context = context.get()] ()
{
if (!context->handler.expired() && context->owner.Get()) {
if (context->owner.Get()->IsEntityType(ET_Hero)) {
Hero* hero = (Hero*)context->owner.Get();
if (hero->master.Get()) {
hero->master.Get()->GetTrigger()->RemoveEventHandler(context->handler);
}
}
}
});
#endif
auto co = std::make_shared<BtCoroutine>(context, "CoAttack");
co->runing_cb =