修复gridservice越界问题

This commit is contained in:
aozhiwei 2019-04-25 16:52:47 +08:00
parent 07535d954f
commit 6d77ee37d8
7 changed files with 30 additions and 5 deletions

View File

@ -97,4 +97,11 @@ void Entity::BroadcastFullState()
void Entity::BroadcastDeleteState() void Entity::BroadcastDeleteState()
{ {
std::set<GridCell*> grid_list;
room->grid_service.Get123456789(grid_id, grid_list);
for (auto& grid : grid_list) {
for (Human* hum : grid->human_list) {
hum->RemoveObjects(this);
}
}
} }

View File

@ -104,7 +104,7 @@ void FrameEvent::AddExplosion(Bullet* bullet, int item_id, Vector2D bomb_pos)
void FrameEvent::AddSmoke(Bullet* bullet, int item_id, Vector2D pos) void FrameEvent::AddSmoke(Bullet* bullet, int item_id, Vector2D pos)
{ {
{ {
auto& tuple = a8::FastAppend(explosions_); auto& tuple = a8::FastAppend(smokes_);
std::get<0>(tuple) = bullet->player; std::get<0>(tuple) = bullet->player;
auto& p = std::get<1>(tuple); auto& p = std::get<1>(tuple);
@ -113,10 +113,10 @@ void FrameEvent::AddSmoke(Bullet* bullet, int item_id, Vector2D pos)
p.set_player_id(bullet->player->entity_uniid); p.set_player_id(bullet->player->entity_uniid);
} }
{ {
int explosion_idx = smokes_.size() - 1; int idx = smokes_.size() - 1;
for (auto& cell : bullet->player->grid_list) { for (auto& cell : bullet->player->grid_list) {
for (auto& hum : cell->human_list) { for (auto& hum : cell->human_list) {
hum->explosions_.push_back(explosion_idx); hum->smokes_.push_back(idx);
} }
} }
} }

View File

@ -174,8 +174,15 @@ void GridService::MoveBullet(Bullet* bullet)
} }
} }
void GridService::DelBullet(Bullet* bullet)
{
GridCell& cell = cells_[bullet->grid_id];
cell.bullet_list.erase(bullet);
}
void GridService::AddEntity(Entity* entity) void GridService::AddEntity(Entity* entity)
{ {
assert(entity->entity_type != ET_Player);
int x = (int)entity->pos.x + cell_width_; int x = (int)entity->pos.x + cell_width_;
int y = (int)entity->pos.y + cell_width_; int y = (int)entity->pos.y + cell_width_;
if (BroderOverFlow(x, y)) { if (BroderOverFlow(x, y)) {
@ -188,6 +195,12 @@ void GridService::AddEntity(Entity* entity)
cells_[entity->grid_id].entity_list.insert(entity); cells_[entity->grid_id].entity_list.insert(entity);
} }
void GridService::DelEntity(Entity* entity)
{
GridCell& cell = cells_[entity->grid_id];
cell.entity_list.erase(entity);
}
bool GridService::HumanInGridList(Human* hum, std::set<GridCell*>& grid_list) bool GridService::HumanInGridList(Human* hum, std::set<GridCell*>& grid_list)
{ {
for (auto& cell : grid_list) { for (auto& cell : grid_list) {

View File

@ -41,8 +41,10 @@ class GridService
void AddBullet(Bullet* bullet); void AddBullet(Bullet* bullet);
void MoveBullet(Bullet* bullet); void MoveBullet(Bullet* bullet);
void DelBullet(Bullet* bullet);
void AddEntity(Entity* entity); void AddEntity(Entity* entity);
void DelEntity(Entity* entity);
bool HumanInGridList(Human* hum, std::set<GridCell*>& grid_list); bool HumanInGridList(Human* hum, std::set<GridCell*>& grid_list);
bool EntityInGridList(Entity* entity, std::set<GridCell*>& grid_list); bool EntityInGridList(Entity* entity, std::set<GridCell*>& grid_list);

View File

@ -41,6 +41,7 @@ Human::~Human()
void Human::Initialize() void Human::Initialize()
{ {
Entity::Initialize(); Entity::Initialize();
RecalcSelfCollider();
} }
float Human::GetSpeed() float Human::GetSpeed()

View File

@ -27,7 +27,6 @@ void Player::Initialize()
Human::Initialize(); Human::Initialize();
health = meta->i->health(); health = meta->i->health();
max_energy_shield = energy_shield; max_energy_shield = energy_shield;
RecalcSelfCollider();
} }
void Player::Update(int delta_time) void Player::Update(int delta_time)

View File

@ -419,6 +419,7 @@ void Room::CreateLoot(int equip_id, Vector2D pos, int count)
entity->Initialize(); entity->Initialize();
uniid_hash_[entity->entity_uniid] = entity; uniid_hash_[entity->entity_uniid] = entity;
grid_service.AddEntity(entity); grid_service.AddEntity(entity);
entity->BroadcastFullState();
} }
} }
@ -450,16 +451,18 @@ void Room::RemoveObjectLater(Entity* entity)
case ET_Bullet: case ET_Bullet:
{ {
entity->room->moveable_hash_.erase(entity->entity_uniid); entity->room->moveable_hash_.erase(entity->entity_uniid);
entity->room->grid_service.DelBullet((Bullet*)entity);
} }
break; break;
case ET_Loot: case ET_Loot:
{ {
entity->BroadcastDeleteState(); entity->BroadcastDeleteState();
entity->room->grid_service.DelEntity(entity);
} }
break; break;
default: default:
{ {
abort();
} }
break; break;
} }