Add Entity::RenderComponents and Entity::HasComponent
This commit is contained in:
parent
196f1ae572
commit
c395a72e87
|
@ -30,10 +30,8 @@ namespace simpleengine {
|
||||||
virtual void Move(const float& delta_time, const sf::Vector2f& offset) {};
|
virtual void Move(const float& delta_time, const sf::Vector2f& offset) {};
|
||||||
virtual void Move(const sf::Vector2f& offset) {};
|
virtual void Move(const sf::Vector2f& offset) {};
|
||||||
|
|
||||||
virtual void Render(sf::RenderTarget* target) = 0;
|
virtual void Render(sf::RenderTarget* target);
|
||||||
virtual void Update(const float& delta_time) {
|
virtual void Update(const float& delta_time);
|
||||||
UpdateComponents(delta_time);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Called when the entity is about to be destroyed.
|
// Called when the entity is about to be destroyed.
|
||||||
// Make sure to call this in your extending Entity.
|
// 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()
|
void DestroyLater(); // In most cases, this will be ran next EntityEvent::Update()
|
||||||
const bool& IsGettingDestroyed() const;
|
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 UpdateComponents(const float& delta_time);
|
||||||
|
void RenderComponents(sf::RenderTarget* target);
|
||||||
void AddComponent(std::unique_ptr<Component> component);
|
void AddComponent(std::unique_ptr<Component> component);
|
||||||
protected:
|
protected:
|
||||||
std::vector<std::unique_ptr<Component>> components;
|
std::vector<std::unique_ptr<Component>> components;
|
||||||
|
|
|
@ -7,6 +7,14 @@
|
||||||
#include "entity.h"
|
#include "entity.h"
|
||||||
#include "component.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) {
|
void simpleengine::Entity::UpdateComponents(const float& delta_time) {
|
||||||
for (std::unique_ptr<Component>& component : components) {
|
for (std::unique_ptr<Component>& component : components) {
|
||||||
component->Update(delta_time);
|
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) {
|
void simpleengine::Entity::AddComponent(std::unique_ptr<Component> component) {
|
||||||
components.push_back(std::move(component));
|
components.push_back(std::move(component));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue