Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
RiverHillbug committed Mar 2, 2024
1 parent 8125b06 commit 1b18d0f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 18 deletions.
17 changes: 8 additions & 9 deletions Minigin/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void GameObject::Update(const float deltaTime)
{
for (auto& component : components.second)
{
component.get()->Update(deltaTime);
component->Update(deltaTime);
}
}
}
Expand All @@ -58,7 +58,7 @@ void GameObject::SetWorldPosition(const glm::vec2& position)

if (m_pParent)
{
m_LocalTransform.SetPosition(- m_pParent->GetTransform().GetPosition() + m_LocalTransform.GetPosition());
m_LocalTransform.Translate(- m_pParent->GetTransform().GetPosition());
}
}

Expand All @@ -74,7 +74,7 @@ void GameObject::SetLocalPosition(const float x, const float y)
//m_LocalPositionIsDirty = true;
}

void GameObject::SetParent(GameObject* pParent, bool keepWorldPosition)
void GameObject::SetParent(GameObject* pParent, const bool keepWorldPosition)
{
// Parent should not be one of the children, the GameObject itself, or the current parent
if (IsChild(pParent) || (!pParent && pParent == this) || m_pParent == pParent)
Expand All @@ -84,14 +84,14 @@ void GameObject::SetParent(GameObject* pParent, bool keepWorldPosition)
{
if (m_pParent != nullptr)
{
// Set new localPosition as current worldPosition
m_LocalTransform.SetPosition(m_LocalTransform.GetPosition() + m_pParent->GetWorldPosition());
// Set old worldPosition as new localPosition
m_LocalTransform.Translate(m_pParent->GetWorldPosition());
}

if (pParent != nullptr)
{
// Set localPosition as (worldPosition - parent.WorldPositon)
m_LocalTransform.SetPosition(m_LocalTransform.GetPosition() - pParent->GetWorldPosition());
// Remove new parent's position from the localPosition
m_LocalTransform.Translate(- pParent->GetWorldPosition());

}
}
Expand Down Expand Up @@ -136,6 +136,5 @@ void GameObject::RemoveChild(GameObject* pChild)

bool GameObject::IsChild(const GameObject* pChild)
{
//std::ranges::find does not work here, some issue with move constructor, which weak_ptr doesn't have
return std::find(m_Children.begin(), m_Children.end(), pChild) != m_Children.end();
return std::ranges::find(m_Children, pChild) != m_Children.end();
}
4 changes: 2 additions & 2 deletions Minigin/GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class GameObject final
}
void Update(const float deltaTime);

void SetParent(GameObject* pParent, bool keepWorldPosition);
void SetParent(GameObject* pParent, const bool keepWorldPosition);

inline Transform GetTransform() const { return m_LocalTransform; }
glm::vec2 GetWorldPosition() const;
Expand All @@ -50,7 +50,7 @@ class GameObject final
GameObject& operator=(GameObject&& other) = delete;

private:
Transform m_LocalTransform{}; //WorldPosition
Transform m_LocalTransform{};
//bool m_LocalPositionIsDirty{ false };
bool m_IsDestroyed{ false };

Expand Down
8 changes: 3 additions & 5 deletions Minigin/Rotator.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "Rotator.h"
#include "GameObject.h"

constexpr float PI{ 3.14159f };

Rotator::Rotator(const std::weak_ptr<GameObject> pOwner, const float angleDegrees, const glm::vec2& center)
: Component(pOwner)
, m_RotationAngleDegrees{ angleDegrees }
Expand All @@ -23,7 +21,7 @@ void Rotator::Update(const float deltaTime)
{
if (auto owner{ GetOwner() })
{
glm::vec2 position{ GetOwner()->GetTransform().GetPosition() };
glm::vec2 position{ owner->GetTransform().GetPosition() };

const float anglesRad{ m_RotationAngleDegrees * (PI / 180.0f) * deltaTime };

Expand All @@ -39,6 +37,6 @@ void Rotator::Update(const float deltaTime)
x += m_RotationCenter.x;
y += m_RotationCenter.y;

GetOwner()->SetLocalPosition(x, y);
}
owner->SetLocalPosition(x, y);
}
}
2 changes: 2 additions & 0 deletions Minigin/Rotator.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "Component.h"
#include <glm/glm.hpp>

constexpr float PI{ 3.14159f };

class Rotator final : public Component
{
public:
Expand Down
13 changes: 12 additions & 1 deletion Minigin/Transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ void Transform::SetPosition(const float x, const float y)
m_Position.y = y;
}

void Transform::SetPosition(const glm::vec2 position)
void Transform::SetPosition(const glm::vec2& position)
{
m_Position = position;
}

void Transform::Translate(const glm::vec2& translation)
{
m_Position += translation;
}

void Transform::Translate(const float x, const float y)
{
m_Position.x += x;
m_Position.y += y;
}
4 changes: 3 additions & 1 deletion Minigin/Transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class Transform final
Transform(const glm::vec2& position);
inline const glm::vec2& GetPosition() const { return m_Position; }
void SetPosition(const float x, const float y);
void SetPosition(const glm::vec2 position);
void SetPosition(const glm::vec2& position);
void Translate(const glm::vec2& translation);
void Translate(const float x, const float y);

private:
glm::vec2 m_Position;
Expand Down

0 comments on commit 1b18d0f

Please sign in to comment.