Skip to content

Commit

Permalink
Merge pull request #35 from LeeVangraefschepe/remove-observer
Browse files Browse the repository at this point in the history
Removed observer classes
  • Loading branch information
LeeVangraefschepe authored Dec 10, 2023
2 parents 371e549 + 2395136 commit 9141112
Show file tree
Hide file tree
Showing 31 changed files with 286 additions and 210 deletions.
14 changes: 7 additions & 7 deletions LeapEngine/Components/Audio/AudioListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

#include "../Transform/Transform.h"

void leap::AudioListener::Notify()
void leap::AudioListener::Awake()
{
Transform* pTransform{ GetTransform() };
ServiceLocator::GetAudio().UpdateListener3D(pTransform->GetWorldPosition(), {}, pTransform->GetForward(), pTransform->GetUp());
GetTransform()->OnPositionChanged.AddListener(this, &AudioListener::OnPositionChanged);
}

void leap::AudioListener::Awake()
void leap::AudioListener::OnDestroy()
{
GetTransform()->OnPositionChanged.AddListener(this);
GetTransform()->OnPositionChanged.RemoveListener(this, &AudioListener::OnPositionChanged);
}

void leap::AudioListener::OnDestroy()
void leap::AudioListener::OnPositionChanged() const
{
GetTransform()->OnPositionChanged.RemoveListener(this);
Transform* pTransform{ GetTransform() };
ServiceLocator::GetAudio().UpdateListener3D(pTransform->GetWorldPosition(), {}, pTransform->GetForward(), pTransform->GetUp());
}
6 changes: 2 additions & 4 deletions LeapEngine/Components/Audio/AudioListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

#include "../Component.h"

#include <Observer.h>

namespace leap
{
class AudioListener final : public Component, public Observer
class AudioListener final : public Component
{
public:
AudioListener() = default;
Expand All @@ -20,6 +18,6 @@ namespace leap
private:
virtual void Awake() override;
virtual void OnDestroy() override;
virtual void Notify() override;
void OnPositionChanged() const;
};
}
20 changes: 10 additions & 10 deletions LeapEngine/Components/Audio/AudioSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,27 +124,27 @@ void leap::AudioSource::Awake()
{
if (m_PlayOnAwake) Play();

GetTransform()->OnPositionChanged.AddListener(this);
}

void leap::AudioSource::Notify()
{
// Only update if there is a 3D sound playing
if (!IsPlaying() || !m_Is3DSound) return;

Update3DSound();
GetTransform()->OnPositionChanged.AddListener(this, &AudioSource::OnPositionChanged);
}

void leap::AudioSource::OnDestroy()
{
GetTransform()->OnPositionChanged.RemoveListener(this);
GetTransform()->OnPositionChanged.RemoveListener(this, &AudioSource::OnPositionChanged);

if (!IsPlaying()) return;

Stop();
m_Channel = -1;
}

void leap::AudioSource::OnPositionChanged() const
{
// Only update if there is a 3D sound playing
if (!IsPlaying() || !m_Is3DSound) return;

Update3DSound();
}

void leap::AudioSource::Update2DVolume() const
{
ServiceLocator::GetAudio().SetVolume2D(m_Channel, m_MaxVolume);
Expand Down
6 changes: 2 additions & 4 deletions LeapEngine/Components/Audio/AudioSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include "../Component.h"

#include <Observer.h>

#include <string>

namespace leap
Expand All @@ -13,7 +11,7 @@ namespace leap
class IAudioClip;
}

class AudioSource final : public Component, public Observer
class AudioSource final : public Component
{
public:
AudioSource() = default;
Expand Down Expand Up @@ -44,9 +42,9 @@ namespace leap

private:
virtual void Awake() override;
virtual void Notify() override;
virtual void OnDestroy() override;

void OnPositionChanged() const;
void Update2DVolume() const;
void Update3DSound() const;
void UpdateLoopCount() const;
Expand Down
9 changes: 2 additions & 7 deletions LeapEngine/Components/Physics/Collider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,13 @@ void leap::Collider::Awake()
m_pShape->SetRelativeTransform(relativePosition, relativeRotation);
}

GetTransform()->OnScaleChanged.AddListener(this);
GetTransform()->OnScaleChanged.AddListener(this, &Collider::RescaleShape);
}

void leap::Collider::OnDestroy()
{
ServiceLocator::GetPhysics().Get(m_pOwningObject)->RemoveShape(m_pShape.get());
GetTransform()->OnScaleChanged.RemoveListener(this);
}

