机器人跟随实现

This commit is contained in:
aozhiwei 2021-04-20 19:17:39 +08:00
parent a553d6b0d8
commit dda953c6a2
5 changed files with 53 additions and 22 deletions

View File

@ -8,8 +8,6 @@
#include "metamgr.h"
#include "player.h"
const int SHUA_RANGE = 580;
enum ShotType_e
{
kShotNone = 0,
@ -17,20 +15,6 @@ enum ShotType_e
kShotHold = 2,
};
/*
nn目标ai可切换/()
ai级别
1: ()
2:
3:
4: ()
5:
6:
7:
8:
*/
HeroAI::~HeroAI()
{
}
@ -96,6 +80,11 @@ void HeroAI::UpdateAI()
UpdatePursuit();
}
break;
case HSE_FollowMaster:
{
UpdateFollowMaster();
}
break;
default:
{
abort();
@ -128,6 +117,12 @@ void HeroAI::UpdateThinking()
ChangeToStateAI(HSE_RandomWalk);
}
} else {
if (hero->HasBuffEffect(kBET_FollowMaster) &&
hero->master.Get() &&
hero->master.Get()->GetPos().ManhattanDistance(hero->GetPos()) > 200) {
ChangeToStateAI(HSE_FollowMaster);
return;
}
Creature* target = GetTarget();
if (target) {
node_.target.Attach(target);
@ -227,19 +222,35 @@ void HeroAI::UpdatePursuit()
}
}
void HeroAI::UpdateFollowMaster()
{
Hero* myself = (Hero*)owner;
if (myself->master.Get()) {
float mdistance = myself->GetPos().ManhattanDistance(node_.target_pos);
if (mdistance < 10) {
ChangeToStateAI(HSE_Thinking);
}
}
}
void HeroAI::DoMoveAI()
{
Hero* hero = (Hero*)owner;
if (std::abs(hero->GetMoveDir().x) > FLT_EPSILON ||
std::abs(hero->GetMoveDir().y) > FLT_EPSILON) {
auto old_on_move_collision_func = hero->on_move_collision;
hero->on_move_collision =
[this] () {
ChangeToStateAI(HSE_RandomWalk);
if (node_.main_state == HSE_FollowMaster) {
ChangeToStateAI(HSE_FollowMaster);
} else {
ChangeToStateAI(HSE_RandomWalk);
}
return false;
};
int speed = std::max(1, (int)hero->GetSpeed()) * 1;
hero->_UpdateMove(speed);
hero->on_move_collision = nullptr;
hero->on_move_collision = old_on_move_collision_func;
}
}
@ -315,6 +326,22 @@ void HeroAI::ChangeToStateAI(HeroState_e to_state)
}
}
break;
case HSE_FollowMaster:
{
moving_ = true;
if (hero->master.Get()) {
a8::Vec2 target_pos = hero->master.Get()->GetPos();
a8::Vec2 target_dir = a8::Vec2::UP;
target_dir.Rotate(a8::RandAngle());
target_pos = target_pos + target_dir * 80;
a8::Vec2 move_dir = target_pos - hero->GetPos();
move_dir.Normalize();
hero->SetMoveDir(move_dir);
hero->attack_dir = hero->GetMoveDir();
node_.target_pos = target_pos;
}
}
break;
}
node_.main_state = to_state;
node_.frameno = hero->room->GetFrameNo();

View File

@ -14,7 +14,8 @@ enum HeroState_e
HSE_Thinking = 1,
HSE_Attack = 2,
HSE_RandomWalk = 3,
HSE_Pursuit = 4
HSE_Pursuit = 4,
HSE_FollowMaster = 5
};
class Human;
@ -35,6 +36,7 @@ public:
CreatureWeakPtr nearest_human;
long long last_check_nearest_human_frameno = 0;
a8::Vec2 shot_dir;
a8::Vec2 target_pos;
};
class HeroAI : public AIComponent
@ -52,6 +54,7 @@ private:
void UpdateAttack();
void UpdateRandomWalk();
void UpdatePursuit();
void UpdateFollowMaster();
void DoMoveAI();
void ChangeToStateAI(HeroState_e to_state);
void DoShotAI();

View File

@ -47,7 +47,7 @@ void Hero::FillMFObjectFull(Room* room, Human* hum, cs::MFObjectFull* full_data)
TypeConvert::ToPb(GetPos(), p->mutable_pos());
TypeConvert::ToPb(GetMoveDir(), p->mutable_dir());
p->set_heroid(meta->i->id());
p->set_master_uniid(master ? master->GetEntityUniId() : 0);
p->set_master_uniid(master.Get() ? master.Get()->GetEntityUniId() : 0);
}
void Hero::Update(int delta_time)

View File

@ -3,6 +3,7 @@
#include "creature.h"
#include "cs_proto.pb.h"
#include "weakptr.h"
namespace MetaData
{
@ -16,7 +17,7 @@ class Creature;
class Hero : public Creature
{
public:
Creature* master = nullptr;
CreatureWeakPtr master;
MetaData::Player* meta = nullptr;
bool shot_hold = false;

View File

@ -605,7 +605,7 @@ Hero* Room::CreateHero(Creature* master,
{
Hero* hero = EntityFactory::Instance()->MakeHero(AllocUniid());
hero->meta = meta;
hero->master = master;
hero->master.Attach(master);
hero->room = this;
hero->SetPos(pos);
hero->SetMoveDir(dir);