From f38c239a6a5b607ec953b62413570337609d1622 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Fri, 12 Mar 2021 17:08:20 -0600 Subject: [PATCH] Change how some classes are destroyed --- include/simpleengine/async_event.h | 10 ++------- include/simpleengine/component.h | 7 +++---- include/simpleengine/entity.h | 7 ++----- include/simpleengine/event.h | 12 ++++++++--- .../simpleengine/events/collision_handler.h | 1 - include/simpleengine/events/entity_event.h | 10 ++++----- src/component.cpp | 4 ++-- src/entity.cpp | 21 ++++++++----------- src/game.cpp | 4 +--- 9 files changed, 32 insertions(+), 44 deletions(-) diff --git a/include/simpleengine/async_event.h b/include/simpleengine/async_event.h index 66ac947..4b1d790 100644 --- a/include/simpleengine/async_event.h +++ b/include/simpleengine/async_event.h @@ -45,17 +45,11 @@ namespace simpleengine { update_thread.join(); } }; - - const bool& WantsToQuit() { - return quit; - } - - // 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 AsyncUpdate(const float delta_time) = 0; + // DO NOT OVERRIDE THIS FUNCTION + // Notify the update thread to trigger the async update. virtual void Update(const float& delta_time) final { std::unique_lock unique_lock(mutex); tick_delta_time = delta_time; diff --git a/include/simpleengine/component.h b/include/simpleengine/component.h index ad84480..d10598a 100644 --- a/include/simpleengine/component.h +++ b/include/simpleengine/component.h @@ -16,11 +16,10 @@ namespace simpleengine { class Component { public: explicit Component(Entity& owning_entity); - //virtual ~Component() = default; + virtual ~Component() = default; - //virtual void Destroying(); // Called when the Component is about to be destroyed. - void DestroyLater(); // In most cases, this will be ran next Entity::Update. - const bool& IsGettingDestroyed() const; + virtual void Destroy(); // In most cases, this will be ran next Entity::Update. + const bool& IsDestroying() const; 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/entity.h b/include/simpleengine/entity.h index ed67a13..defbfdc 100644 --- a/include/simpleengine/entity.h +++ b/include/simpleengine/entity.h @@ -36,11 +36,8 @@ namespace simpleengine { 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 implment this in your extending Entity. - virtual void Destroying(); - void DestroyLater(); // In most cases, this will be ran next EntityEvent::Update() - const bool& IsGettingDestroyed() const; + virtual void Destroy(); + const bool& IsDestroying() const; template bool HasComponent() const { diff --git a/include/simpleengine/event.h b/include/simpleengine/event.h index b009a84..aa4006c 100644 --- a/include/simpleengine/event.h +++ b/include/simpleengine/event.h @@ -15,13 +15,19 @@ namespace simpleengine { explicit Event(sf::RenderWindow* window = nullptr) : window(window) {} virtual ~Event() = default; - const bool& WantsToQuit() { + // 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 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/include/simpleengine/events/collision_handler.h b/include/simpleengine/events/collision_handler.h index 4f14495..8b07607 100644 --- a/include/simpleengine/events/collision_handler.h +++ b/include/simpleengine/events/collision_handler.h @@ -18,7 +18,6 @@ namespace simpleengine { } void Update(const float& delta_time) override; - void CheckForQuit() override { } void Render(sf::RenderTarget* target) override { } private: std::vector> entities; diff --git a/include/simpleengine/events/entity_event.h b/include/simpleengine/events/entity_event.h index e6a9255..1abc32d 100644 --- a/include/simpleengine/events/entity_event.h +++ b/include/simpleengine/events/entity_event.h @@ -17,14 +17,12 @@ namespace simpleengine { } - void CheckForQuit() override { - if (entity->IsGettingDestroyed()) { - quit = true; - } - } - void Update(const float& delta_time) override { entity->Update(delta_time); + + if (entity->IsDestroying()) { + quit = true; + } } void Render(sf::RenderTarget* target) override { diff --git a/src/component.cpp b/src/component.cpp index 54959aa..2474d1c 100644 --- a/src/component.cpp +++ b/src/component.cpp @@ -10,10 +10,10 @@ simpleengine::Component::Component(Entity& owning_entity) : owning_entity(owning } -void simpleengine::Component::DestroyLater() { +void simpleengine::Component::Destroy() { destroying = true; } -const bool &simpleengine::Component::IsGettingDestroyed() const { +const bool& simpleengine::Component::IsDestroying() const { return destroying; } diff --git a/src/entity.cpp b/src/entity.cpp index fede02a..bbe816a 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -32,11 +32,14 @@ void simpleengine::Entity::Update(const float &delta_time) { } void simpleengine::Entity::UpdateComponents(const float& delta_time) { - for (std::shared_ptr& component : components) { - component->Update(delta_time); + // Update each component and if they are being destroyed, destroy them. + for (std::vector>::iterator it = components.begin(); it != components.end(); ) { + (*it)->Update(delta_time); - if (component->IsGettingDestroyed()) { - components.erase(std::remove(components.begin(), components.end(), component)); + if ((*it)->IsDestroying()) { + it = components.erase(it); + } else { + ++it; } } } @@ -47,16 +50,10 @@ void simpleengine::Entity::RenderComponents(sf::RenderTarget* target) { } } -void simpleengine::Entity::Destroying() { - for (std::shared_ptr& component : components) { - component->DestroyLater(); - } -} - -void simpleengine::Entity::DestroyLater() { +void simpleengine::Entity::Destroy() { destroying = true; } -const bool &simpleengine::Entity::IsGettingDestroyed() const { +const bool& simpleengine::Entity::IsDestroying() const { return destroying; } diff --git a/src/game.cpp b/src/game.cpp index 5e52e25..9f4cc3a 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -51,9 +51,7 @@ void simpleengine::Game::Update() { for (std::vector::iterator it = events.begin(); it != events.end(); ) { (*it)->Update(delta_time); - if ((*it)->WantsToQuit()) { - (*it)->Quiting(); - + if ((*it)->IsQuitting()) { delete (*it); it = events.erase(it); } else {