gridservice AddEntity ok

This commit is contained in:
aozhiwei 2019-04-24 21:08:12 +08:00
parent 281f725ccf
commit a047b17d29
3 changed files with 27 additions and 17 deletions

View File

@ -139,19 +139,38 @@ void GridService::MoveHuman(Human* hum)
}
}
void GridService::AddEntity(Entity* entity, std::set<GridCell*>& inc_grid_list)
void GridService::AddEntity(Entity* entity)
{
int x = (int)entity->pos.x + cell_width_;
int y = (int)entity->pos.y + cell_width_;
if (BroderOverFlow(x, y)) {
abort();
}
entity->grid_id = x/cell_width_ + (y/cell_width_) * cell_count_per_row_;
if (entity->grid_id == 0 || entity->grid_id > max_grid_id_) {
abort();
}
cells_[entity->grid_id].entity_list.insert(entity);
}
bool GridService::HumanInGridList(Human* hum, std::set<GridCell*>& grid_list)
{
for (auto& cell : grid_list) {
if (cell->human_list.find(hum) != cell->human_list.end()) {
return true;
}
}
return false;
}
bool GridService::EntityInGridList(Entity* entity, std::set<GridCell*>& grid_list)
{
for (auto& cell : grid_list) {
if (cell->entity_list.find(entity) != cell->entity_list.end()) {
return true;
}
}
return false;
}
void GridService::GetGridList(int grid_id, int offset,

View File

@ -37,7 +37,7 @@ class GridService
void AddHuman(Human* hum);
void MoveHuman(Human* hum);
void AddEntity(Entity* entity, std::set<GridCell*>& inc_grid_list);
void AddEntity(Entity* entity);
bool HumanInGridList(Human* hum, std::set<GridCell*>& grid_list);
bool EntityInGridList(Entity* entity, std::set<GridCell*>& grid_list);

View File

@ -403,10 +403,7 @@ void Room::CreateBuilding(int thing_id, float building_x, float building_y)
building->pos = Vector2D(building_x, building_y);
building->Initialize();
uniid_hash_[building->entity_uniid] = building;
{
std::set<GridCell*> inc_grid_list;
grid_service.AddEntity(building, inc_grid_list);
}
grid_service.AddEntity(building);
for (size_t door_idx = 0; door_idx < building_meta->doors.size(); ++door_idx) {
if (door_idx >= 0 && door_idx < building->meta->doors.size()) {
MetaData::Building::Door* door_meta = &building->meta->doors[door_idx];
@ -468,10 +465,7 @@ void Room::CreateLoot(int equip_id, Vector2D pos, int count)
entity->count = count;
entity->Initialize();
uniid_hash_[entity->entity_uniid] = entity;
{
std::set<GridCell*> inc_grid_list;
grid_service.AddEntity(entity, inc_grid_list);
}
grid_service.AddEntity(entity);
}
}
@ -953,10 +947,7 @@ Obstacle* Room::InternalCreateObstacle(int id, float x, float y,
on_precreate(entity);
}
uniid_hash_[entity->entity_uniid] = entity;
{
std::set<GridCell*> inc_grid_list;
grid_service.AddEntity(entity, inc_grid_list);
}
grid_service.AddEntity(entity);
return entity;
}
return nullptr;