mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-08-09 20:24:16 +00:00
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:
@@ -1,7 +1,9 @@
|
||||
#include "RenderComponent.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <iomanip>
|
||||
|
||||
#include "Entity.h"
|
||||
@@ -14,8 +16,7 @@
|
||||
|
||||
std::unordered_map<int32_t, float> RenderComponent::m_DurationCache{};
|
||||
|
||||
RenderComponent::RenderComponent(Entity* parent, int32_t componentId): Component(parent) {
|
||||
m_Effects = std::vector<Effect*>();
|
||||
RenderComponent::RenderComponent(Entity* const parentEntity, const int32_t componentId) : Component{ parentEntity } {
|
||||
m_LastAnimationName = "";
|
||||
if (componentId == -1) return;
|
||||
|
||||
@@ -42,100 +43,51 @@ RenderComponent::RenderComponent(Entity* parent, int32_t componentId): Component
|
||||
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) {
|
||||
if (!bIsInitialUpdate) return;
|
||||
|
||||
outBitStream->Write<uint32_t>(m_Effects.size());
|
||||
|
||||
for (Effect* eff : m_Effects) {
|
||||
// we still need to write 0 as the size for name if it is a nullptr
|
||||
if (!eff) {
|
||||
outBitStream->Write<uint8_t>(0);
|
||||
continue;
|
||||
}
|
||||
|
||||
outBitStream->Write<uint8_t>(eff->name.size());
|
||||
for (auto& eff : m_Effects) {
|
||||
outBitStream->Write<uint8_t>(eff.name.size());
|
||||
// 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());
|
||||
for (const auto& value : eff->type) outBitStream->Write<uint16_t>(value);
|
||||
outBitStream->Write<uint8_t>(eff.type.size());
|
||||
for (const auto& value : eff.type) outBitStream->Write<uint16_t>(value);
|
||||
|
||||
outBitStream->Write<float_t>(eff->priority);
|
||||
outBitStream->Write<int64_t>(eff->secondary);
|
||||
outBitStream->Write<float_t>(eff.priority);
|
||||
outBitStream->Write<int64_t>(eff.secondary);
|
||||
}
|
||||
}
|
||||
|
||||
Effect* RenderComponent::AddEffect(const int32_t effectId, const std::string& name, const std::u16string& type, const float priority) {
|
||||
auto* eff = new Effect();
|
||||
|
||||
eff->effectID = effectId;
|
||||
eff->name = name;
|
||||
eff->type = type;
|
||||
eff->priority = priority;
|
||||
m_Effects.push_back(eff);
|
||||
|
||||
return eff;
|
||||
Effect& RenderComponent::AddEffect(const int32_t effectId, const std::string& name, const std::u16string& type, const float priority) {
|
||||
return m_Effects.emplace_back(Effect{ effectId, name, type, priority });
|
||||
}
|
||||
|
||||
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) {
|
||||
auto* eff = m_Effects[i];
|
||||
const auto effectToRemove = std::ranges::find_if(m_Effects, [&name](auto&& effect) { return effect.name == name; });
|
||||
if (effectToRemove == m_Effects.end()) return; // Return early if effect is not present
|
||||
|
||||
if (eff->name == name) {
|
||||
index = i;
|
||||
|
||||
delete eff;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (index == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_Effects.erase(m_Effects.begin() + index);
|
||||
const auto lastEffect = m_Effects.rbegin();
|
||||
*effectToRemove = std::move(*lastEffect); // Move-overwrite
|
||||
m_Effects.pop_back();
|
||||
}
|
||||
|
||||
void RenderComponent::Update(const float deltaTime) {
|
||||
std::vector<Effect*> dead;
|
||||
void RenderComponent::Update(const float deltaTime) {
|
||||
for (auto& effect : m_Effects) {
|
||||
if (effect.time == 0) continue; // Skip persistent effects
|
||||
|
||||
for (auto* effect : m_Effects) {
|
||||
if (effect->time == 0) {
|
||||
continue; // Skip persistent effects
|
||||
}
|
||||
const auto result = effect.time - deltaTime;
|
||||
if (result <= 0) continue;
|
||||
|
||||
const auto result = effect->time - deltaTime;
|
||||
|
||||
if (result <= 0) {
|
||||
dead.push_back(effect);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
effect->time = result;
|
||||
}
|
||||
|
||||
for (auto* effect : dead) {
|
||||
// StopEffect(effect->name);
|
||||
effect.time = result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
auto* effect = AddEffect(effectId, name, effectType, priority);
|
||||
auto& effect = AddEffect(effectId, name, effectType, priority);
|
||||
|
||||
const auto& pair = m_DurationCache.find(effectId);
|
||||
|
||||
if (pair != m_DurationCache.end()) {
|
||||
effect->time = pair->second;
|
||||
effect.time = pair->second;
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -168,16 +120,16 @@ void RenderComponent::PlayEffect(const int32_t effectId, const std::u16string& e
|
||||
|
||||
m_DurationCache[effectId] = 0;
|
||||
|
||||
effect->time = 0; // Persistent effect
|
||||
effect.time = 0; // Persistent effect
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
effect->time = static_cast<float>(result.getFloatField(0));
|
||||
effect.time = static_cast<float>(result.getFloatField(0));
|
||||
|
||||
result.finalize();
|
||||
|
||||
m_DurationCache[effectId] = effect->time;
|
||||
m_DurationCache[effectId] = effect.time;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
std::vector<Effect*>& RenderComponent::GetEffects() {
|
||||
return m_Effects;
|
||||
}
|
||||
|
||||
|
||||
float RenderComponent::PlayAnimation(Entity* self, const std::u16string& animation, float priority, float scale) {
|
||||
if (!self) return 0.0f;
|
||||
return RenderComponent::PlayAnimation(self, GeneralUtils::UTF16ToWTF8(animation), priority, scale);
|
||||
|
Reference in New Issue
Block a user