2021-12-05 17:54:36 +00:00
|
|
|
#include "ProximityMonitorComponent.h"
|
|
|
|
#include "GameMessages.h"
|
|
|
|
#include "dZoneManager.h"
|
|
|
|
#include "ControllablePhysicsComponent.h"
|
|
|
|
#include "EntityManager.h"
|
|
|
|
#include "SimplePhysicsComponent.h"
|
|
|
|
|
2024-04-05 05:52:26 +00:00
|
|
|
const std::unordered_set<LWOOBJID> ProximityMonitorComponent::m_EmptyObjectSet = {};
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
ProximityMonitorComponent::ProximityMonitorComponent(Entity* parent, int radiusSmall, int radiusLarge) : Component(parent) {
|
|
|
|
if (radiusSmall != -1 && radiusLarge != -1) {
|
|
|
|
SetProximityRadius(radiusSmall, "rocketSmall");
|
|
|
|
SetProximityRadius(radiusLarge, "rocketLarge");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ProximityMonitorComponent::~ProximityMonitorComponent() {
|
|
|
|
for (const auto& en : m_ProximitiesData) {
|
|
|
|
if (!en.second) continue;
|
|
|
|
|
2024-01-19 21:12:05 +00:00
|
|
|
dpWorld::RemoveEntity(en.second);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_ProximitiesData.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProximityMonitorComponent::SetProximityRadius(float proxRadius, const std::string& name) {
|
|
|
|
dpEntity* en = new dpEntity(m_Parent->GetObjectID(), proxRadius);
|
|
|
|
en->SetPosition(m_Parent->GetPosition());
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-01-19 21:12:05 +00:00
|
|
|
dpWorld::AddEntity(en);
|
2021-12-05 17:54:36 +00:00
|
|
|
m_ProximitiesData.insert(std::make_pair(name, en));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProximityMonitorComponent::SetProximityRadius(dpEntity* entity, const std::string& name) {
|
2024-01-19 21:12:05 +00:00
|
|
|
dpWorld::AddEntity(entity);
|
2021-12-05 17:54:36 +00:00
|
|
|
entity->SetPosition(m_Parent->GetPosition());
|
|
|
|
m_ProximitiesData.insert(std::make_pair(name, entity));
|
|
|
|
}
|
|
|
|
|
2024-12-08 22:27:04 +00:00
|
|
|
const std::unordered_set<LWOOBJID>& ProximityMonitorComponent::GetProximityObjects(const std::string& name) const {
|
2024-04-05 05:52:26 +00:00
|
|
|
const auto iter = m_ProximitiesData.find(name);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-04-05 05:52:26 +00:00
|
|
|
if (iter == m_ProximitiesData.cend()) {
|
|
|
|
return m_EmptyObjectSet;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return iter->second->GetCurrentlyCollidingObjects();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProximityMonitorComponent::IsInProximity(const std::string& name, LWOOBJID objectID) {
|
2024-04-05 05:52:26 +00:00
|
|
|
const auto iter = m_ProximitiesData.find(name);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-04-05 05:52:26 +00:00
|
|
|
if (iter == m_ProximitiesData.cend()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-04-05 05:52:26 +00:00
|
|
|
const auto& collisions = iter->second->GetCurrentlyCollidingObjects();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-04-05 05:52:26 +00:00
|
|
|
return collisions.contains(objectID);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProximityMonitorComponent::Update(float deltaTime) {
|
|
|
|
for (const auto& prox : m_ProximitiesData) {
|
|
|
|
if (!prox.second) continue;
|
|
|
|
|
2024-03-27 02:06:22 +00:00
|
|
|
prox.second->SetPosition(m_Parent->GetPosition());
|
2021-12-05 17:54:36 +00:00
|
|
|
//Process enter events
|
2024-04-05 05:52:26 +00:00
|
|
|
for (const auto id : prox.second->GetNewObjects()) {
|
|
|
|
m_Parent->OnCollisionProximity(id, prox.first, "ENTER");
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Process exit events
|
2024-04-05 05:52:26 +00:00
|
|
|
for (const auto id : prox.second->GetRemovedObjects()) {
|
|
|
|
m_Parent->OnCollisionProximity(id, prox.first, "LEAVE");
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|