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
|
|
|
|
|
|
|
std::vector<SwitchComponent*> SwitchComponent::petSwitches;
|
|
|
|
|
|
|
|
SwitchComponent::SwitchComponent(Entity* parent) : Component(parent) {
|
|
|
|
m_Active = false;
|
|
|
|
|
2023-06-09 09:46:01 +00:00
|
|
|
m_ResetTime = m_ParentEntity->GetVarAs<int32_t>(u"switch_reset_time");
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-06-09 22:12:57 +00:00
|
|
|
m_Rebuild = m_ParentEntity->GetComponent<QuickBuildComponent>();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SwitchComponent::~SwitchComponent() {
|
|
|
|
const auto& iterator = std::find(petSwitches.begin(), petSwitches.end(), this);
|
|
|
|
|
|
|
|
if (iterator != petSwitches.end()) {
|
|
|
|
petSwitches.erase(iterator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwitchComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) {
|
|
|
|
outBitStream->Write(m_Active);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwitchComponent::SetActive(bool active) {
|
|
|
|
m_Active = active;
|
|
|
|
|
|
|
|
if (m_PetBouncer != nullptr) {
|
|
|
|
m_PetBouncer->SetPetBouncerEnabled(active);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SwitchComponent::GetActive() const {
|
|
|
|
return m_Active;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwitchComponent::EntityEnter(Entity* entity) {
|
|
|
|
if (!m_Active) {
|
|
|
|
if (m_Rebuild) {
|
2023-05-02 22:39:21 +00:00
|
|
|
if (m_Rebuild->GetState() != eRebuildState::COMPLETED) return;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
m_Active = true;
|
2023-06-09 09:46:01 +00:00
|
|
|
if (!m_ParentEntity) return;
|
|
|
|
m_ParentEntity->TriggerEvent(eTriggerEventType::ACTIVATED, entity);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-06-09 09:46:01 +00:00
|
|
|
const auto grpName = m_ParentEntity->GetVarAsString(u"grp_name");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
if (!grpName.empty()) {
|
|
|
|
const auto entities = EntityManager::Instance()->GetEntitiesInGroup(grpName);
|
|
|
|
|
|
|
|
for (auto* entity : entities) {
|
|
|
|
entity->OnFireEventServerSide(entity, "OnActivated");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Timer = m_ResetTime;
|
|
|
|
|
|
|
|
if (m_PetBouncer != nullptr) {
|
2023-06-09 09:46:01 +00:00
|
|
|
GameMessages::SendPlayFXEffect(m_ParentEntity->GetObjectID(), 2602, u"pettriggeractive", "BounceEffect", LWOOBJID_EMPTY, 1, 1, true);
|
|
|
|
RenderComponent::PlayAnimation(m_ParentEntity, u"engaged");
|
2021-12-05 17:54:36 +00:00
|
|
|
m_PetBouncer->SetPetBouncerEnabled(true);
|
|
|
|
} else {
|
2023-06-09 09:46:01 +00:00
|
|
|
EntityManager::Instance()->SerializeEntity(m_ParentEntity);
|
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;
|
2023-06-09 09:46:01 +00:00
|
|
|
if (!m_ParentEntity) return;
|
|
|
|
m_ParentEntity->TriggerEvent(eTriggerEventType::DEACTIVATED, m_ParentEntity);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-06-09 09:46:01 +00:00
|
|
|
const auto grpName = m_ParentEntity->GetVarAsString(u"grp_name");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
if (!grpName.empty()) {
|
|
|
|
const auto entities = EntityManager::Instance()->GetEntitiesInGroup(grpName);
|
|
|
|
|
|
|
|
for (auto* entity : entities) {
|
|
|
|
entity->OnFireEventServerSide(entity, "OnDectivated");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_PetBouncer != nullptr) {
|
|
|
|
m_PetBouncer->SetPetBouncerEnabled(false);
|
|
|
|
} else {
|
2023-06-09 09:46:01 +00:00
|
|
|
EntityManager::Instance()->SerializeEntity(m_ParentEntity);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Entity* SwitchComponent::GetParentEntity() const {
|
2023-06-09 09:46:01 +00:00
|
|
|
return m_ParentEntity;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SwitchComponent* SwitchComponent::GetClosestSwitch(NiPoint3 position) {
|
|
|
|
float closestDistance = 0;
|
|
|
|
SwitchComponent* closest = nullptr;
|
|
|
|
|
|
|
|
for (SwitchComponent* petSwitch : petSwitches) {
|
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
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (closest == nullptr || distance < closestDistance) {
|
|
|
|
closestDistance = distance;
|
|
|
|
closest = petSwitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return closest;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SwitchComponent::SetPetBouncer(BouncerComponent* value) {
|
|
|
|
m_PetBouncer = value;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (value != nullptr) {
|
|
|
|
m_PetBouncer->SetPetEnabled(true);
|
|
|
|
petSwitches.push_back(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BouncerComponent* SwitchComponent::GetPetBouncer() const {
|
|
|
|
return m_PetBouncer;
|
|
|
|
}
|