From c395a72e87f664ec82bb6db3410da180cb5fa385 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Tue, 7 Jul 2020 00:50:39 -0500 Subject: [PATCH] Add Entity::RenderComponents and Entity::HasComponent --- include/simpleengine/entity.h | 18 ++++++++++++++---- src/entity.cpp | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/include/simpleengine/entity.h b/include/simpleengine/entity.h index 0e4322e..1a2febf 100644 --- a/include/simpleengine/entity.h +++ b/include/simpleengine/entity.h @@ -30,10 +30,8 @@ namespace simpleengine { virtual void Move(const float& delta_time, const sf::Vector2f& offset) {}; virtual void Move(const sf::Vector2f& offset) {}; - virtual void Render(sf::RenderTarget* target) = 0; - virtual void Update(const float& delta_time) { - UpdateComponents(delta_time); - }; + virtual void Render(sf::RenderTarget* target); + virtual void Update(const float& delta_time); // Called when the entity is about to be destroyed. // Make sure to call this in your extending Entity. @@ -41,7 +39,19 @@ namespace simpleengine { void DestroyLater(); // In most cases, this will be ran next EntityEvent::Update() const bool& IsGettingDestroyed() const; + template + bool HasComponent() { + for (std::unique_ptr& comp : components) { + if (dynamic_cast(comp.get())) { + return true; + } + } + + return false; + } + void UpdateComponents(const float& delta_time); + void RenderComponents(sf::RenderTarget* target); void AddComponent(std::unique_ptr component); protected: std::vector> components; diff --git a/src/entity.cpp b/src/entity.cpp index eee4543..f6fc504 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -7,6 +7,14 @@ #include "entity.h" #include "component.h" +void simpleengine::Entity::Render(sf::RenderTarget *target) { + RenderComponents(target); +} + +void simpleengine::Entity::Update(const float &delta_time) { + UpdateComponents(delta_time); +} + void simpleengine::Entity::UpdateComponents(const float& delta_time) { for (std::unique_ptr& component : components) { component->Update(delta_time); @@ -17,6 +25,12 @@ void simpleengine::Entity::UpdateComponents(const float& delta_time) { } } +void simpleengine::Entity::RenderComponents(sf::RenderTarget* target) { + for (std::unique_ptr& component : components) { + component->Render(target); + } +} + void simpleengine::Entity::AddComponent(std::unique_ptr component) { components.push_back(std::move(component)); }