game2001/server/gameserver/movement.cc
aozhiwei e7915dbe7d 1
2019-06-11 14:07:16 +08:00

184 lines
6.7 KiB
C++

#include "precompile.h"
#include "movement.h"
#include "bullet.h"
#include "human.h"
#include "room.h"
#include "metadata.h"
#include "collider.h"
#include "collision.h"
#include "app.h"
void MovementComponent::RayDetection()
{
App::Instance()->perf.ray_times++;
long long tick = a8::XGetTickCount();
Clear();
if (owner->entity_type == ET_Bullet) {
Bullet* bullet = (Bullet*)owner;
} 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]++;
}
}
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;
}
}
}//end for
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;
}
void MovementComponent::Clear()
{
passed_distance = 0.0f;
target_distance = 0.0f;
start_point = Vector2D();
target_point = Vector2D();
detection_objects.clear();
}
void MovementComponent::GetCollisionObjects(std::set<Entity*>& objects)
{
if (owner->entity_type == ET_Bullet) {
Bullet* bullet = (Bullet*)owner;
for (Entity* entity : detection_objects) {
switch (entity->entity_type) {
case ET_Obstacle:
case ET_Building:
{
if (bullet->TestCollision(entity)) {
objects.insert(entity);
}
}
break;
default:
{
}
break;
}
}
}
}
bool MovementComponent::TestCollision()
{
if (owner->entity_type == ET_Bullet) {
Bullet* bullet = (Bullet*)owner;
for (Entity* entity : detection_objects) {
switch (entity->entity_type) {
case ET_Obstacle:
case ET_Building:
{
if (bullet->TestCollision(entity)) {
return true;
}
}
break;
default:
{
}
break;
}
}
} else if (owner->entity_type == ET_Player) {
Human* hum = (Human*)owner;
if (hum->room->OverBorder(hum->pos, hum->meta->i->radius())){
return true;
}
if (a8::HasBitFlag(hum->status, HS_Jump)) {
return false;
}
for (Entity* entity : detection_objects) {
switch (entity->entity_type) {
case ET_Obstacle:
case ET_Building:
{
if (!entity->dead && hum->TestCollision(entity)) {
if (hum->last_collision_door != entity) {
return true;
}
}
}
break;
default:
{
}
break;
}
}
}
return false;
}