game2005/server/gameserver/gridcell.cc
2021-03-30 10:26:34 +08:00

141 lines
3.3 KiB
C++

#include "precompile.h"
#include "gridcell.h"
#include "bullet.h"
#include "entity.h"
#include "human.h"
#include "room.h"
#include "car.h"
#include "hero.h"
#include "creature.h"
GridCell::GridCell()
{
entitys_.reserve(MAX_ROOM_IDX);
bullets_.reserve(MAX_ROOM_IDX);
creatures_.reserve(MAX_ROOM_IDX);
for (int i = 0; i < MAX_ROOM_IDX; ++i) {
entitys_.push_back(std::set<Entity*>());
bullets_.push_back(std::set<Bullet*>());
creatures_.push_back(std::set<Creature*>());
}
}
void GridCell::ClearRoomData(Room* room)
{
entitys_[room->GetRoomIdx()].clear();
bullets_[room->GetRoomIdx()].clear();
creatures_[room->GetRoomIdx()].clear();
}
void GridCell::TouchHumanList(std::function<void (Human*, bool&)> func,
int room_idx,
bool& stop)
{
for (Creature* c : creatures_[room_idx]) {
if (!c->IsHuman()) {
continue;
}
Human* hum = (Human*)c;
func(hum, stop);
if (stop) {
return;
}
}
}
void GridCell::TouchCreatures(std::function<void (Creature*, bool&)>& func,
int room_idx,
bool& stop)
{
for (Creature* c : creatures_[room_idx]) {
func(c, stop);
if (stop) {
return;
}
}
}
void GridCell::AddBullet(Bullet* bullet)
{
bullets_[bullet->room->GetRoomIdx()].insert(bullet);
}
void GridCell::RemoveBullet(Bullet* bullet)
{
bullets_[bullet->room->GetRoomIdx()].erase(bullet);
}
void GridCell::AddPermanentEntity(Entity* entity)
{
entitys_[0].insert(entity);
}
void GridCell::AddRoomEntity(Room* room, Entity* entity)
{
entitys_[room->GetRoomIdx()].insert(entity);
}
void GridCell::RemoveRoomEntity(Room* room, Entity* entity)
{
entitys_[room->GetRoomIdx()].erase(entity);
}
bool GridCell::EntityExists(Room* room, Entity* entity)
{
if (entitys_[0].find(entity) != entitys_[0].end()) {
return true;
}
return entitys_[room->GetRoomIdx()].find(entity) !=
entitys_[room->GetRoomIdx()].end();
}
void GridCell::TouchLayer0EntityList(std::function<void (Entity*, bool&)>& func,
bool& stop)
{
for (Entity* entity : entitys_[0]) {
func(entity, stop);
if (stop) {
return;
}
}
}
void GridCell::TouchLayer1EntityList(std::function<void (Entity*, bool&)>& func,
int room_idx,
bool& stop)
{
for (Entity* entity : entitys_[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);
}
}
void GridCell::AddCreature(Creature* c)
{
creatures_[c->room->GetRoomIdx()].insert(c);
}
void GridCell::RemoveCreature(Creature* c)
{
creatures_[c->room->GetRoomIdx()].erase(c);
}
bool GridCell::CreatureExists(Creature* c)
{
return creatures_[c->room->GetRoomIdx()].find(c) != creatures_[c->room->GetRoomIdx()].end();
}