yuhua grid_id

This commit is contained in:
aozhiwei 2020-05-30 20:50:55 +08:00
parent e9055b5e49
commit 680e25e15b
5 changed files with 35 additions and 33 deletions

View File

@ -93,7 +93,7 @@ void Entity::ClearColliders()
void Entity::BroadcastFullState(Room* room)
{
std::set<GridCell*> tmp_grids;
room->grid_service->GetAllCells(room, grid_id, tmp_grids);
room->grid_service->GetAllCells(room, grid_id_, tmp_grids);
room->grid_service->TouchAllLayerHumanList
(
room->GetRoomIdx(),
@ -107,7 +107,7 @@ void Entity::BroadcastFullState(Room* room)
void Entity::BroadcastDeleteState(Room* room)
{
std::set<GridCell*> tmp_grids;
room->grid_service->GetAllCells(room, grid_id, tmp_grids);
room->grid_service->GetAllCells(room, grid_id_, tmp_grids);
room->grid_service->TouchAllLayerHumanList
(
room->GetRoomIdx(),

View File

@ -14,8 +14,6 @@ class Entity
{
public:
int grid_id = 0;
Entity();
virtual ~Entity();
virtual void Initialize();
@ -32,6 +30,7 @@ class Entity
EntitySubType_e GetEntitySubType() { return entity_subtype_; }
virtual bool IsEntityType(EntityType_e type) { return type == entity_type_;}
virtual bool IsEntitySubType(EntitySubType_e subtype) { return subtype == entity_subtype_;}
int GetGridId() const { return grid_id_; }
bool TestCollision(Room* room, Entity* b);
bool TestCollision(Room* room, ColliderComponent* b);
bool TestCollisionEx(Room* room, const a8::Vec2& aabb_pos, AabbCollider& aabb_box);
@ -47,6 +46,7 @@ class Entity
private:
void ClearColliders();
void SetGridId(int grid_id) { grid_id_ = grid_id; }
protected:
std::list<ColliderComponent*> colliders_;
@ -57,6 +57,8 @@ private:
EntitySubType_e entity_subtype_ = EST_None;
a8::Vec2 pos_;
int grid_id_ = 0;
friend class EntityFactory;
friend class GridService;
};

View File

@ -120,12 +120,12 @@ void GridService::AddHuman(Human* hum)
if (BroderOverFlow(x, y)) {
abort();
}
hum->grid_id = x/cell_width_ + (y/cell_width_) * cell_count_per_row_;
if (hum->grid_id == 0 || hum->grid_id > max_grid_id_) {
hum->SetGridId(x/cell_width_ + (y/cell_width_) * cell_count_per_row_);
if (hum->GetGridId() == 0 || hum->GetGridId() > max_grid_id_) {
abort();
}
cells_[hum->grid_id].human_list[hum->room->GetRoomIdx()].insert(hum);
GetAllCells(hum->room, hum->grid_id, hum->GetGridList());
cells_[hum->GetGridId()].human_list[hum->room->GetRoomIdx()].insert(hum);
GetAllCells(hum->room, hum->GetGridId(), hum->GetGridList());
}
void GridService::MoveHuman(Human* hum)
@ -139,19 +139,19 @@ void GridService::MoveHuman(Human* hum)
if (new_grid_id == 0 || new_grid_id > max_grid_id_) {
abort();
}
if (new_grid_id != hum->grid_id) {
if (new_grid_id != hum->GetGridId()) {
std::set<GridCell*> inc_grid_list;
std::set<GridCell*> dec_grid_list;
std::set<GridCell*> old_grid_list = hum->GetGridList();
ComputeDiff(hum->room,
hum->grid_id,
hum->GetGridId(),
new_grid_id,
hum->GetGridList(),
inc_grid_list,
dec_grid_list);
cells_[hum->grid_id].human_list[hum->room->GetRoomIdx()].erase(hum);
cells_[hum->GetGridId()].human_list[hum->room->GetRoomIdx()].erase(hum);
cells_[new_grid_id].human_list[hum->room->GetRoomIdx()].insert(hum);
hum->grid_id = new_grid_id;
hum->SetGridId(new_grid_id);
hum->OnGridListChange(old_grid_list, inc_grid_list, dec_grid_list);
}
}
@ -163,12 +163,12 @@ void GridService::AddBullet(Bullet* bullet)
if (BroderOverFlow(x, y)) {
abort();
}
bullet->grid_id = x/cell_width_ + (y/cell_width_) * cell_count_per_row_;
if (bullet->grid_id == 0 || bullet->grid_id > max_grid_id_) {
bullet->SetGridId(x/cell_width_ + (y/cell_width_) * cell_count_per_row_);
if (bullet->GetGridId() == 0 || bullet->GetGridId() > max_grid_id_) {
abort();
}
cells_[bullet->grid_id].bullet_list[bullet->room->GetRoomIdx()].insert(bullet);
GetAllCells(bullet->room, bullet->grid_id, bullet->GetGridList());
cells_[bullet->GetGridId()].bullet_list[bullet->room->GetRoomIdx()].insert(bullet);
GetAllCells(bullet->room, bullet->GetGridId(), bullet->GetGridList());
}
void GridService::MoveBullet(Bullet* bullet)
@ -182,25 +182,25 @@ void GridService::MoveBullet(Bullet* bullet)
if (new_grid_id == 0 || new_grid_id > max_grid_id_) {
abort();
}
if (new_grid_id != bullet->grid_id) {
if (new_grid_id != bullet->GetGridId()) {
std::set<GridCell*> inc_grid_list;
std::set<GridCell*> dec_grid_list;
std::set<GridCell*> old_grid_list = bullet->GetGridList();
ComputeDiff(bullet->room,
bullet->grid_id,
bullet->GetGridId(),
new_grid_id,
bullet->GetGridList(),
inc_grid_list,
dec_grid_list);
cells_[bullet->grid_id].bullet_list[bullet->room->GetRoomIdx()].erase(bullet);
cells_[bullet->GetGridId()].bullet_list[bullet->room->GetRoomIdx()].erase(bullet);
cells_[new_grid_id].bullet_list[bullet->room->GetRoomIdx()].insert(bullet);
bullet->grid_id = new_grid_id;
bullet->SetGridId(new_grid_id);
}
}
void GridService::DelBullet(Bullet* bullet)
{
GridCell& cell = cells_[bullet->grid_id];
GridCell& cell = cells_[bullet->GetGridId()];
cell.bullet_list[bullet->room->GetRoomIdx()].erase(bullet);
}
@ -212,16 +212,16 @@ void GridService::AddRoomEntity(Room* room, Entity* entity)
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_) {
entity->SetGridId(x/cell_width_ + (y/cell_width_) * cell_count_per_row_);
if (entity->GetGridId() == 0 || entity->GetGridId() > max_grid_id_) {
abort();
}
cells_[entity->grid_id].entity_list[room->GetRoomIdx()].insert(entity);
cells_[entity->GetGridId()].entity_list[room->GetRoomIdx()].insert(entity);
}
void GridService::DelRoomEntity(Room* room, Entity* entity)
{
GridCell& cell = cells_[entity->grid_id];
GridCell& cell = cells_[entity->GetGridId()];
cell.entity_list[room->GetRoomIdx()].erase(entity);
}
@ -233,11 +233,11 @@ void GridService::AddPermanentEntity(Entity* entity)
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_) {
entity->SetGridId(x/cell_width_ + (y/cell_width_) * cell_count_per_row_);
if (entity->GetGridId() == 0 || entity->GetGridId() > max_grid_id_) {
abort();
}
cells_[entity->grid_id].entity_list[0].insert(entity);
cells_[entity->GetGridId()].entity_list[0].insert(entity);
}
bool GridService::HumanInGridList(Human* hum, std::set<GridCell*>& grid_list)
@ -412,4 +412,6 @@ void GridService::DeatchHuman(Human* target)
}
cell->human_list[target->room->GetRoomIdx()].erase(target);
}
target->SetGridId(0);
target->GetGridList().clear();
}

