This commit is contained in:
aozhiwei 2023-11-04 20:30:06 +08:00
parent 12187e1c2f
commit b8661400a9
2 changed files with 16 additions and 17 deletions

View File

@ -33,7 +33,10 @@ void GridService::Init(int width, int height, int cell_width)
cell_count_per_row_ = (map_height_ / cell_width + 1) + 2;
cell_count_per_col_ = (map_width_ / cell_width + 1) + 2;
max_grid_id_ = cell_count_per_row_ * cell_count_per_col_;
cells_ = new GridCell[max_grid_id_ + 1];
cells_.reserve(max_grid_id_ + 1);
for (int i = 0; i < max_grid_id_ + 1; ++i) {
cells_.push_back(std::make_shared<GridCell>());
}
grid_offset_arr_[0] = -(cell_count_per_row_ - 1);
grid_offset_arr_[1] = -cell_count_per_row_;
@ -55,10 +58,6 @@ void GridService::Init(int width, int height, int cell_width)
void GridService::UnInit()
{
if (cells_) {
delete[] cells_;
cells_ = nullptr;
}
}
bool GridService::BroderOverFlow(int x, int y)
@ -113,8 +112,8 @@ void GridService::ClearRoomData(Room* room)
A8_ABORT();
}
for (int i = 0; i < (max_grid_id_ + 1); ++i) {
GridCell& cell = cells_[i];
cell.ClearRoomData(room);
auto cell = cells_.at(i);
cell->ClearRoomData(room);
}
}
@ -144,14 +143,14 @@ void GridService::AddCreature(Creature* c)
if (c->GetGridId() == 0 || c->GetGridId() > max_grid_id_) {
A8_ABORT();
}
cells_[c->GetGridId()].AddCreature(c);
cells_.at(c->GetGridId())->AddCreature(c);
GetAllCells(c->room, c->GetGridId(), c->GetGridList());
}
void GridService::RemoveCreature(Creature* c)
{
GridCell& cell = cells_[c->GetGridId()];
cell.RemoveCreature(c);
auto cell = cells_.at(c->GetGridId());
cell->RemoveCreature(c);
}
void GridService::MoveCreature(Creature* c)
@ -190,8 +189,8 @@ void GridService::MoveCreature(Creature* c)
c->GetGridList(),
inc_grid_list,
dec_grid_list);
cells_[c->GetGridId()].RemoveCreature(c);
cells_[new_grid_id].AddCreature(c);
cells_.at(c->GetGridId())->RemoveCreature(c);
cells_.at(new_grid_id)->AddCreature(c);
c->SetGridId(new_grid_id);
c->OnGridListChange(old_grid_list, inc_grid_list, dec_grid_list);
}
@ -224,13 +223,13 @@ void GridService::AddRoomEntity(Room* room, Entity* entity)
if (entity->GetGridId() == 0 || entity->GetGridId() > max_grid_id_) {
A8_ABORT();
}
cells_[entity->GetGridId()].AddRoomEntity(room, entity);
cells_.at(entity->GetGridId())->AddRoomEntity(room, entity);
}
void GridService::DelRoomEntity(Room* room, Entity* entity)
{
GridCell& cell = cells_[entity->GetGridId()];
cell.RemoveRoomEntity(room, entity);
auto cell = cells_.at(entity->GetGridId());
cell->RemoveRoomEntity(room, entity);
}
bool GridService::CreatureInGridList(Creature* c, std::set<GridCell*>& grid_list)
@ -295,7 +294,7 @@ void GridService::GetGridList(int grid_id, int offset,
}
int tmp_grid_id = grid_id + grid_offset_arr_[offset - 1];
if (tmp_grid_id > 0) {
grid_list.insert(&cells_[tmp_grid_id]);
grid_list.insert(cells_.at(tmp_grid_id).get());
}
}

View File

@ -65,7 +65,7 @@ class GridService
std::set<GridCell*>& dec_grid_list);
private:
GridCell* cells_ = nullptr;
std::vector<std::shared_ptr<GridCell>> cells_;
int max_grid_id_ = 0;
int map_width_ = 0;
int map_height_ = 0;