96 lines
2.2 KiB
C++
96 lines
2.2 KiB
C++
#include "precompile.h"
|
|
|
|
#include "entity.h"
|
|
#include "collider.h"
|
|
#include "room.h"
|
|
#include "building.h"
|
|
|
|
Entity::Entity()
|
|
{
|
|
|
|
}
|
|
|
|
Entity::~Entity()
|
|
{
|
|
ClearColliders();
|
|
}
|
|
|
|
void Entity::Initialize()
|
|
{
|
|
xtimer_attacher.xtimer = &room->xtimer;
|
|
}
|
|
|
|
ColliderComponent* Entity::GetBoxBound()
|
|
{
|
|
CircleCollider* collider = new CircleCollider();
|
|
collider->active = true;
|
|
collider->owner = this;
|
|
return collider;
|
|
}
|
|
|
|
bool Entity::TestCollision(Entity* b)
|
|
{
|
|
for (auto& a_collider : colliders) {
|
|
for (auto& b_collider : b->colliders) {
|
|
if (a_collider->Intersect(b_collider)) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void Entity::ClearColliders()
|
|
{
|
|
for (auto& itr : colliders) {
|
|
ColliderComponent* collider = itr;
|
|
DestoryCollider(collider);
|
|
}
|
|
colliders.clear();
|
|
}
|
|
|
|
void Entity::FindLocationWithTarget(Entity* target)
|
|
{
|
|
ColliderComponent* a_collider = GetBoxBound();
|
|
Vector2D old_pos = pos;
|
|
Vector2D new_pos = pos;
|
|
ColliderComponent* target_collider = target->GetBoxBound();
|
|
{
|
|
bool ret = a_collider->CalcSafePoint(target_collider, new_pos);
|
|
if (!ret) {
|
|
abort();
|
|
}
|
|
}
|
|
Vector2D new_pos_dir = new_pos - old_pos;
|
|
float distance = (new_pos - old_pos).Norm();
|
|
new_pos_dir.Normalize();
|
|
for (float i = distance; i < 10000000; i += 5.0f) {
|
|
pos = old_pos + new_pos_dir * i;
|
|
std::vector<Entity*> objects;
|
|
int detection_flags = 0;
|
|
{
|
|
a8::SetBitFlag(detection_flags, ET_Obstacle);
|
|
}
|
|
room->CollisionDetection(this, detection_flags, objects);
|
|
if (objects.empty()) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
DestoryCollider(target_collider);
|
|
DestoryCollider(a_collider);
|
|
}
|
|
|
|
void Entity::BroadcastFullState()
|
|
{
|
|
#if 0
|
|
room->TouchPlayerList(a8::XParams()
|
|
.SetSender(obstacle),
|
|
[] (Player* hum, a8::XParams& param)
|
|
{
|
|
Obstacle* obstacle = (Obstacle*)param.sender.GetUserData();
|
|
hum->AddToNewObjects(obstacle);
|
|
});
|
|
#endif
|
|
}
|