chore: Update render component and delete unused code (#1429)

* Update a few components to use smart pointers for memory management

* 'final' keyword added to classes

* removed duplicate 'const'

* removed unused code

* Updated render component to store effects directly in a vector

* Use move instead of copy

* make pointers const

* attribute tags

* nitpicking

* delete default effect constructor

* Added a vector size check to the RemoveEffect() function

* use empty() instead of size()
This commit is contained in:
jadebenn 2024-01-31 08:38:38 -06:00 committed by GitHub
parent d78b50874c
commit b23981e591
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 46 additions and 125 deletions

View File

@ -27,7 +27,6 @@ set(DGAME_DCOMPONENTS_SOURCES
"PlayerForcedMovementComponent.cpp" "PlayerForcedMovementComponent.cpp"
"PossessableComponent.cpp" "PossessableComponent.cpp"
"PossessorComponent.cpp" "PossessorComponent.cpp"
"PropertyComponent.cpp"
"PropertyEntranceComponent.cpp" "PropertyEntranceComponent.cpp"
"PropertyManagementComponent.cpp" "PropertyManagementComponent.cpp"
"PropertyVendorComponent.cpp" "PropertyVendorComponent.cpp"

View File

@ -1,11 +0,0 @@
#include "PropertyComponent.h"
#include "GameMessages.h"
#include "dZoneManager.h"
PropertyComponent::PropertyComponent(Entity* parent) : Component(parent) {
m_PropertyName = parent->GetVar<std::string>(u"propertyName");
m_PropertyState = new PropertyState();
}
PropertyComponent::~PropertyComponent() = default;

View File

@ -1,34 +1,22 @@
/* /*
* Darkflame Universe * Darkflame Universe
* Copyright 2018 * Copyright 2024
*/ */
#ifndef PROPERTYCOMPONENT_H #ifndef PROPERTYCOMPONENT_H
#define PROPERTYCOMPONENT_H #define PROPERTYCOMPONENT_H
#include "BitStream.h"
#include "Entity.h" #include "Entity.h"
#include "Component.h" #include "Component.h"
#include "eReplicaComponentType.h" #include "eReplicaComponentType.h"
struct PropertyState {
LWOOBJID ownerID;
LWOOBJID propertyID;
bool rented;
};
/** /**
* This component is unused and has no functionality * This component is unused and has no functionality
*/ */
class PropertyComponent final : public Component { class PropertyComponent final : public Component {
public: public:
static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY; static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY;
explicit PropertyComponent(Entity* parentEntity); explicit PropertyComponent(Entity* const parentEntity) noexcept : Component{ parentEntity } {}
~PropertyComponent() override;
[[nodiscard]] PropertyState* GetPropertyState() const { return m_PropertyState; };
private:
PropertyState* m_PropertyState;
std::string m_PropertyName;
}; };
#endif // PROPERTYCOMPONENT_H #endif // !PROPERTYCOMPONENT_H

View File

@ -1,7 +1,9 @@
#include "RenderComponent.h" #include "RenderComponent.h"
#include <algorithm>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <utility>
#include <iomanip> #include <iomanip>
#include "Entity.h" #include "Entity.h"
@ -14,8 +16,7 @@
std::unordered_map<int32_t, float> RenderComponent::m_DurationCache{}; std::unordered_map<int32_t, float> RenderComponent::m_DurationCache{};
RenderComponent::RenderComponent(Entity* parent, int32_t componentId): Component(parent) { RenderComponent::RenderComponent(Entity* const parentEntity, const int32_t componentId) : Component{ parentEntity } {
m_Effects = std::vector<Effect*>();
m_LastAnimationName = ""; m_LastAnimationName = "";
if (componentId == -1) return; if (componentId == -1) return;
@ -42,100 +43,51 @@ RenderComponent::RenderComponent(Entity* parent, int32_t componentId): Component
result.finalize(); result.finalize();
} }
RenderComponent::~RenderComponent() {
for (Effect* eff : m_Effects) {
if (eff) {
delete eff;
eff = nullptr;
}
}
m_Effects.clear();
}
void RenderComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate) { void RenderComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate) {
if (!bIsInitialUpdate) return; if (!bIsInitialUpdate) return;
outBitStream->Write<uint32_t>(m_Effects.size()); outBitStream->Write<uint32_t>(m_Effects.size());
for (Effect* eff : m_Effects) { for (auto& eff : m_Effects) {
// we still need to write 0 as the size for name if it is a nullptr outBitStream->Write<uint8_t>(eff.name.size());
if (!eff) {
outBitStream->Write<uint8_t>(0);
continue;
}
outBitStream->Write<uint8_t>(eff->name.size());
// if there is no name, then we don't write anything else // if there is no name, then we don't write anything else
if (eff->name.empty()) continue; if (eff.name.empty()) continue;
for (const auto& value : eff->name) outBitStream->Write<uint8_t>(value); for (const auto& value : eff.name) outBitStream->Write<uint8_t>(value);
outBitStream->Write(eff->effectID); outBitStream->Write(eff.effectID);
outBitStream->Write<uint8_t>(eff->type.size()); outBitStream->Write<uint8_t>(eff.type.size());
for (const auto& value : eff->type) outBitStream->Write<uint16_t>(value); for (const auto& value : eff.type) outBitStream->Write<uint16_t>(value);
outBitStream->Write<float_t>(eff->priority); outBitStream->Write<float_t>(eff.priority);
outBitStream->Write<int64_t>(eff->secondary); outBitStream->Write<int64_t>(eff.secondary);
} }
} }
Effect* RenderComponent::AddEffect(const int32_t effectId, const std::string& name, const std::u16string& type, const float priority) { Effect& RenderComponent::AddEffect(const int32_t effectId, const std::string& name, const std::u16string& type, const float priority) {
auto* eff = new Effect(); return m_Effects.emplace_back(Effect{ effectId, name, type, priority });
eff->effectID = effectId;
eff->name = name;
eff->type = type;
eff->priority = priority;
m_Effects.push_back(eff);
return eff;
} }
void RenderComponent::RemoveEffect(const std::string& name) { void RenderComponent::RemoveEffect(const std::string& name) {
uint32_t index = -1; if (m_Effects.empty()) return;
for (auto i = 0u; i < m_Effects.size(); ++i) { const auto effectToRemove = std::ranges::find_if(m_Effects, [&name](auto&& effect) { return effect.name == name; });
auto* eff = m_Effects[i]; if (effectToRemove == m_Effects.end()) return; // Return early if effect is not present
if (eff->name == name) { const auto lastEffect = m_Effects.rbegin();
index = i; *effectToRemove = std::move(*lastEffect); // Move-overwrite
m_Effects.pop_back();
delete eff;
break;
}
}
if (index == -1) {
return;
}
m_Effects.erase(m_Effects.begin() + index);
} }
void RenderComponent::Update(const float deltaTime) { void RenderComponent::Update(const float deltaTime) {
std::vector<Effect*> dead; for (auto& effect : m_Effects) {
if (effect.time == 0) continue; // Skip persistent effects
for (auto* effect : m_Effects) { const auto result = effect.time - deltaTime;
if (effect->time == 0) { if (result <= 0) continue;
continue; // Skip persistent effects
}
const auto result = effect->time - deltaTime; effect.time = result;
if (result <= 0) {
dead.push_back(effect);
continue;
}
effect->time = result;
}
for (auto* effect : dead) {
// StopEffect(effect->name);
} }
} }
@ -144,12 +96,12 @@ void RenderComponent::PlayEffect(const int32_t effectId, const std::u16string& e
GameMessages::SendPlayFXEffect(m_Parent, effectId, effectType, name, secondary, priority, scale, serialize); GameMessages::SendPlayFXEffect(m_Parent, effectId, effectType, name, secondary, priority, scale, serialize);
auto* effect = AddEffect(effectId, name, effectType, priority); auto& effect = AddEffect(effectId, name, effectType, priority);
const auto& pair = m_DurationCache.find(effectId); const auto& pair = m_DurationCache.find(effectId);
if (pair != m_DurationCache.end()) { if (pair != m_DurationCache.end()) {
effect->time = pair->second; effect.time = pair->second;
return; return;
} }
@ -168,16 +120,16 @@ void RenderComponent::PlayEffect(const int32_t effectId, const std::u16string& e
m_DurationCache[effectId] = 0; m_DurationCache[effectId] = 0;
effect->time = 0; // Persistent effect effect.time = 0; // Persistent effect
return; return;
} }
effect->time = static_cast<float>(result.getFloatField(0)); effect.time = static_cast<float>(result.getFloatField(0));
result.finalize(); result.finalize();
m_DurationCache[effectId] = effect->time; m_DurationCache[effectId] = effect.time;
} }
void RenderComponent::StopEffect(const std::string& name, const bool killImmediate) { void RenderComponent::StopEffect(const std::string& name, const bool killImmediate) {
@ -186,11 +138,6 @@ void RenderComponent::StopEffect(const std::string& name, const bool killImmedia
RemoveEffect(name); RemoveEffect(name);
} }
std::vector<Effect*>& RenderComponent::GetEffects() {
return m_Effects;
}
float RenderComponent::PlayAnimation(Entity* self, const std::u16string& animation, float priority, float scale) { float RenderComponent::PlayAnimation(Entity* self, const std::u16string& animation, float priority, float scale) {
if (!self) return 0.0f; if (!self) return 0.0f;
return RenderComponent::PlayAnimation(self, GeneralUtils::UTF16ToWTF8(animation), priority, scale); return RenderComponent::PlayAnimation(self, GeneralUtils::UTF16ToWTF8(animation), priority, scale);

View File

@ -17,7 +17,12 @@ class Entity;
* here. * here.
*/ */
struct Effect { struct Effect {
Effect() { priority = 1.0f; } explicit Effect(const int32_t effectID, const std::string& name, const std::u16string& type, const float priority = 1.0f) noexcept
: effectID{ effectID }
, name{ name }
, type{ type }
, priority{ priority } {
}
/** /**
* The ID of the effect * The ID of the effect
@ -58,8 +63,7 @@ class RenderComponent final : public Component {
public: public:
static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::RENDER; static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::RENDER;
RenderComponent(Entity* entity, int32_t componentId = -1); RenderComponent(Entity* const parentEntity, const int32_t componentId = -1);
~RenderComponent() override;
void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate) override; void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate) override;
void Update(float deltaTime) override; void Update(float deltaTime) override;
@ -72,7 +76,7 @@ public:
* @param priority the priority of the effect * @param priority the priority of the effect
* @return if successful, the effect that was created * @return if successful, the effect that was created
*/ */
Effect* AddEffect(int32_t effectId, const std::string& name, const std::u16string& type, const float priority); [[maybe_unused]] Effect& AddEffect(const int32_t effectId, const std::string& name, const std::u16string& type, const float priority);
/** /**
* Removes an effect for this entity * Removes an effect for this entity
@ -99,12 +103,6 @@ public:
*/ */
void StopEffect(const std::string& name, bool killImmediate = true); void StopEffect(const std::string& name, bool killImmediate = true);
/**
* Returns the list of currently active effects
* @return
*/
std::vector<Effect*>& GetEffects();
/** /**
* Verifies that an animation can be played on this entity by checking * Verifies that an animation can be played on this entity by checking
* if it has the animation assigned to its group. If it does, the animation is echo'd * if it has the animation assigned to its group. If it does, the animation is echo'd
@ -125,10 +123,10 @@ public:
static float PlayAnimation(Entity* self, const std::u16string& animation, float priority = 0.0f, float scale = 1.0f); static float PlayAnimation(Entity* self, const std::u16string& animation, float priority = 0.0f, float scale = 1.0f);
static float PlayAnimation(Entity* self, const std::string& animation, float priority = 0.0f, float scale = 1.0f); static float PlayAnimation(Entity* self, const std::string& animation, float priority = 0.0f, float scale = 1.0f);
static float GetAnimationTime(Entity* self, const std::string& animation); [[nodiscard]] static float GetAnimationTime(Entity* self, const std::string& animation);
static float GetAnimationTime(Entity* self, const std::u16string& animation); [[nodiscard]] static float GetAnimationTime(Entity* self, const std::u16string& animation);
const std::string& GetLastAnimationName() const { return m_LastAnimationName; }; [[nodiscard]] const std::string& GetLastAnimationName() const { return m_LastAnimationName; };
void SetLastAnimationName(const std::string& name) { m_LastAnimationName = name; }; void SetLastAnimationName(const std::string& name) { m_LastAnimationName = name; };
private: private:
@ -136,7 +134,7 @@ private:
/** /**
* List of currently active effects * List of currently active effects
*/ */
std::vector<Effect*> m_Effects; std::vector<Effect> m_Effects;
std::vector<int32_t> m_animationGroupIds; std::vector<int32_t> m_animationGroupIds;