From a5611e9c7fb6c271e7ee050d5c81e0bd62353909 Mon Sep 17 00:00:00 2001 From: David Markowitz Date: Mon, 26 Jun 2023 01:19:49 -0700 Subject: [PATCH] Bouncer cleanup - Correct variable names - Make serialization more efficient and make sense - remove unneeded members - more descriptive logs - Move work to Startup --- dGame/dComponents/BouncerComponent.cpp | 71 ++++++++++---------------- dGame/dComponents/BouncerComponent.h | 32 ++---------- dGame/dComponents/SwitchComponent.cpp | 13 +++-- 3 files changed, 38 insertions(+), 78 deletions(-) diff --git a/dGame/dComponents/BouncerComponent.cpp b/dGame/dComponents/BouncerComponent.cpp index a703f385..df4921e4 100644 --- a/dGame/dComponents/BouncerComponent.cpp +++ b/dGame/dComponents/BouncerComponent.cpp @@ -6,41 +6,34 @@ #include "Game.h" #include "dLogger.h" #include "GameMessages.h" -#include +#include "BitStream.h" #include "eTriggerEventType.h" BouncerComponent::BouncerComponent(Entity* parent) : Component(parent) { - m_PetEnabled = false; - m_PetBouncerEnabled = false; - m_PetSwitchLoaded = false; - - if (parent->GetLOT() == 7625) { - LookupPetSwitch(); - } + m_BounceOnCollision = false; + m_DirtyBounceInfo = false; } -BouncerComponent::~BouncerComponent() { +void BouncerComponent::Startup() { + if (m_ParentEntity->GetLOT() == 7625) LookupPetSwitch(); } void BouncerComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) { - outBitStream->Write(m_PetEnabled); - if (m_PetEnabled) { - outBitStream->Write(m_PetBouncerEnabled); + outBitStream->Write(bIsInitialUpdate || m_DirtyBounceInfo); + if (bIsInitialUpdate || m_DirtyBounceInfo) { + outBitStream->Write(m_BounceOnCollision); + m_DirtyBounceInfo = false; } } -Entity* BouncerComponent::GetParentEntity() const { - return m_ParentEntity; +void BouncerComponent::SetBounceOnCollision(bool value) { + if (m_BounceOnCollision == value) return; + m_BounceOnCollision = value; + m_DirtyBounceInfo = true; } -void BouncerComponent::SetPetEnabled(bool value) { - m_PetEnabled = value; - - EntityManager::Instance()->SerializeEntity(m_ParentEntity); -} - -void BouncerComponent::SetPetBouncerEnabled(bool value) { - m_PetBouncerEnabled = value; +void BouncerComponent::SetBouncerEnabled(bool value) { + m_BounceOnCollision = value; GameMessages::SendBouncerActiveStatus(m_ParentEntity->GetObjectID(), value, UNASSIGNED_SYSTEM_ADDRESS); @@ -48,7 +41,7 @@ void BouncerComponent::SetPetBouncerEnabled(bool value) { if (value) { m_ParentEntity->TriggerEvent(eTriggerEventType::PET_ON_SWITCH, m_ParentEntity); - GameMessages::SendPlayFXEffect(m_ParentEntity->GetObjectID(), 1513, u"create", "PetOnSwitch", LWOOBJID_EMPTY, 1, 1, true); + GameMessages::SendPlayFXEffect(m_ParentEntity->GetObjectID(), 1513, u"create", "PetOnSwitch"); } else { m_ParentEntity->TriggerEvent(eTriggerEventType::PET_OFF_SWITCH, m_ParentEntity); GameMessages::SendStopFXEffect(m_ParentEntity, true, "PetOnSwitch"); @@ -56,14 +49,6 @@ void BouncerComponent::SetPetBouncerEnabled(bool value) { } -bool BouncerComponent::GetPetEnabled() const { - return m_PetEnabled; -} - -bool BouncerComponent::GetPetBouncerEnabled() const { - return m_PetBouncerEnabled; -} - void BouncerComponent::LookupPetSwitch() { const auto& groups = m_ParentEntity->GetGroups(); @@ -73,24 +58,22 @@ void BouncerComponent::LookupPetSwitch() { for (auto* entity : entities) { auto* switchComponent = entity->GetComponent(); - if (switchComponent != nullptr) { - switchComponent->SetPetBouncer(this); + if (!switchComponent) continue; + switchComponent->SetPetBouncer(this); - m_PetSwitchLoaded = true; - m_PetEnabled = true; + m_DirtyBounceInfo = true; - EntityManager::Instance()->SerializeEntity(m_ParentEntity); + EntityManager::Instance()->SerializeEntity(m_ParentEntity); - Game::logger->Log("BouncerComponent", "Loaded pet bouncer"); - } + Game::logger->Log("BouncerComponent", "Loaded bouncer %i:%llu", m_ParentEntity->GetLOT(), m_ParentEntity->GetObjectID()); + return; } } - if (!m_PetSwitchLoaded) { - Game::logger->Log("BouncerComponent", "Failed to load pet bouncer"); + float retryTime = 0.5f; + Game::logger->Log("BouncerComponent", "Failed to load pet bouncer for %i:%llu, trying again in %f seconds", m_ParentEntity->GetLOT(), m_ParentEntity->GetObjectID(), retryTime); - m_ParentEntity->AddCallbackTimer(0.5f, [this]() { - LookupPetSwitch(); - }); - } + m_ParentEntity->AddCallbackTimer(retryTime, [this]() { + LookupPetSwitch(); + }); } diff --git a/dGame/dComponents/BouncerComponent.h b/dGame/dComponents/BouncerComponent.h index 811fe661..6005cc3b 100644 --- a/dGame/dComponents/BouncerComponent.h +++ b/dGame/dComponents/BouncerComponent.h @@ -15,36 +15,22 @@ public: inline static const eReplicaComponentType ComponentType = eReplicaComponentType::BOUNCER; BouncerComponent(Entity* parentEntity); - ~BouncerComponent() override; + void Startup() override; void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags); - Entity* GetParentEntity() const; - /** * Sets whether or not this bouncer needs to be activated by a pet * @param value whether or not this bouncer needs to be activated by a pet */ - void SetPetEnabled(bool value); + void SetBounceOnCollision(bool value); /** * Sets whether or not this bouncer is currently being activated by a pet, allowing entities to bounce off of it, * also displays FX accordingly. * @param value whether or not this bouncer is activated by a pet */ - void SetPetBouncerEnabled(bool value); - - /** - * Gets whether this bouncer should be enabled using pets - * @return whether this bouncer should be enabled using pets - */ - bool GetPetEnabled() const; - - /** - * Gets whether this bouncer is currently activated by a pet - * @return whether this bouncer is currently activated by a pet - */ - bool GetPetBouncerEnabled() const; + void SetBouncerEnabled(bool value); /** * Finds the switch used to activate this bouncer if its pet-enabled and stores this components' state there @@ -52,20 +38,12 @@ public: void LookupPetSwitch(); private: - /** - * Whether this bouncer needs to be activated by a pet - */ - bool m_PetEnabled; - /** * Whether this bouncer is currently being activated by a pet */ - bool m_PetBouncerEnabled; + bool m_BounceOnCollision; - /** - * Whether the pet switch for this bouncer has been located - */ - bool m_PetSwitchLoaded; + bool m_DirtyBounceInfo; }; #endif // BOUNCERCOMPONENT_H diff --git a/dGame/dComponents/SwitchComponent.cpp b/dGame/dComponents/SwitchComponent.cpp index 06f88301..c93f6656 100644 --- a/dGame/dComponents/SwitchComponent.cpp +++ b/dGame/dComponents/SwitchComponent.cpp @@ -28,9 +28,7 @@ void SwitchComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitial void SwitchComponent::SetActive(bool active) { m_Active = active; - if (m_PetBouncer != nullptr) { - m_PetBouncer->SetPetBouncerEnabled(active); - } + if (m_PetBouncer) m_PetBouncer->SetBouncerEnabled(active); } bool SwitchComponent::GetActive() const { @@ -61,7 +59,7 @@ void SwitchComponent::EntityEnter(Entity* entity) { if (m_PetBouncer != nullptr) { GameMessages::SendPlayFXEffect(m_ParentEntity->GetObjectID(), 2602, u"pettriggeractive", "BounceEffect", LWOOBJID_EMPTY, 1, 1, true); RenderComponent::PlayAnimation(m_ParentEntity, u"engaged"); - m_PetBouncer->SetPetBouncerEnabled(true); + m_PetBouncer->SetBouncerEnabled(true); } else { EntityManager::Instance()->SerializeEntity(m_ParentEntity); } @@ -92,8 +90,8 @@ void SwitchComponent::Update(float deltaTime) { } } - if (m_PetBouncer != nullptr) { - m_PetBouncer->SetPetBouncerEnabled(false); + if (m_PetBouncer) { + m_PetBouncer->SetBouncerEnabled(false); } else { EntityManager::Instance()->SerializeEntity(m_ParentEntity); } @@ -126,7 +124,8 @@ void SwitchComponent::SetPetBouncer(BouncerComponent* value) { m_PetBouncer = value; if (value != nullptr) { - m_PetBouncer->SetPetEnabled(true); + m_PetBouncer->SetBounceOnCollision(true); + EntityManager::Instance()->SerializeEntity(m_PetBouncer->GetParentEntity()); petSwitches.push_back(this); } }