This commit is contained in:
aozhiwei 2023-04-05 14:12:44 +08:00
parent df7794d882
commit 18a6cb7db8
6 changed files with 69 additions and 136 deletions

View File

@ -24,73 +24,6 @@ State_e AndroidAgent::GetState()
return kPreBattle;
}
behaviac::EBTStatus AndroidAgent::DoIdle(int min_time, int max_time)
{
if (status_ == behaviac::BT_RUNNING) {
return DoRunningCb();
}
auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED
(
CreatureWeakPtr owner;
a8::XTimerWp timer_ptr;
CreatureWeakPtr last_attacker;
long long last_attacked_frameno = 0;
std::weak_ptr<EventHandlerPtr> handler;
);
context->owner = GetOwner()->GetWeakPtrRef();
context->timer_ptr = GetOwner()->room->xtimer.SetTimeoutWpEx
(
a8::RandEx(min_time, max_time) / FRAME_RATE_MS,
[] (int event, const a8::Args* args)
{
},
&GetOwner()->xtimer_attacher);
context->handler = 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->last_attacker = c->GetWeakPtrRef();
context->last_attacked_frameno = c->room->GetFrameNo();
}
});
context->_destory_cb =
(
[context = context.get()] ()
{
if (context->owner.Get()) {
context->owner.Get()->GetTrigger()->RemoveEventHandler(context->handler);
}
});
auto co = std::make_shared<BtCoroutine>(context, "CoIdle");
co->runing_cb =
[this, context] ()
{
if (!context->timer_ptr.expired()) {
return behaviac::BT_RUNNING;
} else {
return behaviac::BT_SUCCESS;
}
};
co->event_cb =
[this, context]
(bool is_test, bool& has_event) mutable
{
has_event = context->last_attacker.Get() && !context->last_attacker.Get()->dead ? true : false;
if (!is_test && has_event) {
FireEvent("OnAttacked",
context->last_attacker.Get()->GetUniId(),
context->last_attacked_frameno);
}
};
return StartCoroutine(co);
}
behaviac::EBTStatus AndroidAgent::DoRandomWalk()
{
if (status_ == behaviac::BT_RUNNING) {

View File

@ -14,7 +14,6 @@ public:
State_e GetState();
behaviac::EBTStatus DoIdle(int min_time, int max_time);
behaviac::EBTStatus DoRandomWalk();
behaviac::EBTStatus DoRandomShot();
behaviac::EBTStatus DoAttack();

View File

@ -6,6 +6,7 @@
#include "human.h"
#include "movement.h"
#include "glmhelper.h"
#include "trigger.h"
#include "f8/btmgr.h"
@ -207,3 +208,70 @@ void BaseAgent::SetBulletTraceMode(bool mode)
{
bullet_trace_mode_ = mode;
}
behaviac::EBTStatus BaseAgent::DoIdle(int min_time, int max_time)
{
if (status_ == behaviac::BT_RUNNING) {
return DoRunningCb();
}
auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED
(
CreatureWeakPtr owner;
a8::XTimerWp timer_ptr;
CreatureWeakPtr last_attacker;
long long last_attacked_frameno = 0;
std::weak_ptr<EventHandlerPtr> handler;
);
context->owner = GetOwner()->GetWeakPtrRef();
context->timer_ptr = GetOwner()->room->xtimer.SetTimeoutWpEx
(
a8::RandEx(min_time, max_time) / FRAME_RATE_MS,
[] (int event, const a8::Args* args)
{
},
&GetOwner()->xtimer_attacher);
context->handler = 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->last_attacker = c->GetWeakPtrRef();
context->last_attacked_frameno = c->room->GetFrameNo();
}
});
context->_destory_cb =
(
[context = context.get()] ()
{
if (context->owner.Get()) {
context->owner.Get()->GetTrigger()->RemoveEventHandler(context->handler);
}
});
auto co = std::make_shared<BtCoroutine>(context, "CoIdle");
co->runing_cb =
[this, context] ()
{
if (!context->timer_ptr.expired()) {
return behaviac::BT_RUNNING;
} else {
return behaviac::BT_SUCCESS;
}
};
co->event_cb =
[this, context]
(bool is_test, bool& has_event) mutable
{
has_event = context->last_attacker.Get() && !context->last_attacker.Get()->dead ? true : false;
if (!is_test && has_event) {
FireEvent("OnAttacked",
context->last_attacker.Get()->GetUniId(),
context->last_attacked_frameno);
}
};
return StartCoroutine(co);
}

View File

@ -38,6 +38,7 @@ public:
void SetBulletTraceMode(bool mode);
behaviac::EBTStatus CoAttackTarget(int target_id);
behaviac::EBTStatus DoIdle(int min_time, int max_time);
public:
void SetOwner(Creature* owner) { owner_ = owner; };

View File

@ -27,73 +27,6 @@ State_e HeroAgent::GetState()
return kPreBattle;
}
behaviac::EBTStatus HeroAgent::DoIdle(int min_time, int max_time)
{
if (status_ == behaviac::BT_RUNNING) {
return DoRunningCb();
}
auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED
(
CreatureWeakPtr owner;
a8::XTimerWp timer_ptr;
CreatureWeakPtr last_attacker;
long long last_attacked_frameno = 0;
std::weak_ptr<EventHandlerPtr> handler;
);
context->owner = GetOwner()->GetWeakPtrRef();
context->timer_ptr = GetOwner()->room->xtimer.SetTimeoutWpEx
(
a8::RandEx(min_time, max_time) / FRAME_RATE_MS,
[] (int event, const a8::Args* args)
{
},
&GetOwner()->xtimer_attacher);
context->handler = 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->last_attacker = c->GetWeakPtrRef();
context->last_attacked_frameno = c->room->GetFrameNo();
}
});
context->_destory_cb =
(
[context = context.get()] ()
{
if (context->owner.Get()) {
context->owner.Get()->GetTrigger()->RemoveEventHandler(context->handler);
}
});
auto co = std::make_shared<BtCoroutine>(context, "CoIdle");
co->runing_cb =
[this, context] ()
{
if (!context->timer_ptr.expired()) {
return behaviac::BT_RUNNING;
} else {
return behaviac::BT_SUCCESS;
}
};
co->event_cb =
[this, context]
(bool is_test, bool& has_event) mutable
{
has_event = context->last_attacker.Get() && !context->last_attacker.Get()->dead ? true : false;
if (!is_test && has_event) {
FireEvent("OnAttacked",
context->last_attacker.Get()->GetUniId(),
context->last_attacked_frameno);
}
};
return StartCoroutine(co);
}
behaviac::EBTStatus HeroAgent::DoRandomWalk()
{
if (status_ == behaviac::BT_RUNNING) {

View File

@ -14,7 +14,6 @@ public:
State_e GetState();
behaviac::EBTStatus DoIdle(int min_time, int max_time);
behaviac::EBTStatus DoRandomWalk();
behaviac::EBTStatus DoRandomShot();
behaviac::EBTStatus DoAttack();