This commit is contained in:
aozhiwei 2021-03-30 14:05:06 +08:00
parent 97ad733576
commit 418d98deea
2 changed files with 15 additions and 14 deletions

View File

@ -136,7 +136,7 @@ void HeroAI::UpdateThinking()
} else { } else {
Human* target = GetTarget(); Human* target = GetTarget();
if (target) { if (target) {
node_.target = target; node_.target.Attach(target);
ChangeToStateAI(HSE_Attack); ChangeToStateAI(HSE_Attack);
} else { } else {
if (hero->room->GetFrameNo() >= node_.next_random_move_frameno) { if (hero->room->GetFrameNo() >= node_.next_random_move_frameno) {
@ -156,7 +156,7 @@ void HeroAI::UpdateThinking()
void HeroAI::UpdateAttack() void HeroAI::UpdateAttack()
{ {
Hero* myself = (Hero*)owner; Hero* myself = (Hero*)owner;
if (!node_.target || node_.target->dead) { if (!node_.target.Get() || node_.target.Get()->dead) {
ChangeToStateAI(HSE_Thinking); ChangeToStateAI(HSE_Thinking);
return; return;
} }
@ -164,7 +164,7 @@ void HeroAI::UpdateAttack()
ChangeToStateAI(HSE_Thinking); ChangeToStateAI(HSE_Thinking);
return; return;
} }
float distance = myself->GetPos().Distance(node_.target->GetPos()); float distance = myself->GetPos().Distance(node_.target.Get()->GetPos());
if (distance > GetAttackRange()) { if (distance > GetAttackRange()) {
if (ai_meta->i->pursuit_radius() <= 0) { if (ai_meta->i->pursuit_radius() <= 0) {
//站桩 //站桩
@ -222,7 +222,7 @@ void HeroAI::UpdateRandomWalk()
void HeroAI::UpdatePursuit() void HeroAI::UpdatePursuit()
{ {
Hero* myself = (Hero*)owner; Hero* myself = (Hero*)owner;
float distance = myself->GetPos().Distance(node_.target->GetPos()); float distance = myself->GetPos().Distance(node_.target.Get()->GetPos());
if (!myself->HasBuffEffect(kBET_Jump) && if (!myself->HasBuffEffect(kBET_Jump) &&
distance < GetAttackRange()) { distance < GetAttackRange()) {
ChangeToStateAI(HSE_Attack); ChangeToStateAI(HSE_Attack);
@ -255,7 +255,7 @@ void HeroAI::ChangeToStateAI(HeroState_e to_state)
switch (to_state) { switch (to_state) {
case HSE_Idle: case HSE_Idle:
{ {
node_.target = nullptr; node_.target.Reset();
node_.param1 = 0; node_.param1 = 0;
node_.start_shot_frameno = 0; node_.start_shot_frameno = 0;
node_.shot_times = 0; node_.shot_times = 0;
@ -271,7 +271,7 @@ void HeroAI::ChangeToStateAI(HeroState_e to_state)
break; break;
case HSE_Thinking: case HSE_Thinking:
{ {
node_.target = nullptr; node_.target.Reset();
node_.param1 = 0; node_.param1 = 0;
node_.start_shot_frameno = 0; node_.start_shot_frameno = 0;
node_.shot_times = 0; node_.shot_times = 0;
@ -290,7 +290,7 @@ void HeroAI::ChangeToStateAI(HeroState_e to_state)
case HSE_RandomWalk: case HSE_RandomWalk:
{ {
moving_ = true; moving_ = true;
node_.target = nullptr; node_.target.Reset();
#if 1 #if 1
node_.param1 = SERVER_FRAME_RATE * ai_meta->GetMoveTime(); node_.param1 = SERVER_FRAME_RATE * ai_meta->GetMoveTime();
#else #else
@ -314,8 +314,8 @@ void HeroAI::ChangeToStateAI(HeroState_e to_state)
case HSE_Pursuit: case HSE_Pursuit:
{ {
moving_ = true; moving_ = true;
if (node_.target) { if (node_.target.Get()) {
hero->move_dir = node_.target->GetPos() - hero->GetPos(); hero->move_dir = node_.target.Get()->GetPos() - hero->GetPos();
hero->move_dir.Normalize(); hero->move_dir.Normalize();
#if 0 #if 0
hero->attack_dir = hero->move_dir; hero->attack_dir = hero->move_dir;
@ -363,7 +363,7 @@ Human* HeroAI::GetTarget()
} }
}); });
if (target) { if (target) {
node_.nearest_human = target; node_.nearest_human.Attach(target);
node_.last_check_nearest_human_frameno = myself->room->GetFrameNo(); node_.last_check_nearest_human_frameno = myself->room->GetFrameNo();
float distance = myself->GetPos().Distance(target->GetPos()); float distance = myself->GetPos().Distance(target->GetPos());
if (distance > GetAttackRange()) { if (distance > GetAttackRange()) {
@ -389,14 +389,14 @@ float HeroAI::GetAttackRange()
void HeroAI::DoShotAI() void HeroAI::DoShotAI()
{ {
Hero* myself = (Hero*)owner; Hero* myself = (Hero*)owner;
if (!node_.target) { if (!node_.target.Get()) {
return; return;
} }
bool shot_ok = false; bool shot_ok = false;
a8::Vec2 shot_dir = myself->attack_dir; a8::Vec2 shot_dir = myself->attack_dir;
if (node_.total_shot_times >= node_.next_total_shot_times) { if (node_.total_shot_times >= node_.next_total_shot_times) {
shot_dir = node_.target->GetPos() - myself->GetPos(); shot_dir = node_.target.Get()->GetPos() - myself->GetPos();
node_.next_total_shot_times += 7 + (rand() % 6); node_.next_total_shot_times += 7 + (rand() % 6);
myself->attack_dir = shot_dir; myself->attack_dir = shot_dir;
} }

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "aicomponent.h" #include "aicomponent.h"
#include "weakptr.h"
namespace MetaData namespace MetaData
{ {
@ -30,8 +31,8 @@ public:
int next_total_shot_times = 0; int next_total_shot_times = 0;
long long param1 = 0; long long param1 = 0;
Human* target = nullptr; CreatureWeakPtr target;
Human* nearest_human = nullptr; CreatureWeakPtr nearest_human;
long long last_check_nearest_human_frameno = 0; long long last_check_nearest_human_frameno = 0;
a8::Vec2 shot_dir; a8::Vec2 shot_dir;
}; };