This commit is contained in:
aozhiwei 2023-04-05 14:08:18 +08:00
parent 8cca25ad24
commit df7794d882

View File

@ -9,6 +9,7 @@
#include "trigger.h"
#include "glmhelper.h"
#include "mapinstance.h"
#include "human.h"
#include "mt/Map.h"
@ -387,27 +388,60 @@ behaviac::EBTStatus HeroAgent::DoPursuit()
if (status_ == behaviac::BT_RUNNING) {
return DoRunningCb();
}
Creature* enemy = GetOwner()->room->FindEnemy(GetOwner(), 500);
Human* enemy = GetOwner()->room->FindEnemy(GetOwner()->AsHuman(), 500);
if (!enemy) {
return behaviac::BT_FAILURE;
}
CreatureWeakPtr target = enemy->GetWeakPtrRef();
long long last_frameno = GetOwner()->room->GetFrameNo();
long long last_pursuit_frameno = GetOwner()->room->GetFrameNo();
return StartCoroutine
auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED
(
[this, last_frameno, target, last_pursuit_frameno] () mutable
CreatureWeakPtr owner;
CreatureWeakPtr target;
CreatureWeakPtr last_attacker;
long long last_attacked_frameno = 0;
long long last_frameno = 0;
long long last_pursuit_frameno = 0;
std::weak_ptr<EventHandlerPtr> handler;
);
context->owner = GetOwner()->GetWeakPtrRef();
context->target = enemy->GetWeakPtrRef();
context->last_frameno = GetOwner()->room->GetFrameNo();
context->last_pursuit_frameno = GetOwner()->room->GetFrameNo();
context->handler = GetOwner()->GetTrigger()->AddListener
(
kAttacked,
[context_wp = context->GetWp()] (const a8::Args& args)
{
if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 10 ||
!target.Get() || target.Get()->dead) {
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, "CoPursuit");
co->runing_cb =
[this, context] ()
{
if (GetOwner()->room->GetFrameNo() - context->last_frameno > SERVER_FRAME_RATE * 10 ||
!context->target.Get() || context->target.Get()->dead) {
status_ = behaviac::BT_SUCCESS;
return behaviac::BT_SUCCESS;
} else {
glm::vec3 dir = GetOwner()->GetPos().CalcDir(target.Get()->GetPos());
glm::vec3 dir = GetOwner()->GetPos().CalcDir(context->target.Get()->GetPos());
if (GlmHelper::Norm(dir) <= 1.0f) {
GetOwner()->GetMovement()->CalcTargetPos(60);
last_pursuit_frameno = GetOwner()->room->GetFrameNo();
context->last_pursuit_frameno = GetOwner()->room->GetFrameNo();
} else {
bool is_shot = false;
if (GlmHelper::Norm(dir) > 300) {
@ -416,14 +450,14 @@ behaviac::EBTStatus HeroAgent::DoPursuit()
GetOwner()->SetMoveDir(dir);
GetOwner()->SetAttackDir(dir);
GetOwner()->GetMovement()->CalcTargetPos(200);
last_pursuit_frameno = GetOwner()->room->GetFrameNo();
context->last_pursuit_frameno = GetOwner()->room->GetFrameNo();
} else {
if (GetOwner()->room->GetFrameNo() - last_pursuit_frameno > SERVER_FRAME_RATE * 1) {
if (GetOwner()->room->GetFrameNo() - context->last_pursuit_frameno > SERVER_FRAME_RATE * 1) {
GlmHelper::Normalize(dir);
GetOwner()->SetMoveDir(dir);
GetOwner()->SetAttackDir(dir);
GetOwner()->GetMovement()->CalcTargetPos(200);
last_pursuit_frameno = GetOwner()->room->GetFrameNo();
context->last_pursuit_frameno = GetOwner()->room->GetFrameNo();
}
}
} else {
@ -434,22 +468,18 @@ behaviac::EBTStatus HeroAgent::DoPursuit()
bool shot_ok = false;
glm::vec3 shot_dir = dir;
GetOwner()->SetAttackDir(dir);
if (bullet_trace_mode_) {
GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
} else {
GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
}
}
}
return behaviac::BT_RUNNING;
}
},
[this] (bool is_test, bool& has_event)
};
co->event_cb =
[this, context]
(bool is_test, bool& has_event) mutable
{
},
"CoPursuit"
);
};
return StartCoroutine(co);
}
behaviac::EBTStatus HeroAgent::DoHelpAttack(int target_uniid)
@ -483,49 +513,56 @@ behaviac::EBTStatus HeroAgent::DoHelpAttack(int target_uniid)
GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
}
CreatureWeakPtr target = enemy->GetWeakPtrRef();
long long last_frameno = GetOwner()->room->GetFrameNo();
return StartCoroutine
auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED
(
[this, last_frameno, target] () mutable
CreatureWeakPtr target;
long long last_frameno = 0;
);
context->target = enemy->GetWeakPtrRef();
context->last_frameno = GetOwner()->room->GetFrameNo();
auto co = std::make_shared<BtCoroutine>(context, "CoHelpAttack");
co->runing_cb =
[this, context] ()
{
if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 3) {
if (GetOwner()->room->GetFrameNo() - context->last_frameno > SERVER_FRAME_RATE * 3) {
status_ = behaviac::BT_SUCCESS;
return behaviac::BT_SUCCESS;
} else {
if (!target.Get()) {
if (!context->target.Get()) {
return behaviac::BT_SUCCESS;
}
if (target.Get()->dead) {
if (context->target.Get()->dead) {
return behaviac::BT_SUCCESS;
}
if (GlmHelper::IsEqual2D(GetOwner()->GetPos().ToGlmVec3(),
target.Get()->GetPos().ToGlmVec3())) {
context->target.Get()->GetPos().ToGlmVec3())) {
return behaviac::BT_FAILURE;
}
float distance = target.Get()->GetPos().Distance2D2(GetOwner()->GetPos());
float distance = context->target.Get()->GetPos().Distance2D2(GetOwner()->GetPos());
if (distance > GetAttackRange()) {
return behaviac::BT_FAILURE;
}
bool shot_ok = false;
glm::vec3 shot_dir = GetOwner()->GetPos().CalcDir(target.Get()->GetPos());;
glm::vec3 shot_dir = GetOwner()->GetPos().CalcDir(context->target.Get()->GetPos());;
GlmHelper::Normalize(shot_dir);
GetOwner()->SetAttackDir(shot_dir);
if (bullet_trace_mode_) {
GetOwner()->Shot(shot_dir, shot_ok, 0, target.Get()->GetUniId());
GetOwner()->Shot(shot_dir, shot_ok, 0, context->target.Get()->GetUniId());
} else {
GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
}
return behaviac::BT_RUNNING;
}
},
[this] (bool is_test, bool& has_event)
};
co->event_cb =
[this, context]
(bool is_test, bool& has_event) mutable
{
},
"CoHelpAttack"
);
};
return StartCoroutine(co);
}
behaviac::EBTStatus HeroAgent::DoFlyToMasterAround()
@ -613,13 +650,20 @@ behaviac::EBTStatus HeroAgent::DoFollowMaster()
return behaviac::BT_FAILURE;
}
long long last_frameno = GetOwner()->room->GetFrameNo();
long long last_follow_frameno = GetOwner()->room->GetFrameNo();
return StartCoroutine
auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED
(
[this, last_frameno, last_follow_frameno] () mutable
long long last_frameno = 0;
long long last_follow_frameno = 0;
);
context->last_frameno = GetOwner()->room->GetFrameNo();
context->last_follow_frameno = GetOwner()->room->GetFrameNo();
auto co = std::make_shared<BtCoroutine>(context, "CoFollowMaster");
co->runing_cb =
[this, context] ()
{
if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 10 ||
if (GetOwner()->room->GetFrameNo() - context->last_frameno > SERVER_FRAME_RATE * 10 ||
!GetOwner()->AsHero()->master.Get() || GetOwner()->AsHero()->master.Get()->dead) {
status_ = behaviac::BT_SUCCESS;
return behaviac::BT_SUCCESS;
@ -637,17 +681,17 @@ behaviac::EBTStatus HeroAgent::DoFollowMaster()
GetOwner()->SetMoveDir(dir);
GetOwner()->SetAttackDir(dir);
GetOwner()->GetMovement()->CalcTargetPos(60);
last_follow_frameno = GetOwner()->room->GetFrameNo();
context->last_follow_frameno = GetOwner()->room->GetFrameNo();
}
return behaviac::BT_RUNNING;
}
},
[this] (bool is_test, bool& has_event)
};
co->event_cb =
[this, context]
(bool is_test, bool& has_event) mutable
{
},
"CoFollowMaster"
);
};
return StartCoroutine(co);
}
float HeroAgent::GetMasterDistance()