FindLocationWithTarget ok

This commit is contained in:
aozhiwei 2019-05-05 11:45:26 +08:00
parent e2bce87588
commit 308a64c62d
5 changed files with 98 additions and 22 deletions

View File

@ -67,19 +67,55 @@ void Entity::FindLocationWithTarget(Entity* target)
new_pos_dir.Normalize();
for (float i = distance; i < 10000000; i += 5.0f) {
pos = old_pos + new_pos_dir * i;
std::set<GridCell*> new_grid_list;
room->grid_service.GetAllCellsByXy(pos.x, pos.y, new_grid_list);
std::vector<Entity*> objects;
int detection_flags = 0;
for (auto& grid : new_grid_list) {
for (Entity* entity : grid->entity_list) {
switch (entity->entity_type) {
case ET_Obstacle:
{
a8::SetBitFlag(detection_flags, ET_Obstacle);
if (entity != this && TestCollision(entity)){
objects.push_back(entity);
break;
}
}
break;
default:
{
}
break;
}
}
}
#if 0
room->CollisionDetection(this, detection_flags, objects);
#endif
if (objects.empty()) {
break;
}
}
room->grid_service.RemoveFromGridList(grid_list, this);
{
grid_list.clear();
switch (entity_type) {
case ET_Player:
{
room->grid_service.AddHuman((Human*)this);
}
break;
case ET_Bullet:
{
room->grid_service.AddBullet((Bullet*)this);
}
break;
default:
{
room->grid_service.AddEntity(this);
}
break;
}
}
DestoryCollider(target_collider);
DestoryCollider(a_collider);
}
@ -87,7 +123,7 @@ void Entity::FindLocationWithTarget(Entity* target)
void Entity::BroadcastFullState()
{
std::set<GridCell*> grid_list;
room->grid_service.Get123456789(grid_id, grid_list);
room->grid_service.GetAllCells(grid_id, grid_list);
for (auto& grid : grid_list) {
for (Human* hum : grid->human_list) {
hum->AddToNewObjects(this);
@ -98,7 +134,7 @@ void Entity::BroadcastFullState()
void Entity::BroadcastDeleteState()
{
std::set<GridCell*> grid_list;
room->grid_service.Get123456789(grid_id, grid_list);
room->grid_service.GetAllCells(grid_id, grid_list);
for (auto& grid : grid_list) {
for (Human* hum : grid->human_list) {
hum->RemoveObjects(this);

View File

@ -56,13 +56,24 @@ bool GridService::BroderOverFlow(int x, int y)
return x <= min_x_ || x >= max_x_ || y <= min_y_ || y >= max_y_;
}
void GridService::Get123456789(int grid_id, std::set<GridCell*>& grid_list)
void GridService::GetAllCells(int grid_id, std::set<GridCell*>& grid_list)
{
for (size_t i = 1; i <= 9; ++i) {
GetGridList(grid_id, i, grid_list);
}
}
void GridService::GetAllCellsByXy(int x, int y, std::set<GridCell*>& grid_list)
{
int new_x = x + cell_width_;
int new_y = y + 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();
}
GetAllCells(new_grid_id, grid_list);
}
void GridService::Get123(int grid_id, std::set<GridCell*>& grid_list)
{
GetGridList(grid_id, 1, grid_list);
@ -117,7 +128,7 @@ void GridService::AddHuman(Human* hum)
abort();
}
cells_[hum->grid_id].human_list.insert(hum);
Get123456789(hum->grid_id, hum->grid_list);
GetAllCells(hum->grid_id, hum->grid_list);
}
void GridService::MoveHuman(Human* hum)
@ -158,7 +169,7 @@ void GridService::AddBullet(Bullet* bullet)
abort();
}
cells_[bullet->grid_id].bullet_list.insert(bullet);
Get123456789(bullet->grid_id, bullet->grid_list);
GetAllCells(bullet->grid_id, bullet->grid_list);
}
void GridService::MoveBullet(Bullet* bullet)
@ -246,6 +257,29 @@ bool GridService::InView(int a_grid, int b_grid)
a_grid + grid_offset_arr_[8] == b_grid;
}
void GridService::RemoveFromGridList(std::set<GridCell*>& grid_list, Entity* entity)
{
for (auto& grid : grid_list) {
switch (entity->entity_type) {
case ET_Player:
{
grid->human_list.erase((Human*)entity);
}
break;
case ET_Bullet:
{
grid->bullet_list.erase((Bullet*)entity);
}
break;
default:
{
grid->entity_list.erase(entity);
}
break;
}
}
}
void GridService::GetGridList(int grid_id, int offset,
std::set<GridCell*>& grid_list)
{
@ -270,7 +304,7 @@ void GridService::ComputeDiff(int old_grid_id, int new_grid_id,
#if 1
{
std::set<GridCell*> new_grid_list;
Get123456789(new_grid_id, new_grid_list);
GetAllCells(new_grid_id, new_grid_list);
for (auto& cell : new_grid_list) {
if (grid_list.find(cell) == grid_list.end()) {
inc_grid_list.insert(cell);
@ -329,7 +363,7 @@ void GridService::ComputeDiff(int old_grid_id, int new_grid_id,
Get123(old_grid_id, dec_grid_list);
} else {
std::set<GridCell*> new_grid_list;
Get123456789(new_grid_id, new_grid_list);
GetAllCells(new_grid_id, new_grid_list);
for (auto& cell : new_grid_list) {
if (grid_list.find(cell) == grid_list.end()) {
inc_grid_list.insert(cell);
@ -344,5 +378,5 @@ void GridService::ComputeDiff(int old_grid_id, int new_grid_id,
#endif
grid_list.clear();
Get123456789(new_grid_id, grid_list);
GetAllCells(new_grid_id, grid_list);
}

View File

@ -28,13 +28,8 @@ class GridService
void Init(int width, int height, int cell_width);
void UnInit();
bool BroderOverFlow(int x, int y);
void Get123456789(int grid_id, std::set<GridCell*>& grid_list);
void Get123(int grid_id, std::set<GridCell*>& grid_list);
void Get456(int grid_id, std::set<GridCell*>& grid_list);
void Get789(int grid_id, std::set<GridCell*>& grid_list);
void Get147(int grid_id, std::set<GridCell*>& grid_list);
void Get258(int grid_id, std::set<GridCell*>& grid_list);
void Get369(int grid_id, std::set<GridCell*>& grid_list);
void GetAllCells(int grid_id, std::set<GridCell*>& grid_list);
void GetAllCellsByXy(int x, int y, std::set<GridCell*>& grid_list);
void AddHuman(Human* hum);
void MoveHuman(Human* hum);
@ -49,8 +44,16 @@ class GridService
bool HumanInGridList(Human* hum, std::set<GridCell*>& grid_list);
bool EntityInGridList(Entity* entity, std::set<GridCell*>& grid_list);
bool InView(int a_grid, int b_grid);
void RemoveFromGridList(std::set<GridCell*>& grid_list, Entity* entity);
private:
void Get123(int grid_id, std::set<GridCell*>& grid_list);
void Get456(int grid_id, std::set<GridCell*>& grid_list);
void Get789(int grid_id, std::set<GridCell*>& grid_list);
void Get147(int grid_id, std::set<GridCell*>& grid_list);
void Get258(int grid_id, std::set<GridCell*>& grid_list);
void Get369(int grid_id, std::set<GridCell*>& grid_list);
inline void GetGridList(int grid_id, int offset,
std::set<GridCell*>& grid_list);
void ComputeDiff(int old_grid_id, int new_grid_id,

View File

@ -802,6 +802,7 @@ void Human::FindLocation()
}
if (ret) {
pos = new_pos;
room->grid_service.MoveHuman(this);
return;
}
}

View File

@ -46,7 +46,6 @@ void Room::Init()
xtimer_attacher.xtimer = &xtimer;
grid_service.Init(MAP_WIDTH, MAP_HEIGHT, MAP_CELL_WIDTH);
ShuaAndroid();
CreateThings();
stats_timer_ = a8::Timer::Instance()->AddRepeatTimer(
1000 * 5,
@ -72,6 +71,7 @@ void Room::Init()
&xtimer_attacher.timer_list_);
}
InitAirDrop();
ShuaAndroid();
}
void Room::UnInit()
@ -176,6 +176,7 @@ void Room::AddPlayer(Player* hum)
human_hash_[hum->entity_uniid] = hum;
++alive_count_;
grid_service.AddHuman(hum);
hum->FindLocation();
hum->RefreshView();
MatchTeam(hum);
}
@ -215,6 +216,7 @@ void Room::ShuaAndroid()
human_hash_[hum->entity_uniid] = hum;
++alive_count_;
grid_service.AddHuman(hum);
hum->FindLocation();
hum->RefreshView();
}
}