This commit is contained in:
aozhiwei 2022-12-08 19:36:56 +08:00
parent 5cf7397ddf
commit 7ff312185a

View File

@ -79,12 +79,23 @@ behaviac::EBTStatus AndroidAgent::DoRandomWalk()
return behaviac::BT_FAILURE; return behaviac::BT_FAILURE;
} }
std::weak_ptr<EventHandlerPtr> handler = GetOwner()->GetTrigger()->AddListener
(
kAttacked,
[this] (const std::vector<std::any>& args)
{
Creature* c = std::any_cast<Creature*>(args.at(0));
FireEvent("OnAttacked", c->GetUniId(), c->room->GetFrameNo());
});
return StartCoroutine return StartCoroutine
( (
[this] () [this, handler] ()
{ {
if (GetOwner()->GetMoveHelper()->GetPathSize() <= 0) { if (GetOwner()->GetMoveHelper()->GetPathSize() <= 0) {
status_ = behaviac::BT_SUCCESS; if (!handler.expired()) {
GetOwner()->GetTrigger()->RemoveEventHandler(handler);
}
return behaviac::BT_SUCCESS; return behaviac::BT_SUCCESS;
} else { } else {
return behaviac::BT_RUNNING; return behaviac::BT_RUNNING;
@ -109,14 +120,24 @@ behaviac::EBTStatus AndroidAgent::DoRandomShot()
a8::Vec2 shot_dir = dir; a8::Vec2 shot_dir = dir;
GetOwner()->Shot(shot_dir, shot_ok, 0, 0); GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
std::weak_ptr<EventHandlerPtr> handler = GetOwner()->GetTrigger()->AddListener
(
kAttacked,
[this] (const std::vector<std::any>& args)
{
Creature* c = std::any_cast<Creature*>(args.at(0));
FireEvent("OnAttacked", c->GetUniId(), c->room->GetFrameNo());
});
long long last_frameno = GetOwner()->room->GetFrameNo(); long long last_frameno = GetOwner()->room->GetFrameNo();
return StartCoroutine return StartCoroutine
( (
[this, last_frameno] () [this, last_frameno, handler] ()
{ {
if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 3) { if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 3) {
status_ = behaviac::BT_SUCCESS; if (!handler.expired()) {
GetOwner()->GetTrigger()->RemoveEventHandler(handler);
}
return behaviac::BT_SUCCESS; return behaviac::BT_SUCCESS;
} else { } else {
bool shot_ok = false; bool shot_ok = false;