View File

@ -1357,7 +1357,7 @@ void Human::FillMFGasData(cs::MFGasData* gas_data)
bool Human::CanSee(const Human* hum) const
{
return room->grid_service->InView(grid_id, hum->grid_id);
return room->grid_service->InView(GetGridId(), hum->GetGridId());
}
void Human::RecalcVolume()
@ -3268,7 +3268,5 @@ void Human::RemoveFromScene()
hum->RemovePartObjects(target);
return true;
});
grid_id = 0;
GetGridList().clear();
last_collision_door_ = nullptr;
}

View File

@ -2048,7 +2048,7 @@ void Room::ShuaGridRound(Human* target)
a8::HasBitFlag(hum->status, HS_Disable) &&
!hum->real_dead &&
hum->team_uuid.empty() &&
grid_service->InView(target->grid_id, hum->GetPos().x, hum->GetPos().y)
grid_service->InView(target->GetGridId(), hum->GetPos().x, hum->GetPos().y)
) {
EnableHuman(hum);
xtimer.AddDeadLineTimerAndAttach
@ -2074,7 +2074,7 @@ void Room::ShuaGridRound(Human* target)
}
}
#ifdef DEBUG
a8::UdpLog::Instance()->Debug("OnHumanGrid %d %d", {target->grid_id, count});
a8::UdpLog::Instance()->Debug("OnHumanGrid %d %d", {target->GetGridId(), count});
#endif
#ifdef DEBUG
CheckPartObjects();