diff --git a/include/simpleengine/component.h b/include/simpleengine/component.h index d10598a..86b394f 100644 --- a/include/simpleengine/component.h +++ b/include/simpleengine/component.h @@ -7,19 +7,20 @@ #ifndef SIMPLEENGINE_COMPONENT_H #define SIMPLEENGINE_COMPONENT_H +#include "destructable.h" + #include #include namespace simpleengine { class Entity; - class Component { + class Component : public simpleengine::Destructable { public: - explicit Component(Entity& owning_entity); - virtual ~Component() = default; + explicit Component(Entity& owning_entity) : owning_entity(owning_entity) { - virtual void Destroy(); // In most cases, this will be ran next Entity::Update. - const bool& IsDestroying() const; + } + virtual ~Component() = default; virtual void Update(const float& delta_time) = 0; virtual void Render(sf::RenderTarget* target) {}; // Most components won't need to be rendered. diff --git a/include/simpleengine/destructable.h b/include/simpleengine/destructable.h new file mode 100644 index 0000000..817593a --- /dev/null +++ b/include/simpleengine/destructable.h @@ -0,0 +1,25 @@ +// +// Created by SeanOMik on 3/12/2021. +// Github: https://github.com/SeanOMik +// Email: seanomik@gmail.com +// + +#ifndef SIMPLEENGINE_DESTRUCTABLE_H +#define SIMPLEENGINE_DESTRUCTABLE_H + +namespace simpleengine { + class Destructable { + public: + virtual void Destroy() { + destroying = true; + } + + virtual const bool& IsDestroying() const { + return destroying; + } + protected: + bool destroying = false; + }; +} + +#endif //SIMPLEENGINE_DESTRUCTABLE_H diff --git a/include/simpleengine/entity.h b/include/simpleengine/entity.h index defbfdc..c07fd2a 100644 --- a/include/simpleengine/entity.h +++ b/include/simpleengine/entity.h @@ -7,6 +7,8 @@ #ifndef SIMPLEENGINE_ENTITY_H #define SIMPLEENGINE_ENTITY_H +#include "destructable.h" + #include #include @@ -20,8 +22,7 @@ namespace simpleengine { class Game; class Event; - // @TODO Create a Destructible class that replaces Entity::Destroying, Entity::DestroyLater, and Entity::IsGettingDestroyed. - class Entity { + class Entity : public simpleengine::Destructable { friend class Game; friend class Event; public: @@ -36,9 +37,6 @@ namespace simpleengine { virtual void Render(sf::RenderTarget* target); virtual void Update(const float& delta_time); - virtual void Destroy(); - const bool& IsDestroying() const; - template bool HasComponent() const { for (std::shared_ptr comp : components) { diff --git a/include/simpleengine/event.h b/include/simpleengine/event.h index aa4006c..590d1fe 100644 --- a/include/simpleengine/event.h +++ b/include/simpleengine/event.h @@ -7,27 +7,16 @@ #ifndef SIMPLEENGINE_EVENT_H #define SIMPLEENGINE_EVENT_H +#include "destructable.h" + #include namespace simpleengine { - class Event { + class Event : public simpleengine::Destructable { public: explicit Event(sf::RenderWindow* window = nullptr) : window(window) {} virtual ~Event() = default; - - // Check if this event is quitting. - virtual const bool& IsQuitting() { - return quit; - } - - // Quit the event at next tick. - virtual void Quit() { - quit = true; - } - - // Abstract methods - //virtual void CheckForQuit() = 0; // Ran every Update to check if we're gonna quit. - //virtual void Quiting() {} // Ran when a State is about to be destroyed. + virtual void Update(const float& delta_time) = 0; virtual void Render(sf::RenderTarget* target) = 0; protected: diff --git a/src/component.cpp b/src/component.cpp deleted file mode 100644 index 2474d1c..0000000 --- a/src/component.cpp +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by SeanOMik on 7/3/2020. -// Github: https://github.com/SeanOMik -// Email: seanomik@gmail.com -// - -#include "component.h" - -simpleengine::Component::Component(Entity& owning_entity) : owning_entity(owning_entity) { - -} - -void simpleengine::Component::Destroy() { - destroying = true; -} - -const bool& simpleengine::Component::IsDestroying() const { - return destroying; -} diff --git a/src/entity.cpp b/src/entity.cpp index bbe816a..6bfa161 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -48,12 +48,4 @@ void simpleengine::Entity::RenderComponents(sf::RenderTarget* target) { for (std::shared_ptr& component : components) { component->Render(target); } -} - -void simpleengine::Entity::Destroy() { - destroying = true; -} - -const bool& simpleengine::Entity::IsDestroying() const { - return destroying; -} +} \ No newline at end of file diff --git a/src/game.cpp b/src/game.cpp index 9f4cc3a..c7c973e 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -51,7 +51,7 @@ void simpleengine::Game::Update() { for (std::vector::iterator it = events.begin(); it != events.end(); ) { (*it)->Update(delta_time); - if ((*it)->IsQuitting()) { + if ((*it)->IsDestroying()) { delete (*it); it = events.erase(it); } else {