game2004/server/gameserver/gridcell.cc
2020-05-30 22:10:41 +08:00

129 lines
3.1 KiB
C++

#include "precompile.h"
#include "gridcell.h"
#include "bullet.h"
#include "entity.h"
#include "human.h"
#include "room.h"
GridCell::GridCell()
{
human_list_.reserve(MAX_ROOM_IDX);
entity_list_.reserve(MAX_ROOM_IDX);
bullet_list_.reserve(MAX_ROOM_IDX);
for (int i = 0; i < MAX_ROOM_IDX; ++i) {
human_list_.push_back(std::set<Human*>());
entity_list_.push_back(std::set<Entity*>());
bullet_list_.push_back(std::set<Bullet*>());
}
}
void GridCell::ClearRoomData(Room* room)
{
human_list_[room->GetRoomIdx()].clear();
entity_list_[room->GetRoomIdx()].clear();
bullet_list_[room->GetRoomIdx()].clear();
}
void GridCell::AddHuman(Human* hum)
{
human_list_[hum->room->GetRoomIdx()].insert(hum);
}
void GridCell::RemoveHuman(Human* hum)
{
human_list_[hum->room->GetRoomIdx()].erase(hum);
}
bool GridCell::ExistsHuman(Human* hum)
{
return human_list_[hum->room->GetRoomIdx()].find(hum) != human_list_[hum->room->GetRoomIdx()].end();
}
void GridCell::TouchHumanList(std::function<void (Human*, bool&)>& func,
int room_idx,
bool& stop)
{
for (Human* hum : human_list_[room_idx]) {
func(hum, stop);
if (stop) {
return;
}
}
}
void GridCell::TouchHumanListEx(std::function<void (Human*, bool&)> func,
int room_idx,
bool& stop)
{
TouchHumanList(func, room_idx, stop);
}
void GridCell::AddBullet(Bullet* bullet)
{
bullet_list_[bullet->room->GetRoomIdx()].insert(bullet);
}
void GridCell::RemoveBullet(Bullet* bullet)
{
bullet_list_[bullet->room->GetRoomIdx()].erase(bullet);
}
void GridCell::AddPermanentEntity(Entity* entity)
{
entity_list_[0].insert(entity);
}
void GridCell::AddRoomEntity(Room* room, Entity* entity)
{
entity_list_[room->GetRoomIdx()].insert(entity);
}
void GridCell::RemoveRoomEntity(Room* room, Entity* entity)
{
entity_list_[room->GetRoomIdx()].erase(entity);
}
bool GridCell::ExistsEntity(Room* room, Entity* entity)
{
if (entity_list_[0].find(entity) != entity_list_[0].end()) {
return true;
}
return entity_list_[room->GetRoomIdx()].find(entity) !=
entity_list_[room->GetRoomIdx()].end();
}
void GridCell::TouchLayer0EntityList(std::function<void (Entity*, bool&)>& func,
bool& stop)
{
for (Entity* entity : entity_list_[0]) {
func(entity, stop);
if (stop) {
return;
}
}
}
void GridCell::TouchLayer1EntityList(std::function<void (Entity*, bool&)>& func,
int room_idx,
bool& stop)
{
for (Entity* entity : entity_list_[room_idx]) {
func(entity, stop);
if (stop) {
return;
}
}
}
void GridCell::TouchAllLayerEntityList(std::function<void (Entity*, bool&)>& func,
int room_idx,
bool& stop)
{
TouchLayer0EntityList(func, stop);
if (!stop) {
TouchLayer1EntityList(func, room_idx, stop);
}
}