game2006/server/gameserver/moveableentity.cc
aozhiwei 41b32e5f5f 1
2023-11-20 15:31:52 +08:00

126 lines
3.4 KiB
C++

#include "precompile.h"
#include "moveableentity.h"
#include "room.h"
#include "human.h"
#include "mapinstance.h"
MoveableEntity::MoveableEntity():RoomEntity()
{
}
void MoveableEntity::TraverseAllLayerEntityList(std::function<void (Entity*, bool&)> func)
{
room->grid_service->TraverseAllLayerEntityList(room->GetRoomIdx(), grid_list_, func);
}
void MoveableEntity::TraverseAllLayerHumanList(std::function<void (Human*, bool&)> func)
{
room->grid_service->TraverseAllLayerHumanList(room->GetRoomIdx(), grid_list_, func);
}
void MoveableEntity::RefreshView()
{
TraverseAllLayerHumanList
(
[this] (Human* hum, bool& stop)
{
hum->AddToNewObjects(this);
hum->AddToPartObjects(this);
});
}
void MoveableEntity::OnGridListChange(std::set<GridCell*>& old_grids,
std::set<GridCell*>& inc_grids,
std::set<GridCell*>& dec_grids
)
{
room->grid_service->TraverseAllLayerHumanList
(
room->GetRoomIdx(),
inc_grids,
[this, &old_grids] (Human* hum, bool& stop)
{
if (!room->grid_service->CreatureInGridList(hum, old_grids)) {
hum->AddToNewObjects(this);
hum->AddToPartObjects(this);
hum->RemoveOutObjects(this);
}
});
room->grid_service->TraverseAllLayerHumanList
(
room->GetRoomIdx(),
dec_grids,
[this] (Human* hum, bool& stop)
{
if (!room->grid_service->CreatureInGridList(hum, GetGridList())) {
hum->AddOutObjects(this);
}
});
}
void MoveableEntity::SyncAroundPlayers(const char* file, int line, const char* func)
{
TraverseAllLayerHumanList
(
[this, file, line, func] (Human* hum, bool& stop)
{
hum->AddToNewObjects(this);
});
}
void MoveableEntity::UpdateCharImage(const char* file, int line, const char* func)
{
if (IsCreature(room)) {
Creature* c = (Creature*)this;
TraverseAllLayerHumanList
(
[c, file, line, func] (Human* hum, bool& stop)
{
hum->AddToImageObjects(c);
});
}
}
void MoveableEntity::TraverseCreatures(std::function<void (Creature*, bool&)> func)
{
room->grid_service->TraverseCreatures(room->GetRoomIdx(),
GetGridList(),
func);
}
void MoveableEntity::SetMoveDir(const glm::vec3& move_dir)
{
move_dir_ = move_dir;
++chg_move_dir_times_;
}
void MoveableEntity::SetAttackDir(const glm::vec3& attack_dir)
{
attack_dir_ = attack_dir;
++chg_attack_dir_times_;
#ifdef MYDEBUG1
if (IsCreature(room) && ((Creature*)this)->IsPlayer()) {
Creature* c = (Creature*)this;
c->SendDebugMsg(
a8::Format("SetAttackDir:%f,%f angle:%f\n",
{
attack_dir.x,
attack_dir.y,
GetAttackDirRotate()
})
);
}
#endif
}
float MoveableEntity::GetAttackDirRotate()
{
float angle = GlmHelper::CalcAngle(GetAttackDir(), GlmHelper::UP);
if (GetAttackDir().y > 0.00001f) {
angle = -angle;
}
return angle;
}