This commit is contained in:
aozhiwei 2022-12-08 19:28:18 +08:00
parent eb291bdc54
commit 5cf7397ddf
3 changed files with 142 additions and 107 deletions

View File

@ -4,6 +4,7 @@
#include "android.h"
#include "room.h"
#include "movehelper.h"
#include "trigger.h"
AndroidAgent::AndroidAgent():BaseAgent()
{
@ -24,6 +25,7 @@ behaviac::EBTStatus AndroidAgent::DoIdle(int min_time, int max_time)
if (status_ == behaviac::BT_RUNNING) {
return DoRunningCb();
}
int idle_time = a8::RandEx(min_time, max_time);
xtimer_list* timer = GetOwner()->room->xtimer.AddDeadLineTimerAndAttach
(
@ -34,18 +36,31 @@ behaviac::EBTStatus AndroidAgent::DoIdle(int min_time, int max_time)
},
&GetOwner()->xtimer_attacher.timer_list_);
std::weak_ptr<a8::XTimerPtr> timer_ptr = GetOwner()->room->xtimer.GetTimerPtr(timer);
status_runing_cb_ =
[this, timer_ptr] ()
std::weak_ptr<EventHandlerPtr> handler = GetOwner()->GetTrigger()->AddListener
(
kAttacked,
[this] (const std::vector<std::any>& args)
{
if (timer_ptr.lock()) {
Creature* c = std::any_cast<Creature*>(args.at(0));
FireEvent("OnAttacked", c->GetUniId(), c->room->GetFrameNo());
});
return StartCoroutine
(
[this, timer_ptr, handler] ()
{
if (!timer_ptr.expired()) {
return behaviac::BT_RUNNING;
} else {
status_ = behaviac::BT_SUCCESS;
if (!handler.expired()) {
GetOwner()->GetTrigger()->RemoveEventHandler(handler);
}
return behaviac::BT_SUCCESS;
}
};
status_ = behaviac::BT_RUNNING;
return status_;
},
"CoIdle"
);
}
behaviac::EBTStatus AndroidAgent::DoRandomWalk()
@ -63,7 +78,9 @@ behaviac::EBTStatus AndroidAgent::DoRandomWalk()
if (GetOwner()->GetMoveHelper()->GetPathSize() <= 0) {
return behaviac::BT_FAILURE;
}
status_runing_cb_ =
return StartCoroutine
(
[this] ()
{
if (GetOwner()->GetMoveHelper()->GetPathSize() <= 0) {
@ -72,16 +89,17 @@ behaviac::EBTStatus AndroidAgent::DoRandomWalk()
} else {
return behaviac::BT_RUNNING;
}
};
status_ = behaviac::BT_RUNNING;
return status_;
},
"CoRandomWalk"
);
}
behaviac::EBTStatus AndroidAgent::DoRandomShot()
{
if (status_ == behaviac::BT_RUNNING) {
return status_runing_cb_();
return DoRunningCb();
}
a8::Vec2 dir = GetOwner()->GetMoveDir();
dir.Rotate((10 + rand() % 360)/ 180.0f);
dir.Normalize();
@ -91,8 +109,10 @@ behaviac::EBTStatus AndroidAgent::DoRandomShot()
a8::Vec2 shot_dir = dir;
GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
long long last_frameno = GetOwner()->room->GetFrameNo();
status_runing_cb_ =
return StartCoroutine
(
[this, last_frameno] ()
{
if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 3) {
@ -105,10 +125,9 @@ behaviac::EBTStatus AndroidAgent::DoRandomShot()
return behaviac::BT_RUNNING;
}
};
status_ = behaviac::BT_RUNNING;
return status_;
},
"CoRandomShot"
);
}
behaviac::EBTStatus AndroidAgent::DoAttack()
@ -130,7 +149,8 @@ behaviac::EBTStatus AndroidAgent::DoAttack()
GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
long long last_frameno = GetOwner()->room->GetFrameNo();
status_runing_cb_ =
return StartCoroutine
(
[this, last_frameno] ()
{
if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 3) {
@ -143,10 +163,9 @@ behaviac::EBTStatus AndroidAgent::DoAttack()
return behaviac::BT_RUNNING;
}
};
status_ = behaviac::BT_RUNNING;
return status_;
},
"CoAttack"
);
}
behaviac::EBTStatus AndroidAgent::DoPursuit()
@ -162,7 +181,8 @@ behaviac::EBTStatus AndroidAgent::DoPursuit()
CreatureWeakPtr target = enemy->GetWeakPtrRef();
long long last_frameno = GetOwner()->room->GetFrameNo();
long long last_pursuit_frameno = GetOwner()->room->GetFrameNo();
status_runing_cb_ =
return StartCoroutine
(
[this, last_frameno, target, last_pursuit_frameno] () mutable
{
if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 10 ||
@ -205,8 +225,7 @@ behaviac::EBTStatus AndroidAgent::DoPursuit()
}
return behaviac::BT_RUNNING;
}
};
status_ = behaviac::BT_RUNNING;
return status_;
},
"CoPursuit"
);
}

View File

@ -32,14 +32,24 @@ behaviac::EBTStatus BaseAgent::DoRunningCb()
abort();
}
status_ = status_runing_cb_();
#ifdef DEBUG
if ((GetOwner()->room->GetFrameNo() - status_frameno_) % SERVER_FRAME_RATE == 0) {
a8::XPrintf("Running Status:%s %d\n", {status_name_, status_});
}
#endif
if (status_ != behaviac::BT_RUNNING) {
status_runing_cb_ = nullptr;
}
return status_;
}
behaviac::EBTStatus BaseAgent::StartCoroutine(std::function<behaviac::EBTStatus()> cb)
behaviac::EBTStatus BaseAgent::StartCoroutine(std::function<behaviac::EBTStatus()> cb,
const char* name)
{
#ifdef DEBUG
status_frameno_ = GetOwner()->room->GetFrameNo();
status_name_ = name;
#endif
status_runing_cb_ = std::move(cb);
status_ = behaviac::BT_RUNNING;
return status_;
@ -104,6 +114,7 @@ behaviac::EBTStatus BaseAgent::CoAttackTarget(int target_id)
}
}
return behaviac::BT_RUNNING;
}
},
"CoAttackTarget"
);
}

View File

@ -24,9 +24,14 @@ public:
protected:
behaviac::EBTStatus DoRunningCb();
behaviac::EBTStatus StartCoroutine(std::function<behaviac::EBTStatus()> cb);
behaviac::EBTStatus StartCoroutine(std::function<behaviac::EBTStatus()> cb,
const char* name);
protected:
#ifdef DEBUG
long long status_frameno_ = 0;
const char* status_name_ = nullptr;
#endif
behaviac::EBTStatus status_= behaviac::BT_SUCCESS;
std::function<behaviac::EBTStatus()> status_runing_cb_;