From e288dc09b53d48e3bfb6d527489ae633f73abdf3 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 11 Jun 2019 17:55:16 +0800 Subject: [PATCH] 1 --- server/gameserver/movement.cc | 151 +++++++++++++++++----------------- server/gameserver/movement.h | 5 ++ 2 files changed, 79 insertions(+), 77 deletions(-) diff --git a/server/gameserver/movement.cc b/server/gameserver/movement.cc index 067dc2f..b5c7637 100644 --- a/server/gameserver/movement.cc +++ b/server/gameserver/movement.cc @@ -8,93 +8,65 @@ #include "collider.h" #include "collision.h" #include "app.h" +#include "obstacle.h" void MovementComponent::RayDetection() { - App::Instance()->perf.ray_times++; - long long tick = a8::XGetTickCount(); Clear(); + App::Instance()->perf.ray_times++; + long long tick = a8::XGetTickCount(); + Vector2D left_p0; + Vector2D left_p1; + Vector2D right_p0; + Vector2D right_p1; if (owner->entity_type == ET_Bullet) { Bullet* bullet = (Bullet*)owner; + Init(bullet->pos, bullet->born_dir, bullet->gun_meta->i->bullet_rad(), MAP_CELL_WIDTH - MAP_GRID_WIDTH * 3, + left_p0, left_p1, right_p0, right_p1); } else if (owner->entity_type == ET_Player) { Human* hum = (Human*)owner; - start_point = hum->pos; - target_point = hum->pos + hum->move_dir * (MAP_CELL_WIDTH - MAP_GRID_WIDTH); - target_distance = (target_point - start_point).Norm(); - { - long long tick1 = a8::XGetTickCount(); - Vector2D left_dir = hum->move_dir; - left_dir.Rotate(-90); - left_dir.Normalize(); - Vector2D right_dir = hum->move_dir; - right_dir.Rotate(+90); - right_dir.Normalize(); - App::Instance()->perf.params[1] += a8::XGetTickCount() - tick1; - - Vector2D left_p0 = hum->pos + left_dir * (hum->meta->i->radius() + 1); - Vector2D left_p1 = left_p0 + hum->move_dir * (MAP_CELL_WIDTH - MAP_GRID_WIDTH * 3); - Vector2D right_p0 = hum->pos + right_dir * (hum->meta->i->radius() + 1); - Vector2D right_p1 = right_p0 + hum->move_dir * (MAP_CELL_WIDTH - MAP_GRID_WIDTH * 3); - - int count = 0; - for (auto& grid : hum->grid_list) { - for (Entity* entity : grid->entity_list) { - count++; - switch (entity->entity_type) { - case ET_Obstacle: - { - if ( - (hum->last_collision_door == nullptr || hum->last_collision_door != entity) - ){ - long long tick2 = a8::XGetTickCount(); - AabbCollider aabb_box; - entity->GetAabbBox(aabb_box); - if (IntersectSegmentAabb(left_p0, left_p1, - aabb_box.owner->pos + aabb_box._min, - aabb_box.owner->pos + aabb_box._max) || - IntersectSegmentAabb(right_p0, right_p1, - aabb_box.owner->pos + aabb_box._min, - aabb_box.owner->pos + aabb_box._max) - ) { - detection_objects.insert(entity); - } - App::Instance()->perf.params[3] += a8::XGetTickCount() - tick2; - App::Instance()->perf.params[4]++; - } + Init(hum->pos, hum->move_dir, hum->meta->i->radius(), MAP_CELL_WIDTH - MAP_GRID_WIDTH * 3, + left_p0, left_p1, right_p0, right_p1); + } + int count = 0; + for (auto& grid : owner->grid_list) { + for (Entity* entity : grid->entity_list) { + count++; + switch (entity->entity_type) { + case ET_Building: + case ET_Obstacle: + { + if (entity->entity_type == ET_Obstacle && ((Obstacle*)entity)->is_door) { + //门直接加入检查列表 + detection_objects.insert(entity); + } else { + AabbCollider aabb_box; + entity->GetAabbBox(aabb_box); + if (IntersectSegmentAabb(left_p0, left_p1, + aabb_box.owner->pos + aabb_box._min, + aabb_box.owner->pos + aabb_box._max) || + IntersectSegmentAabb(right_p0, right_p1, + aabb_box.owner->pos + aabb_box._min, + aabb_box.owner->pos + aabb_box._max) + ) { + detection_objects.insert(entity); } - break; - case ET_Building: - { - long long tick2 = a8::XGetTickCount(); - AabbCollider aabb_box; - entity->GetAabbBox(aabb_box); - if (IntersectSegmentAabb(left_p0, left_p1, - aabb_box.owner->pos + aabb_box._min, - aabb_box.owner->pos + aabb_box._max) || - IntersectSegmentAabb(right_p0, right_p1, - aabb_box.owner->pos + aabb_box._min, - aabb_box.owner->pos + aabb_box._max) - ) { - detection_objects.insert(entity); - } - App::Instance()->perf.params[3] += a8::XGetTickCount() - tick2; - App::Instance()->perf.params[4]++; - } - break; - default: - { - } - break; } + App::Instance()->perf.params[4]++; } - }//end for - if (App::Instance()->perf.params[2] < count) { - App::Instance()->perf.params[2] = count; + break; + default: + { + } + break; } - if (App::Instance()->perf.params[6] < detection_objects.size()) { - App::Instance()->perf.params[6] = detection_objects.size(); - } - } + }//end for entity_list + }//end for grid + if (App::Instance()->perf.params[2] < count) { + App::Instance()->perf.params[2] = count; + } + if (App::Instance()->perf.params[6] < detection_objects.size()) { + App::Instance()->perf.params[6] = detection_objects.size(); } App::Instance()->perf.ray_time += a8::XGetTickCount() - tick; } @@ -117,7 +89,7 @@ void MovementComponent::GetCollisionObjects(std::set& objects) case ET_Obstacle: case ET_Building: { - if (bullet->TestCollision(entity)) { + if (!entity->dead && bullet->TestCollision(entity)) { objects.insert(entity); } } @@ -135,12 +107,16 @@ bool MovementComponent::TestCollision() { if (owner->entity_type == ET_Bullet) { Bullet* bullet = (Bullet*)owner; + if (bullet->room->OverBorder(bullet->pos, bullet->gun_meta->i->bullet_rad())){ + return true; + } + for (Entity* entity : detection_objects) { switch (entity->entity_type) { case ET_Obstacle: case ET_Building: { - if (bullet->TestCollision(entity)) { + if (!entity->dead && bullet->TestCollision(entity)) { return true; } } @@ -181,3 +157,24 @@ bool MovementComponent::TestCollision() } return false; } + + +void MovementComponent::Init(Vector2D pos, Vector2D dir, float rad, float distance, + Vector2D& left_p0, Vector2D& left_p1, Vector2D& right_p0, Vector2D& right_p1) +{ + start_point = pos; + target_point = pos + dir * (MAP_CELL_WIDTH - MAP_GRID_WIDTH); + target_distance = (target_point - start_point).Norm(); + + Vector2D left_dir = dir; + left_dir.Rotate(-90); + left_dir.Normalize(); + Vector2D right_dir = dir; + right_dir.Rotate(+90); + right_dir.Normalize(); + + left_p0 = pos + left_dir * (rad + 1); + left_p1 = left_p0 + dir * distance; + right_p0 = pos + right_dir * (rad + 1); + right_p1 = right_p0 + dir * distance; +} diff --git a/server/gameserver/movement.h b/server/gameserver/movement.h index 3014282..5450101 100644 --- a/server/gameserver/movement.h +++ b/server/gameserver/movement.h @@ -15,4 +15,9 @@ class MovementComponent void Clear(); void GetCollisionObjects(std::set& objects); bool TestCollision(); + +private: + + void Init(Vector2D pos, Vector2D dir, float rad, float distance, + Vector2D& left_p0, Vector2D& left_p1, Vector2D& right_p0, Vector2D& right_p1); };