OnGridListChange

This commit is contained in:
aozhiwei 2019-04-24 19:40:52 +08:00
parent 9dfdb4952d
commit 1e88baf74e
5 changed files with 112 additions and 18 deletions

View File

@ -119,15 +119,37 @@ void GridService::AddHuman(Human* hum)
Get123456789(hum->grid_id, hum->grid_list); Get123456789(hum->grid_id, hum->grid_list);
} }
void GridService::MoveHuman(Human* hum, float x, float y, void GridService::MoveHuman(Human* hum)
std::set<GridCell*>& inc_grid_list, {
std::set<GridCell*>& dec_grid_list int new_x = (int)hum->pos.x + cell_width_;
) int new_y = (int)hum->pos.y + cell_width_;
int new_grid_id = new_x/cell_width_ + (new_y/cell_width_) * cell_count_per_row_;
if (new_grid_id != hum->grid_id) {
std::set<GridCell*> inc_grid_list;
std::set<GridCell*> dec_grid_list;
std::set<GridCell*> old_grid_list = hum->grid_list;
ComputeDiff(hum->grid_id, new_grid_id,
hum->grid_list,
inc_grid_list,
dec_grid_list);
cells_[hum->grid_id].human_list.erase(hum);
cells_[new_grid_id].human_list.insert(hum);
hum->grid_id = new_grid_id;
hum->OnGridListChange(old_grid_list, inc_grid_list, dec_grid_list);
}
}
void GridService::AddEntity(Entity* entity, std::set<GridCell*>& inc_grid_list)
{ {
} }
void GridService::AddEntity(Entity* entity, std::set<GridCell*>& inc_grid_list) bool GridService::HumanInGridList(Human* hum, std::set<GridCell*>& grid_list)
{
}
bool GridService::EntityInGridList(Entity* entity, std::set<GridCell*>& grid_list)
{ {
} }
@ -146,3 +168,11 @@ void GridService::GetGridList(int grid_id, int offset,
grid_list.insert(&cells_[tmp_grid_id]); grid_list.insert(&cells_[tmp_grid_id]);
} }
} }
void GridService::ComputeDiff(int old_grid_id, int new_grid_id,
std::set<GridCell*>& grid_list,
std::set<GridCell*>& inc_grid_list,
std::set<GridCell*>& dec_grid_list)
{
int diff_grid = new_grid_id - old_grid_id;
}

View File

@ -35,16 +35,20 @@ class GridService
void Get369(int grid_id, std::set<GridCell*>& grid_list); void Get369(int grid_id, std::set<GridCell*>& grid_list);
void AddHuman(Human* hum); void AddHuman(Human* hum);
void MoveHuman(Human* hum, float x, float y, void MoveHuman(Human* hum);
std::set<GridCell*>& inc_grid_list,
std::set<GridCell*>& dec_grid_list
);
void AddEntity(Entity* entity, std::set<GridCell*>& inc_grid_list); void AddEntity(Entity* entity, std::set<GridCell*>& inc_grid_list);
bool HumanInGridList(Human* hum, std::set<GridCell*>& grid_list);
bool EntityInGridList(Entity* entity, std::set<GridCell*>& grid_list);
private: private:
inline void GetGridList(int grid_id, int offset, inline void GetGridList(int grid_id, int offset,
std::set<GridCell*>& grid_list); std::set<GridCell*>& grid_list);
void ComputeDiff(int old_grid_id, int new_grid_id,
std::set<GridCell*>& grid_list,
std::set<GridCell*>& inc_grid_list,
std::set<GridCell*>& dec_grid_list);
private: private:
Room* room_ = nullptr; Room* room_ = nullptr;

View File

@ -584,14 +584,9 @@ void Human::AddToPartObjects(Entity* entity)
part_objects.insert(entity); part_objects.insert(entity);
} }
void Human::RemoveNewObjects(Entity* entity) void Human::RemoveObjects(Entity* entity)
{ {
new_objects.erase(entity); del_objects.insert(entity->entity_uniid);
}
void Human::RemovePartObjects(Entity* entity)
{
part_objects.erase(entity);
} }
bool Human::HasLiveTeammate() bool Human::HasLiveTeammate()
@ -713,3 +708,59 @@ void Human::RefreshView()
} }
} }
} }
void Human::OnGridListChange(std::set<GridCell*>& old_grid_list,
std::set<GridCell*>& inc_grid_list,
std::set<GridCell*>& dec_grid_list
)
{
for (GridCell* cell : inc_grid_list) {
for (Human* hum : cell->human_list) {
if (!room->grid_service.HumanInGridList(hum, old_grid_list)) {
hum->AddToNewObjects(this);
hum->AddToPartObjects(this);
AddToNewObjects(hum);
AddToPartObjects(hum);
}
}
for (Entity* entity : cell->entity_list) {
if (!room->grid_service.EntityInGridList(entity, old_grid_list)) {
switch (entity->entity_type) {
case ET_Building:
case ET_Obstacle:
{
AddToNewObjects(entity);
}
break;
default:
{
}
break;
}
}
}
}
for (GridCell* cell : dec_grid_list) {
for (Human* entity : cell->human_list) {
if (!room->grid_service.HumanInGridList(entity, grid_list)) {
RemoveObjects(entity);
}
}
for (Entity* entity : cell->entity_list) {
if (!room->grid_service.EntityInGridList(entity, grid_list)) {
switch (entity->entity_type) {
case ET_Building:
case ET_Obstacle:
{
RemoveObjects(entity);
}
break;
default:
{
}
break;
}
}
}
}
}

View File

@ -115,18 +115,22 @@ class Human : public Entity
void DecHP(float dec_hp, int killer_id, const std::string& killer_name); void DecHP(float dec_hp, int killer_id, const std::string& killer_name);
void AddToNewObjects(Entity* entity); void AddToNewObjects(Entity* entity);
void AddToPartObjects(Entity* entity); void AddToPartObjects(Entity* entity);
void RemoveNewObjects(Entity* entity); void RemoveObjects(Entity* entity);
void RemovePartObjects(Entity* entity);
bool HasLiveTeammate(); bool HasLiveTeammate();
void Land(); void Land();
void DoJump(); void DoJump();
void FindLocation(); void FindLocation();
void RefreshView(); void RefreshView();
void OnGridListChange(std::set<GridCell*>& old_grid_list,
std::set<GridCell*>& inc_grid_list,
std::set<GridCell*>& dec_grid_list
);
protected: protected:
long long last_shot_frameno_ = 0; long long last_shot_frameno_ = 0;
std::set<Entity*> new_objects; std::set<Entity*> new_objects;
std::set<Entity*> part_objects; std::set<Entity*> part_objects;
std::set<int> del_objects;
private: private:
CircleCollider* self_collider_ = nullptr; CircleCollider* self_collider_ = nullptr;

View File

@ -101,6 +101,7 @@ void Player::UpdateMove()
moved_frames = 0; moved_frames = 0;
return; return;
} }
bool move_ok = false;
int speed = std::max(1, (int)GetSpeed()); int speed = std::max(1, (int)GetSpeed());
for (int i = 0; i < speed; ++i) { for (int i = 0; i < speed; ++i) {
Vector2D old_pos = pos; Vector2D old_pos = pos;
@ -112,6 +113,10 @@ void Player::UpdateMove()
} }
break; break;
} }
move_ok = true;
}
if (move_ok) {
room->grid_service.MoveHuman(this);
} }
if (last_collision_door && !TestCollision(last_collision_door)) { if (last_collision_door && !TestCollision(last_collision_door)) {
last_collision_door = nullptr; last_collision_door = nullptr;