133 lines
2.7 KiB
C++
133 lines
2.7 KiB
C++
#include "precompile.h"
|
|
|
|
#include "entity.h"
|
|
#include "collider.h"
|
|
#include "room.h"
|
|
#include "building.h"
|
|
#include "human.h"
|
|
#include "app.h"
|
|
|
|
Entity::Entity()
|
|
{
|
|
|
|
}
|
|
|
|
Entity::~Entity()
|
|
{
|
|
ClearColliders();
|
|
}
|
|
|
|
void Entity::Initialize()
|
|
{
|
|
}
|
|
|
|
void Entity::GetAabbBox(AabbCollider& aabb_box)
|
|
{
|
|
aabb_box.active = true;
|
|
aabb_box.owner = this;
|
|
}
|
|
|
|
void Entity::GetCircleBox(CircleCollider& circle_box)
|
|
{
|
|
circle_box.active = true;
|
|
circle_box.owner = this;
|
|
circle_box.rad = 1;
|
|
}
|
|
|
|
bool Entity::TestCollision(Room* room, Entity* b)
|
|
{
|
|
App::Instance()->perf.test_times++;
|
|
if (b->IsDead(room)) {
|
|
return false;
|
|
}
|
|
OnPreCollision(room);
|
|
b->OnPreCollision(room);
|
|
for (auto& a_collider : colliders) {
|
|
for (auto& b_collider : b->colliders) {
|
|
if (a_collider->Intersect(b_collider)) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool Entity::TestCollision(Room* room,ColliderComponent* b)
|
|
{
|
|
if (b->owner->IsDead(room)) {
|
|
return false;
|
|
}
|
|
OnPreCollision(room);
|
|
b->owner->OnPreCollision(room);
|
|
for (auto& a_collider : colliders) {
|
|
if (a_collider->Intersect(b)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool Entity::TestCollisionEx(Room* room, const a8::Vec2& aabb_pos, AabbCollider& aabb_box)
|
|
{
|
|
OnPreCollision(room);
|
|
if (aabb_box.owner) {
|
|
aabb_box.owner->OnPreCollision(room);
|
|
}
|
|
for (auto& a_collider : colliders) {
|
|
if (a_collider->IntersectEx(aabb_pos, &aabb_box)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void Entity::ClearColliders()
|
|
{
|
|
for (auto& itr : colliders) {
|
|
ColliderComponent* collider = itr;
|
|
DestoryCollider(collider);
|
|
}
|
|
colliders.clear();
|
|
}
|
|
|
|
void Entity::BroadcastFullState(Room* room)
|
|
{
|
|
std::set<GridCell*> grid_list;
|
|
room->grid_service->GetAllCells(room, grid_id, grid_list);
|
|
for (auto& grid : grid_list) {
|
|
for (Human* hum : grid->human_list[room->room_idx]) {
|
|
hum->AddToNewObjects(this);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Entity::BroadcastDeleteState(Room* room)
|
|
{
|
|
std::set<GridCell*> grid_list;
|
|
room->grid_service->GetAllCells(room, grid_id, grid_list);
|
|
for (auto& grid : grid_list) {
|
|
for (Human* hum : grid->human_list[room->room_idx]) {
|
|
hum->RemoveObjects(this);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Entity::AddEntityCollider(ColliderComponent* collider)
|
|
{
|
|
colliders.push_back(collider);
|
|
}
|
|
|
|
bool Entity::IsPermanent()
|
|
{
|
|
if (is_permanent) {
|
|
if (entity_uniid >= FIXED_OBJECT_MAXID) {
|
|
abort();
|
|
}
|
|
} else {
|
|
if (entity_uniid < FIXED_OBJECT_MAXID) {
|
|
abort();
|
|
}
|
|
}
|
|
return is_permanent;
|
|
}
|