This commit is contained in:
aozhiwei 2019-03-26 15:32:35 +08:00
commit 24c8d74094
9 changed files with 96 additions and 32 deletions

View File

@ -83,7 +83,7 @@ bool IntersectAabbCircle(Vector2D a_min, Vector2D a_max, Vector2D b_pos, float b
b_pos.y >= a_min.y && b_pos.y <= a_max.y) { b_pos.y >= a_min.y && b_pos.y <= a_max.y) {
return true; return true;
} }
Vector2D nearest_point(a8::Clamp(b_pos.x, a_min.x, a_max.y), Vector2D nearest_point(a8::Clamp(b_pos.x, a_min.x, a_max.x),
a8::Clamp(b_pos.y, a_min.y, a_max.y)); a8::Clamp(b_pos.y, a_min.y, a_max.y));
Vector2D i = b_pos - nearest_point; Vector2D i = b_pos - nearest_point;
float n = a8::LengthSqr(i); float n = a8::LengthSqr(i);

View File

@ -33,9 +33,9 @@ bool Entity::TestCollision(Entity* b)
for (auto& a_collider : colliders) { for (auto& a_collider : colliders) {
for (auto& b_collider : b->colliders) { for (auto& b_collider : b->colliders) {
if (a_collider->Intersect(b_collider)) { if (a_collider->Intersect(b_collider)) {
return false;
}
}
}
return true; return true;
}
}
}
return false;
} }

View File

