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"
|
2024-11-26 04:55:50 +00:00
|
|
|
#include "DestroyableComponent.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
std::vector<SwitchComponent*> SwitchComponent::petSwitches;
|
|
|
|
|
|
|
|
SwitchComponent::SwitchComponent(Entity* parent) : Component(parent) {
|
|
|
|
m_Active = false;
|
|
|
|
|
|
|
|
m_ResetTime = m_Parent->GetVarAs<int32_t>(u"switch_reset_time");
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-12-29 04:24:30 +00:00
|
|
|
m_QuickBuild = m_Parent->GetComponent<QuickBuildComponent>();
|
2024-11-26 04:55:50 +00:00
|
|
|
|
|
|
|
const auto factions = GeneralUtils::SplitString(m_Parent->GetVar<std::u16string>(u"respond_to_faction"), u':');
|
|
|
|
for (const auto& faction : factions) {
|
|
|
|
auto factionID = GeneralUtils::TryParse<int32_t>(GeneralUtils::UTF16ToWTF8(faction));
|
|
|
|
if (!factionID) continue;
|
|
|
|
m_FactionsToRespondTo.push_back(factionID.value());
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SwitchComponent::~SwitchComponent() {
|
|
|
|
const auto& iterator = std::find(petSwitches.begin(), petSwitches.end(), this);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (iterator != petSwitches.end()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
petSwitches.erase(iterator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
void SwitchComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) {
|
|
|
|
outBitStream.Write(m_Active);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2024-11-26 04:55:50 +00:00
|
|
|
void SwitchComponent::OnUse(Entity* originator) {
|
|
|
|
const auto* const destroyableComponent = originator->GetComponent<DestroyableComponent>();
|
|
|
|
if (!destroyableComponent) return;
|
|
|
|
for (const auto faction : m_FactionsToRespondTo) {
|
|
|
|
if (destroyableComponent->HasFaction(faction)) {
|
|
|
|
EntityEnter(originator);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
void SwitchComponent::SetActive(bool active) {
|
|
|
|
m_Active = active;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (m_PetBouncer != nullptr) {
|
2021-12-05 17:54:36 +00:00
|
|
|
m_PetBouncer->SetPetBouncerEnabled(active);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
bool SwitchComponent::GetActive() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return m_Active;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwitchComponent::EntityEnter(Entity* entity) {
|
|
|
|
if (!m_Active) {
|
2023-12-29 04:24:30 +00:00
|
|
|
if (m_QuickBuild) {
|
|
|
|
if (m_QuickBuild->GetState() != eQuickBuildState::COMPLETED) return;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
m_Active = true;
|
|
|
|
if (!m_Parent) return;
|
2023-03-25 10:26:39 +00:00
|
|
|
m_Parent->TriggerEvent(eTriggerEventType::ACTIVATED, entity);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
const auto grpName = m_Parent->GetVarAsString(u"grp_name");
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!grpName.empty()) {
|
2023-07-15 20:56:33 +00:00
|
|
|
const auto entities = Game::entityManager->GetEntitiesInGroup(grpName);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto* entity : entities) {
|
2021-12-05 17:54:36 +00:00
|
|
|
entity->OnFireEventServerSide(entity, "OnActivated");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Timer = m_ResetTime;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (m_PetBouncer != nullptr) {
|
2021-12-05 17:54:36 +00:00
|
|
|
GameMessages::SendPlayFXEffect(m_Parent->GetObjectID(), 2602, u"pettriggeractive", "BounceEffect", LWOOBJID_EMPTY, 1, 1, true);
|
2023-03-20 13:10:52 +00:00
|
|
|
RenderComponent::PlayAnimation(m_Parent, u"engaged");
|
2021-12-05 17:54:36 +00:00
|
|
|
m_PetBouncer->SetPetBouncerEnabled(true);
|
2022-07-28 13:39:57 +00:00
|
|
|
} else {
|
2023-07-15 20:56:33 +00:00
|
|
|
Game::entityManager->SerializeEntity(m_Parent);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwitchComponent::EntityLeave(Entity* entity) {
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SwitchComponent::Update(float deltaTime) {
|
|
|
|
if (m_Active) {
|
|
|
|
m_Timer -= deltaTime;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (m_Timer <= 0.0f) {
|
|
|
|
m_Active = false;
|
|
|
|
if (!m_Parent) return;
|
2023-03-25 10:26:39 +00:00
|
|
|
m_Parent->TriggerEvent(eTriggerEventType::DEACTIVATED, m_Parent);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto grpName = m_Parent->GetVarAsString(u"grp_name");
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!grpName.empty()) {
|
2023-07-15 20:56:33 +00:00
|
|
|
const auto entities = Game::entityManager->GetEntitiesInGroup(grpName);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto* entity : entities) {
|
2021-12-05 17:54:36 +00:00
|
|
|
entity->OnFireEventServerSide(entity, "OnDectivated");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (m_PetBouncer != nullptr) {
|
2021-12-05 17:54:36 +00:00
|
|
|
m_PetBouncer->SetPetBouncerEnabled(false);
|
2022-07-28 13:39:57 +00:00
|
|
|
} else {
|
2023-07-15 20:56:33 +00:00
|
|
|
Game::entityManager->SerializeEntity(m_Parent);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Entity* SwitchComponent::GetParentEntity() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return m_Parent;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
SwitchComponent* SwitchComponent::GetClosestSwitch(NiPoint3 position) {
|
2021-12-05 17:54:36 +00:00
|
|
|
float closestDistance = 0;
|
|
|
|
SwitchComponent* closest = nullptr;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (SwitchComponent* petSwitch : petSwitches) {
|
2021-12-05 17:54:36 +00:00
|
|
|
float distance = Vector3::DistanceSquared(petSwitch->m_Parent->GetPosition(), position);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
|
|
|
if (closest == nullptr || distance < closestDistance) {
|
2021-12-05 17:54:36 +00:00
|
|
|
closestDistance = distance;
|
|
|
|
closest = petSwitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return closest;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void SwitchComponent::SetPetBouncer(BouncerComponent* value) {
|
2021-12-05 17:54:36 +00:00
|
|
|
m_PetBouncer = value;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
|
|
|
if (value != nullptr) {
|
2021-12-05 17:54:36 +00:00
|
|
|
m_PetBouncer->SetPetEnabled(true);
|
|
|
|
petSwitches.push_back(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
BouncerComponent* SwitchComponent::GetPetBouncer() const {
|
2021-12-05 17:54:36 +00:00
|
|
|
return m_PetBouncer;
|
|
|
|
}
|