void leap::Collider::Notify()
{
RescaleShape();
GetTransform()->OnScaleChanged.RemoveListener(this, &Collider::RescaleShape);
}

void leap::Collider::Move(const Rigidbody* pRigidbody)
Expand Down
5 changes: 1 addition & 4 deletions LeapEngine/Components/Physics/Collider.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <Interfaces/IShape.h>
#include <Subject.h>
#include <Observer.h>

#include <memory>

Expand All @@ -19,7 +18,7 @@ namespace leap
class IPhysicsMaterial;
}

class Collider : public Component, public Observer
class Collider : public Component
{
public:
struct CollisionCallback final
Expand Down Expand Up @@ -55,8 +54,6 @@ namespace leap
virtual void Awake() override;
virtual void OnDestroy() override;

virtual void Notify() override;

void Move(const Rigidbody* pRigidbody);

GameObject* m_pOwningObject{};
Expand Down
19 changes: 7 additions & 12 deletions LeapEngine/Components/RenderComponents/CameraComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
leap::CameraComponent::CameraComponent()
{
const auto window = GameContext::GetInstance().GetWindow();
window->AddListener(this);
window->AddListener(this, &CameraComponent::OnScreenSizeChanged);
const auto& size = window->GetWindowSize();
constexpr float fov = 90.f;
m_pCamera = std::make_unique<graphics::Camera>(static_cast<float>(size.x), static_cast<float>(size.y), fov);
Expand All @@ -26,30 +26,25 @@ leap::CameraComponent::~CameraComponent()
{
}

void leap::CameraComponent::Notify(const glm::ivec2& data)
void leap::CameraComponent::OnScreenSizeChanged(const glm::ivec2& data)
{
m_pCamera->SetAspectRatio(data);
}

void leap::CameraComponent::Awake()
{
GetTransform()->OnPositionChanged.AddListener(this);
GetTransform()->OnRotationChanged.AddListener(this);
GetTransform()->OnPositionChanged.AddListener(this, &CameraComponent::UpdateTransform);
GetTransform()->OnRotationChanged.AddListener(this, &CameraComponent::UpdateTransform);

UpdateTransform();
}

void leap::CameraComponent::OnDestroy()
{
GameContext::GetInstance().GetWindow()->RemoveListener(this);
GameContext::GetInstance().GetWindow()->RemoveListener(this, &CameraComponent::OnScreenSizeChanged);

GetTransform()->OnPositionChanged.RemoveListener(this);
GetTransform()->OnRotationChanged.RemoveListener(this);
}

void leap::CameraComponent::Notify()
{
UpdateTransform();
GetTransform()->OnPositionChanged.RemoveListener(this, &CameraComponent::UpdateTransform);
GetTransform()->OnRotationChanged.RemoveListener(this, &CameraComponent::UpdateTransform);
}

void leap::CameraComponent::UpdateTransform() const
Expand Down
8 changes: 3 additions & 5 deletions LeapEngine/Components/RenderComponents/CameraComponent.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "../Component.h"
#include "Observer.h"
#include <memory>

#include "vec2.hpp"
Expand All @@ -13,14 +12,12 @@ namespace leap
class Camera;
}

class CameraComponent final : public Component, TObserver<glm::ivec2>, Observer
class CameraComponent final : public Component
{
public:
CameraComponent();
virtual ~CameraComponent();

void Notify(const glm::ivec2& data) override;

CameraComponent(const CameraComponent& other) = delete;
CameraComponent(CameraComponent&& other) = delete;
CameraComponent& operator=(const CameraComponent& other) = delete;
Expand All @@ -32,7 +29,8 @@ namespace leap
private:
virtual void Awake() override;
virtual void OnDestroy() override;
virtual void Notify() override;
void OnScreenSizeChanged(const glm::ivec2& data);
void OnTransformChanged();

void UpdateTransform() const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@

void leap::DirectionalLightComponent::Awake()
{
GetTransform()->OnPositionChanged.AddListener(this);
GetTransform()->OnRotationChanged.AddListener(this);
GetTransform()->OnRotationChanged.AddListener(this, &DirectionalLightComponent::UpdateTransform);
UpdateTransform();
}

void leap::DirectionalLightComponent::OnDestroy()
{
GetTransform()->OnPositionChanged.RemoveListener(this);
GetTransform()->OnRotationChanged.RemoveListener(this);
}

void leap::DirectionalLightComponent::Notify()
{
UpdateTransform();
GetTransform()->OnRotationChanged.RemoveListener(this, &DirectionalLightComponent::UpdateTransform);
}

void leap::DirectionalLightComponent::UpdateTransform() const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

#include "../Component.h"

#include <Observer.h>

namespace leap
{
class DirectionalLightComponent final : public Component, public Observer
class DirectionalLightComponent final : public Component
{
public:
DirectionalLightComponent() = default;
Expand All @@ -20,8 +18,6 @@ namespace leap
private:
virtual void Awake() override;
virtual void OnDestroy() override;
virtual void Notify() override;

void UpdateTransform() const;
};
}
14 changes: 7 additions & 7 deletions LeapEngine/Components/RenderComponents/MeshRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ leap::graphics::IMaterial* leap::MeshRenderer::GetMaterial() const

void leap::MeshRenderer::Awake()
{
GetTransform()->OnPositionChanged.AddListener(this);
GetTransform()->OnRotationChanged.AddListener(this);
GetTransform()->OnScaleChanged.AddListener(this);
GetTransform()->OnPositionChanged.AddListener(this, &MeshRenderer::OnTransformChanged);
GetTransform()->OnRotationChanged.AddListener(this, &MeshRenderer::OnTransformChanged);
GetTransform()->OnScaleChanged.AddListener(this, &MeshRenderer::OnTransformChanged);
}

void leap::MeshRenderer::LateUpdate()
Expand All @@ -49,13 +49,13 @@ void leap::MeshRenderer::LateUpdate()

void leap::MeshRenderer::OnDestroy()
{
GetTransform()->OnPositionChanged.RemoveListener(this);
GetTransform()->OnRotationChanged.RemoveListener(this);
GetTransform()->OnScaleChanged.RemoveListener(this);
GetTransform()->OnPositionChanged.RemoveListener(this, &MeshRenderer::OnTransformChanged);
GetTransform()->OnRotationChanged.RemoveListener(this, &MeshRenderer::OnTransformChanged);
GetTransform()->OnScaleChanged.RemoveListener(this, &MeshRenderer::OnTransformChanged);
ServiceLocator::GetRenderer().RemoveMeshRenderer(m_pRenderer);
}

void leap::MeshRenderer::Notify()
void leap::MeshRenderer::OnTransformChanged()
{
m_IsDirty = true;
}
8 changes: 3 additions & 5 deletions LeapEngine/Components/RenderComponents/MeshRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "../Component.h"

#include <Interfaces/IMeshRenderer.h>
#include <Observer.h>

namespace leap
{
Expand All @@ -15,7 +14,7 @@ namespace leap
class CustomMesh;
}

class MeshRenderer final : public Component, public Observer
class MeshRenderer final : public Component
{
public:
MeshRenderer();
Expand All @@ -30,13 +29,12 @@ namespace leap
void SetMaterial(const Material& material);
graphics::IMaterial* GetMaterial() const;

protected:
private:
virtual void Awake() override;
virtual void LateUpdate() override;
virtual void OnDestroy() override;
virtual void Notify() override;
void OnTransformChanged();

private:
graphics::IMeshRenderer* m_pRenderer{};
bool m_IsDirty{ true };
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@ void leap::CanvasComponent::SetMatchMode(MatchMode matchMode)

void leap::CanvasComponent::Awake()
{
GameContext::GetInstance().GetWindow()->AddListener(this);
GameContext::GetInstance().GetWindow()->AddListener(this, &CanvasComponent::UpdateResolution);
}

void leap::CanvasComponent::OnDestroy()
{
GameContext::GetInstance().GetWindow()->RemoveListener(this);
}

void leap::CanvasComponent::Notify(const glm::ivec2& size)
{
UpdateResolution(size);
GameContext::GetInstance().GetWindow()->RemoveListener(this, &CanvasComponent::UpdateResolution);
}

void leap::CanvasComponent::UpdateResolution(const glm::ivec2& size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

#include "../../Component.h"

#include <Observer.h>
#include <Subject.h>

#include <vec2.hpp>
#include <Vector2.h>

namespace leap
{
class CanvasComponent final : public Component, public TObserver<glm::ivec2>
class CanvasComponent final : public Component
{
public:
enum class MatchMode
Expand Down Expand Up @@ -41,8 +40,6 @@ namespace leap
virtual void OnDestroy() override;

private:
virtual void Notify(const glm::ivec2& size) override;

void UpdateResolution(const glm::ivec2& size);

MatchMode m_MatchMode{ MatchMode::MatchHeight };
Expand Down
Loading

0 comments on commit 9141112

Please sign in to comment.