2021-12-05 17:54:36 +00:00
|
|
|
#include "SwitchComponent.h"
|
|
|
|
#include "EntityManager.h"
|
2023-02-10 08:29:53 +00:00
|
|
|
#include "eTriggerEventType.h"
|
2023-03-20 13:10:52 +00:00
|
|
|
#include "RenderComponent.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
std::vector<SwitchComponent*> SwitchComponent::switches;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
SwitchComponent::SwitchComponent(Entity* parent) : Component(parent) {
|
|
|
|
m_Active = false;
|
2023-07-07 19:13:26 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
void SwitchComponent::Startup() {
|
2023-06-09 22:12:57 +00:00
|
|
|
m_Rebuild = m_ParentEntity->GetComponent<QuickBuildComponent>();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
void SwitchComponent::LoadConfigData() {
|
|
|
|
m_ResetTime = m_ParentEntity->GetVarAs<int32_t>(u"switch_reset_time");
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
SwitchComponent::~SwitchComponent() {
|
|
|
|
switches.erase(std::remove(switches.begin(), switches.end(), this), switches.end());
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SwitchComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) {
|
|
|
|
outBitStream->Write(m_Active);
|
|
|
|
}
|
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
void SwitchComponent::SetActive(const bool active) {
|
|
|
|
if (m_Active == active) return;
|
2021-12-05 17:54:36 +00:00
|
|
|
m_Active = active;
|
2023-07-07 19:13:26 +00:00
|
|
|
if (m_Bouncer) m_Bouncer->SetBouncerEnabled(active);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SwitchComponent::EntityEnter(Entity* entity) {
|
2023-07-07 19:13:26 +00:00
|
|
|
if (m_Active) return;
|
|
|
|
if (m_Rebuild) {
|
|
|
|
if (m_Rebuild->GetState() != eRebuildState::COMPLETED) return;
|
|
|
|
}
|
|
|
|
m_Active = true;
|
|
|
|
if (!m_ParentEntity) return;
|
|
|
|
m_ParentEntity->TriggerEvent(eTriggerEventType::ACTIVATED, entity);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
const auto grpName = m_ParentEntity->GetVarAsString(u"grp_name");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
if (!grpName.empty()) {
|
|
|
|
const auto entities = EntityManager::Instance()->GetEntitiesInGroup(grpName);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
for (auto* entity : entities) {
|
|
|
|
entity->OnFireEventServerSide(entity, "OnActivated");
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2023-07-07 19:13:26 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
m_Timer = m_ResetTime;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
if (m_Bouncer) {
|
|
|
|
GameMessages::SendPlayFXEffect(m_ParentEntity->GetObjectID(), 2602, u"pettriggeractive", "BounceEffect", LWOOBJID_EMPTY, 1, 1, true);
|
|
|
|
RenderComponent::PlayAnimation(m_ParentEntity, u"engaged");
|
|
|
|
m_Bouncer->SetBouncerEnabled(true);
|
|
|
|
} else {
|
|
|
|
EntityManager::Instance()->SerializeEntity(m_ParentEntity);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
void SwitchComponent::Update(const float deltaTime) {
|
|
|
|
if (!m_Active) return;
|
|
|
|
m_Timer -= deltaTime;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
if (m_Timer > 0.0f) return;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
m_Active = false;
|
|
|
|
if (!m_ParentEntity) return;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
m_ParentEntity->TriggerEvent(eTriggerEventType::DEACTIVATED, m_ParentEntity);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
const auto grpName = m_ParentEntity->GetVarAsString(u"grp_name");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
if (!grpName.empty()) {
|
|
|
|
const auto entities = EntityManager::Instance()->GetEntitiesInGroup(grpName);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
for (auto* entity : entities) {
|
|
|
|
entity->OnFireEventServerSide(entity, "OnDectivated");
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
if (m_Bouncer) {
|
|
|
|
m_Bouncer->SetBouncerEnabled(false);
|
|
|
|
} else {
|
|
|
|
EntityManager::Instance()->SerializeEntity(m_ParentEntity);
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
SwitchComponent* SwitchComponent::GetClosestSwitch(const NiPoint3& position) {
|
|
|
|
float closestDistance = 0.0f;
|
2021-12-05 17:54:36 +00:00
|
|
|
SwitchComponent* closest = nullptr;
|
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
for (auto* petSwitch : switches) {
|
2023-06-09 09:46:01 +00:00
|
|
|
float distance = Vector3::DistanceSquared(petSwitch->m_ParentEntity->GetPosition(), position);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
if (closest && distance >= closestDistance) continue;
|
|
|
|
closestDistance = distance;
|
|
|
|
closest = petSwitch;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return closest;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SwitchComponent::SetPetBouncer(BouncerComponent* value) {
|
2023-07-07 19:13:26 +00:00
|
|
|
m_Bouncer = value;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-07-07 19:13:26 +00:00
|
|
|
if (!value) return;
|
|
|
|
m_Bouncer->SetBounceOnCollision(true);
|
|
|
|
EntityManager::Instance()->SerializeEntity(m_Bouncer->GetParentEntity());
|
|
|
|
switches.push_back(this);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|