Add Entity::RenderComponents and Entity::HasComponent

This commit is contained in:
SeanOMik 2020-07-07 00:50:39 -05:00
parent 196f1ae572
commit c395a72e87
No known key found for this signature in database
GPG Key ID: FA4D55AC05268A88
2 changed files with 28 additions and 4 deletions

View File

@ -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<typename T>
bool HasComponent() {
for (std::unique_ptr<Component>& comp : components) {
if (dynamic_cast<T*>(comp.get())) {
return true;
}
}
return false;
}
void UpdateComponents(const float& delta_time);
void RenderComponents(sf::RenderTarget* target);
void AddComponent(std::unique_ptr<Component> component);
protected:
std::vector<std::unique_ptr<Component>> components;

View File

@ -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>& 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>& component : components) {
component->Render(target);
}
}
void simpleengine::Entity::AddComponent(std::unique_ptr<Component> component) {
components.push_back(std::move(component));
}