137 lines
4.3 KiB
C++
137 lines
4.3 KiB
C++
#include "precompile.h"
|
|
|
|
#include "incubator.h"
|
|
#include "room.h"
|
|
#include "human.h"
|
|
#include "metamgr.h"
|
|
|
|
void Incubator::Init()
|
|
{
|
|
|
|
}
|
|
|
|
void Incubator::UnInit()
|
|
{
|
|
|
|
}
|
|
|
|
void Incubator::AllocAndroid(Human* target, int num)
|
|
{
|
|
if (room->xtimer.GetRunningTimer() == nullptr) {
|
|
abort();
|
|
}
|
|
int try_count = 0;
|
|
a8::Vec2 dir = a8::Vec2::UP;
|
|
while (num > 0 && try_count < 20 && !hold_humans_.empty()) {
|
|
dir.Rotate(a8::RandAngle());
|
|
int rand_len = rand() % MetaMgr::Instance()->incubator_rand_length;
|
|
Human* hum = hold_humans_[0];
|
|
a8::Vec2 old_pos = hum->GetPos();
|
|
hum->SetPos(target->GetPos() + dir * (MetaMgr::Instance()->incubator_base_length + rand_len));
|
|
if (hum->CollisonDetection() || !CanSet(hum, target)) {
|
|
hum->SetPos(old_pos);
|
|
} else {
|
|
room->EnableHuman(hum);
|
|
#ifdef DEBUG
|
|
if (!target->InNewObjects(hum)) {
|
|
abort();
|
|
}
|
|
if (hum->dead) {
|
|
abort();
|
|
}
|
|
#endif
|
|
hum->MustBeAddBuff(hum, kTraceBuffId);
|
|
hold_humans_.erase(hold_humans_.begin());
|
|
--num;
|
|
#ifdef DEBUG
|
|
room->BroadcastDebugMsg(a8::Format("投放机器人 %d:%s pos:%d,%d pos1:%d,%d %d num:%d",
|
|
{hum->GetEntityUniId(),
|
|
hum->name,
|
|
hum->GetPos().x,
|
|
hum->GetPos().y,
|
|
target->GetPos().x,
|
|
target->GetPos().y,
|
|
hum->GetPos().Distance(target->GetPos()),
|
|
hold_humans_.size()}));
|
|
#endif
|
|
}
|
|
++try_count;
|
|
}
|
|
#ifdef DEBUG
|
|
if (num > 0) {
|
|
room->BroadcastDebugMsg(a8::Format("投放机器人 分配失败 %d", {hold_humans_.size()}));
|
|
}
|
|
#endif
|
|
}
|
|
|
|
void Incubator::RecycleAndroid(Human* hum)
|
|
{
|
|
Human* nearest_hum = nullptr;
|
|
Human* target = hum;
|
|
float distance = 10000000;
|
|
room->TouchAlivePlayers
|
|
(
|
|
a8::XParams(),
|
|
[&nearest_hum, target, &distance] (Human* hum, a8::XParams& param) -> bool
|
|
{
|
|
float tmp_distance = hum->GetPos().ManhattanDistance(target->GetPos());
|
|
if (tmp_distance < distance) {
|
|
nearest_hum = hum;
|
|
distance = tmp_distance;
|
|
}
|
|
return true;
|
|
});
|
|
if (hum->dead) {
|
|
#ifdef DEBUG
|
|
room->BroadcastDebugMsg(a8::Format("回收机器人 %d:%s 角色已死亡",
|
|
{hum->GetEntityUniId(),
|
|
hum->name}));
|
|
#endif
|
|
hum->RemoveBuffByEffectId(kBET_BeRecycle);
|
|
return;
|
|
}
|
|
if (distance < 450) {
|
|
#ifdef DEBUG
|
|
room->BroadcastDebugMsg(a8::Format("回收机器人 %d:%s 距离太近",
|
|
{hum->GetEntityUniId(),
|
|
hum->name}));
|
|
#endif
|
|
hum->RemoveBuffByEffectId(kBET_BeRecycle);
|
|
return;
|
|
}
|
|
if (distance > 1450) {
|
|
hum->RemoveBuffByEffectId(kBET_BeRecycle);
|
|
hold_humans_.push_back(hum);
|
|
room->DisableHuman(hum);
|
|
#ifdef DEBUG
|
|
room->BroadcastDebugMsg(a8::Format("回收机器人 %d:%s:%d 添加到回收列表",
|
|
{hum->GetEntityUniId(),
|
|
hum->name,
|
|
hold_humans_.size()
|
|
}));
|
|
#endif
|
|
return;
|
|
}
|
|
}
|
|
|
|
bool Incubator::CanSet(Human* hum, Human* exclude_hum)
|
|
{
|
|
Human* target = hum;
|
|
bool can_set = true;
|
|
room->TouchAlivePlayers
|
|
(
|
|
a8::XParams(),
|
|
[target, exclude_hum, &can_set] (Human* hum, a8::XParams& param) -> bool
|
|
{
|
|
if (hum != exclude_hum) {
|
|
if (target->GetPos().ManhattanDistance(hum->GetPos()) <
|
|
MetaMgr::Instance()->incubator_canset_distance) {
|
|
can_set = false;
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
});
|
|
return can_set;
|
|
}
|