Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Update render component and delete unused code #1429

Merged
merged 14 commits into from
Jan 31, 2024
10 changes: 4 additions & 6 deletions dGame/dComponents/PropertyComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
#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(Entity* parentEntity)
: Component{ parentEntity }
, m_PropertyName{ parentEntity->GetVar<std::string>(u"propertyName") }
, m_PropertyState{ std::make_unique<PropertyState>() } {
jadebenn marked this conversation as resolved.
Show resolved Hide resolved
}

PropertyComponent::~PropertyComponent() = default;

9 changes: 4 additions & 5 deletions dGame/dComponents/PropertyComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ struct PropertyState {
/**
* This component is unused and has no functionality
*/
class PropertyComponent : public Component {
class PropertyComponent final : public Component {
public:
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY;
constexpr static eReplicaComponentType ComponentType = eReplicaComponentType::PROPERTY;
explicit PropertyComponent(Entity* parentEntity);
~PropertyComponent() override;
[[nodiscard]] PropertyState* GetPropertyState() const { return m_PropertyState; };
[[nodiscard]] PropertyState* GetPropertyState() const { return m_PropertyState.get(); };
jadebenn marked this conversation as resolved.
Show resolved Hide resolved
private:
PropertyState* m_PropertyState;
std::unique_ptr<PropertyState> m_PropertyState;
std::string m_PropertyName;
};

Expand Down
38 changes: 12 additions & 26 deletions dGame/dComponents/RenderComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

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* parentEntity, const int32_t componentId): Component{ parentEntity } {
m_Effects = std::vector<std::unique_ptr<Effect>>();
m_LastAnimationName = "";
if (componentId == -1) return;

Expand All @@ -42,23 +42,12 @@ 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) {
for (auto& 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);
Expand All @@ -82,27 +71,29 @@ void RenderComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitial
}

Effect* RenderComponent::AddEffect(const int32_t effectId, const std::string& name, const std::u16string& type, const float priority) {
auto* eff = new Effect();
auto eff = std::make_unique<Effect>();

eff->effectID = effectId;
eff->name = name;
eff->type = type;
eff->priority = priority;
m_Effects.push_back(eff);

return eff;
auto* nonOwningPtr = eff.get();
m_Effects.push_back(std::move(eff));

return nonOwningPtr;
}

void RenderComponent::RemoveEffect(const std::string& name) {
uint32_t index = -1;

for (auto i = 0u; i < m_Effects.size(); ++i) {
auto* eff = m_Effects[i];
auto& eff = m_Effects[i];

if (eff->name == name) {
index = i;

delete eff;
eff.reset(); // Delete effect

break;
}
Expand All @@ -118,15 +109,15 @@ void RenderComponent::RemoveEffect(const std::string& name) {
void RenderComponent::Update(const float deltaTime) {
std::vector<Effect*> dead;

for (auto* effect : m_Effects) {
for (auto& effect : m_Effects) {
if (effect->time == 0) {
continue; // Skip persistent effects
}

const auto result = effect->time - deltaTime;

if (result <= 0) {
dead.push_back(effect);
dead.push_back(effect.get());

continue;
}
Expand Down Expand Up @@ -186,11 +177,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);
Expand Down
15 changes: 4 additions & 11 deletions dGame/dComponents/RenderComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ struct Effect {
* Determines that a component should be visibly rendered into the world, most entities have this. This component
* also handles effects that play for entities.
*/
class RenderComponent : public Component {
class RenderComponent final : public Component {
public:
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::RENDER;
constexpr static eReplicaComponentType ComponentType = eReplicaComponentType::RENDER;

RenderComponent(Entity* entity, int32_t componentId = -1);
~RenderComponent() override;
RenderComponent(Entity* parentEntity, const int32_t componentId = -1);

void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate) override;
void Update(float deltaTime) override;
Expand Down Expand Up @@ -99,12 +98,6 @@ class RenderComponent : public Component {
*/
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
* if it has the animation assigned to its group. If it does, the animation is echo'd
Expand Down Expand Up @@ -136,7 +129,7 @@ class RenderComponent : public Component {
/**
* List of currently active effects
*/
std::vector<Effect*> m_Effects;
std::vector<std::unique_ptr<Effect>> m_Effects;

std::vector<int32_t> m_animationGroupIds;

Expand Down
Loading