@ -144,8 +144,19 @@ bool Human::IsCollision()
return !objects.empty(); return !objects.empty();
} }
ColliderComponent* Human::GetFirstCollision()
{
int detection_flags = 0;
a8::SetBitFlag(detection_flags, ET_Obstacle);
a8::SetBitFlag(detection_flags, ET_Building);
std::vector<Entity*> objects;
room->CollisionDetection(this, detection_flags, objects);
return objects.empty() ? *objects[0]->colliders.begin() : nullptr;
}
void Human::FindPath() void Human::FindPath()
{ {
ColliderComponent* first_collider = nullptr;
Vector2D old_pos = pos; Vector2D old_pos = pos;
{ {
float up_dot = Vector2D::UP.Dot(move_dir); float up_dot = Vector2D::UP.Dot(move_dir);

View File

@ -59,6 +59,7 @@ class Human : public Entity
void Shot(Vector2D& target_dir); void Shot(Vector2D& target_dir);
void RecalcSelfCollider(); void RecalcSelfCollider();
bool IsCollision(); bool IsCollision();
ColliderComponent* GetFirstCollision();
void FindPath(); void FindPath();
float GetRadius(); float GetRadius();

View File

@ -22,13 +22,41 @@ void Obstacle::Initialize()
void Obstacle::RecalcSelfCollider() void Obstacle::RecalcSelfCollider()
{ {
#if 0 if (meta->i->thing_id() == 61001) {
if (!self_collider2_) {
self_collider2_ = new AabbCollider();
self_collider2_->owner = this;
colliders.push_back(self_collider2_);
}
self_collider2_->_min = Vector2D(-16.0f, -16.0f);
self_collider2_->_max = Vector2D(16.0f, 16.0f);
} else if (meta->i->thing_id() == 61007) {
if (!self_collider_) { if (!self_collider_) {
self_collider_ = new CircleCollider(); self_collider_ = new CircleCollider();
self_collider_->owner = this; self_collider_->owner = this;
colliders.push_back(self_collider_); colliders.push_back(self_collider_);
} }
self_collider_->pos = Vector2D(); self_collider_->pos = Vector2D();
self_collider_->rad = gun_meta->i->bullet_rad(); self_collider_->rad = 32 / 2.0;
#endif }
}
void Obstacle::FillMFObjectPart(cs::MFObjectPart* part_data)
{
part_data->set_object_type(ET_Obstacle);
cs::MFObstaclePart* p = part_data->mutable_union_obj_2();
p->set_obj_uniid(entity_uniid);
pos.ToPB(p->mutable_pos());
p->set_scale(1.0f);
}
void Obstacle::FillMFObjectFull(cs::MFObjectFull* full_data)
{
full_data->set_object_type(ET_Obstacle);
cs::MFObstacleFull* p = full_data->mutable_union_obj_2();
p->set_obj_uniid(entity_uniid);
pos.ToPB(p->mutable_pos());
p->set_scale(1.0f);
p->set_obstacle_id(meta->i->thing_id());
} }

View File

@ -11,6 +11,7 @@ namespace MetaData
class Human; class Human;
class CircleCollider; class CircleCollider;
class AabbCollider;
class Obstacle : public Entity class Obstacle : public Entity
{ {
public: public:
@ -20,8 +21,11 @@ class Obstacle : public Entity
virtual ~Obstacle() override; virtual ~Obstacle() override;
virtual void Initialize() override; virtual void Initialize() override;
void RecalcSelfCollider(); void RecalcSelfCollider();
virtual void FillMFObjectPart(cs::MFObjectPart* part_data) override;
virtual void FillMFObjectFull(cs::MFObjectFull* full_data) override;
private: private:
CircleCollider* self_collider_ = nullptr; CircleCollider* self_collider_ = nullptr;
AabbCollider* self_collider2_ = nullptr;
}; };

View File

@ -139,18 +139,24 @@ void Room::ShuaAndroid()
void Room::ShuaObstacle(Human* hum) void Room::ShuaObstacle(Human* hum)
{ {
#if 0 MetaData::MapThing* a_thing = MetaMgr::Instance()->GetMapThing(61001);
MetaData::MapThing* thing = MetaMgr::Instance()->GetMapThing(61001); MetaData::MapThing* b_thing = MetaMgr::Instance()->GetMapThing(61007);
if (thing) { if (!a_thing || !b_thing) {
Obstacle* entity = new Obstacle(); return;
entity->room = this; }
entity->meta = thing;
entity->entity_uniid = AllocUniid();
#if 1 #if 1
{ {
Vector2D dir = hum->pos; Obstacle* entity = new Obstacle();
dir.Normalize(); entity->room = this;
} entity->meta = a_thing;
entity->entity_uniid = AllocUniid();
#if 0
entity->dir = Vector2D::UP;
#endif
#if 0
entity->pos = Vector2D(100.0f, 164.0f);
#else
entity->pos = hum->pos - Vector2D::LEFT * hum->meta->i->radius() - 100;
#endif #endif
entity->Initialize(); entity->Initialize();
uniid_hash_[entity->entity_uniid] = entity; uniid_hash_[entity->entity_uniid] = entity;
@ -158,17 +164,30 @@ void Room::ShuaObstacle(Human* hum)
pair.second->new_objects.insert(entity); pair.second->new_objects.insert(entity);
pair.second->part_objects.insert(entity); pair.second->part_objects.insert(entity);
} }
#endif
MetaData::MapThing* a_thing = MetaMgr::Instance()->GetMapThing(61001);
MetaData::MapThing* b_thing = MetaMgr::Instance()->GetMapThing(61007);
if (!a_thing || !b_thing) {
return;
} }
#endif
#if 1
{ {
Obstacle* obj = new Obstacle(); Obstacle* entity = new Obstacle();
obj->entity_uniid = AllocUniid(); entity->room = this;
obj->room = this; entity->meta = b_thing;
entity->entity_uniid = AllocUniid();
#if 0
entity->dir = Vector2D::UP;
#endif
#if 0
entity->pos = Vector2D(100.0f, 100.0f);
#else
entity->pos = hum->pos + Vector2D::RIGHT * hum->meta->i->radius() + 100;
#endif
entity->Initialize();
uniid_hash_[entity->entity_uniid] = entity;
for (auto& pair : human_hash_) {
pair.second->new_objects.insert(entity);
pair.second->part_objects.insert(entity);
} }
}
#endif
} }
bool Room::RandomPos(Human* hum, float distance, Vector2D& out_pos) bool Room::RandomPos(Human* hum, float distance, Vector2D& out_pos)
@ -222,7 +241,7 @@ void Room::AddBullet(Bullet* bullet)
be_added_hash_[bullet->entity_uniid] = bullet; be_added_hash_[bullet->entity_uniid] = bullet;
} }
void Room::CollisionDetection(Entity* sender, int detection_flags, std::vector<Entity*> objects) void Room::CollisionDetection(Entity* sender, int detection_flags, std::vector<Entity*>& objects)
{ {
assert(uniid_hash_.size() < 1000); assert(uniid_hash_.size() < 1000);
for (auto& pair : uniid_hash_) { for (auto& pair : uniid_hash_) {
@ -239,8 +258,8 @@ void Room::CollisionDetection(Entity* sender, int detection_flags, std::vector<E
} }
} }
if (a8::HasBitFlag(detection_flags, ET_Obstacle) && pair.second->entity_type == ET_Obstacle) { if (a8::HasBitFlag(detection_flags, ET_Obstacle) && pair.second->entity_type == ET_Obstacle) {
if (sender->entity_type == ET_Bullet || pair.second->entity_type == ET_Player) { if (sender->entity_type == ET_Bullet || sender->entity_type == ET_Player) {
if (sender->TestCollision(pair.second)) { if (pair.second != sender && sender->TestCollision(pair.second)) {
objects.push_back(pair.second); objects.push_back(pair.second);
} }
} }

View File

@ -48,7 +48,7 @@ public:
void ShuaObstacle(Human* hum); void ShuaObstacle(Human* hum);
bool RandomPos(Human* hum, float distance, Vector2D& out_pos); bool RandomPos(Human* hum, float distance, Vector2D& out_pos);
Human* FindEnemy(Human* hum); Human* FindEnemy(Human* hum);
void CollisionDetection(Entity* sender, int detection_flags, std::vector<Entity*> objects); void CollisionDetection(Entity* sender, int detection_flags, std::vector<Entity*>& objects);
void AddDeletedObject(unsigned short obj_uniid); void AddDeletedObject(unsigned short obj_uniid);
void FillSMJoinedNotify(Player* self_hum, cs::SMJoinedNotify& msg); void FillSMJoinedNotify(Player* self_hum, cs::SMJoinedNotify& msg);
void ResetFrameData(); void ResetFrameData();

View File

@ -52,6 +52,7 @@ void RoomMgr::_CMJoin(f8::MsgHdr& hdr, const cs::CMJoin& msg)
hum->socket_handle = hdr.socket_handle; hum->socket_handle = hdr.socket_handle;
hum->Initialize(); hum->Initialize();
room->AddPlayer(hum); room->AddPlayer(hum);
room->ShuaObstacle(hum);
{ {
cs::SMJoinedNotify notifymsg; cs::SMJoinedNotify notifymsg;