This commit is contained in:
aozhiwei 2021-03-16 11:14:02 +08:00
parent 8d1df1ac1c
commit b1305976bb
3 changed files with 53 additions and 3 deletions

View File

@ -5,6 +5,7 @@
#include "entity.h"
#include "human.h"
#include "room.h"
#include "car.h"
GridCell::GridCell()
{
@ -24,6 +25,7 @@ void GridCell::ClearRoomData(Room* room)
human_list_[room->GetRoomIdx()].clear();
entity_list_[room->GetRoomIdx()].clear();
bullet_list_[room->GetRoomIdx()].clear();
bullet_list_[room->GetRoomIdx()].clear();
}
void GridCell::AddHuman(Human* hum)
@ -70,6 +72,16 @@ void GridCell::RemoveBullet(Bullet* bullet)
bullet_list_[bullet->room->GetRoomIdx()].erase(bullet);
}
void GridCell::AddCar(Car* car)
{
car_list_[car->room->GetRoomIdx()].insert(car);
}
void GridCell::RemoveCar(Car* car)
{
car_list_[car->room->GetRoomIdx()].erase(car);
}
void GridCell::AddPermanentEntity(Entity* entity)
{
entity_list_[0].insert(entity);

View File

@ -4,6 +4,7 @@ class Entity;
class Human;
class Bullet;
class Room;
class Car;
class GridCell
{
@ -23,6 +24,8 @@ public:
void AddBullet(Bullet* bullet);
void RemoveBullet(Bullet* bullet);
void AddCar(Car* car);
void RemoveCar(Car* car);
void AddPermanentEntity(Entity* entity);
void AddRoomEntity(Room* room, Entity* entity);
void RemoveRoomEntity(Room* room, Entity* entity);
@ -40,4 +43,5 @@ private:
std::vector<std::set<Human*>> human_list_;
std::vector<std::set<Entity*>> entity_list_;
std::vector<std::set<Bullet*>> bullet_list_;
std::vector<std::set<Car*>> car_list_;
};

View File

@ -5,6 +5,7 @@
#include "bullet.h"
#include "room.h"
#include "gridcell.h"
#include "car.h"
/*
1 2 3
@ -193,17 +194,50 @@ void GridService::DelBullet(Bullet* bullet)
void GridService::AddCar(Car* car)
{
int x = (int)car->GetX() + cell_width_;
int y = (int)car->GetY() + cell_width_;
if (BroderOverFlow(x, y)) {
abort();
}
car->SetGridId(x/cell_width_ + (y/cell_width_) * cell_count_per_row_);
if (car->GetGridId() == 0 || car->GetGridId() > max_grid_id_) {
abort();
}
cells_[car->GetGridId()].AddCar(car);
GetAllCells(car->room, car->GetGridId(), car->GetGridList());
}
void GridService::MoveCar(Car* car)
{
int new_x = (int)car->GetX() + cell_width_;
int new_y = (int)car->GetY() + cell_width_;
int new_grid_id = new_x/cell_width_ + (new_y/cell_width_) * cell_count_per_row_;
if (BroderOverFlow(new_x, new_y)) {
abort();
}
if (new_grid_id == 0 || new_grid_id > max_grid_id_) {
abort();
}
if (new_grid_id != car->GetGridId()) {
std::set<GridCell*> inc_grid_list;
std::set<GridCell*> dec_grid_list;
std::set<GridCell*> old_grid_list = car->GetGridList();
ComputeDiff(car->room,
car->GetGridId(),
new_grid_id,
car->GetGridList(),
inc_grid_list,
dec_grid_list);
cells_[car->GetGridId()].RemoveCar(car);
cells_[new_grid_id].AddCar(car);
car->SetGridId(new_grid_id);
}
}
void GridService::DelCar(Car* car)
{
GridCell& cell = cells_[car->GetGridId()];
cell.RemoveCar(car);
}
void GridService::AddRoomEntity(Room* room, Entity* entity)