Change how some classes are destroyed

This commit is contained in:
SeanOMik 2021-03-12 17:08:20 -06:00
parent e2ef3acc5e
commit f38c239a6a
No known key found for this signature in database
GPG Key ID: CA09E5BE1F32728A
9 changed files with 32 additions and 44 deletions

View File

@ -46,16 +46,10 @@ namespace simpleengine {
} }
}; };
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; 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 { virtual void Update(const float& delta_time) final {
std::unique_lock<std::mutex> unique_lock(mutex); std::unique_lock<std::mutex> unique_lock(mutex);
tick_delta_time = delta_time; tick_delta_time = delta_time;

View File

@ -16,11 +16,10 @@ namespace simpleengine {
class Component { class Component {
public: public:
explicit Component(Entity& owning_entity); explicit Component(Entity& owning_entity);
//virtual ~Component() = default; virtual ~Component() = default;
//virtual void Destroying(); // Called when the Component is about to be destroyed. virtual void Destroy(); // In most cases, this will be ran next Entity::Update.
void DestroyLater(); // In most cases, this will be ran next Entity::Update. const bool& IsDestroying() const;
const bool& IsGettingDestroyed() const;
virtual void Update(const float& delta_time) = 0; virtual void Update(const float& delta_time) = 0;
virtual void Render(sf::RenderTarget* target) {}; // Most components won't need to be rendered. virtual void Render(sf::RenderTarget* target) {}; // Most components won't need to be rendered.

View File

@ -36,11 +36,8 @@ namespace simpleengine {
virtual void Render(sf::RenderTarget* target); virtual void Render(sf::RenderTarget* target);
virtual void Update(const float& delta_time); virtual void Update(const float& delta_time);
// Called when the entity is about to be destroyed. virtual void Destroy();
// Make sure to implment this in your extending Entity. const bool& IsDestroying() const;
virtual void Destroying();
void DestroyLater(); // In most cases, this will be ran next EntityEvent::Update()
const bool& IsGettingDestroyed() const;
template<typename T> template<typename T>
bool HasComponent() const { bool HasComponent() const {

View File

@ -15,13 +15,19 @@ namespace simpleengine {
explicit Event(sf::RenderWindow* window = nullptr) : window(window) {} explicit Event(sf::RenderWindow* window = nullptr) : window(window) {}
virtual ~Event() = default; virtual ~Event() = default;
const bool& WantsToQuit() { // Check if this event is quitting.
virtual const bool& IsQuitting() {
return quit; return quit;
} }
// Quit the event at next tick.
virtual void Quit() {
quit = true;
}
// Abstract methods // Abstract methods
virtual void CheckForQuit() = 0; // Ran every Update to check if we're gonna quit. //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 Quiting() {} // Ran when a State is about to be destroyed.
virtual void Update(const float& delta_time) = 0; virtual void Update(const float& delta_time) = 0;
virtual void Render(sf::RenderTarget* target) = 0; virtual void Render(sf::RenderTarget* target) = 0;
protected: protected:

View File

@ -18,7 +18,6 @@ namespace simpleengine {
} }
void Update(const float& delta_time) override; void Update(const float& delta_time) override;
void CheckForQuit() override { }
void Render(sf::RenderTarget* target) override { } void Render(sf::RenderTarget* target) override { }
private: private:
std::vector<std::shared_ptr<Entity>> entities; std::vector<std::shared_ptr<Entity>> entities;

View File

@ -17,14 +17,12 @@ namespace simpleengine {
} }
void CheckForQuit() override {
if (entity->IsGettingDestroyed()) {
quit = true;
}
}
void Update(const float& delta_time) override { void Update(const float& delta_time) override {
entity->Update(delta_time); entity->Update(delta_time);
if (entity->IsDestroying()) {
quit = true;
}
} }
void Render(sf::RenderTarget* target) override { void Render(sf::RenderTarget* target) override {

View File

@ -10,10 +10,10 @@ simpleengine::Component::Component(Entity& owning_entity) : owning_entity(owning
} }
void simpleengine::Component::DestroyLater() { void simpleengine::Component::Destroy() {
destroying = true; destroying = true;
} }
const bool &simpleengine::Component::IsGettingDestroyed() const { const bool& simpleengine::Component::IsDestroying() const {
return destroying; return destroying;
} }

View File

@ -32,11 +32,14 @@ void simpleengine::Entity::Update(const float &delta_time) {
} }
void simpleengine::Entity::UpdateComponents(const float& delta_time) { void simpleengine::Entity::UpdateComponents(const float& delta_time) {
for (std::shared_ptr<Component>& component : components) { // Update each component and if they are being destroyed, destroy them.
component->Update(delta_time); for (std::vector<std::shared_ptr<Component>>::iterator it = components.begin(); it != components.end(); ) {
(*it)->Update(delta_time);
if (component->IsGettingDestroyed()) { if ((*it)->IsDestroying()) {
components.erase(std::remove(components.begin(), components.end(), component)); it = components.erase(it);
} else {
++it;
} }
} }
} }
@ -47,16 +50,10 @@ void simpleengine::Entity::RenderComponents(sf::RenderTarget* target) {
} }
} }
void simpleengine::Entity::Destroying() { void simpleengine::Entity::Destroy() {
for (std::shared_ptr<Component>& component : components) {
component->DestroyLater();
}
}
void simpleengine::Entity::DestroyLater() {
destroying = true; destroying = true;
} }
const bool &simpleengine::Entity::IsGettingDestroyed() const { const bool& simpleengine::Entity::IsDestroying() const {
return destroying; return destroying;
} }

View File

@ -51,9 +51,7 @@ void simpleengine::Game::Update() {
for (std::vector<Event*>::iterator it = events.begin(); it != events.end(); ) { for (std::vector<Event*>::iterator it = events.begin(); it != events.end(); ) {
(*it)->Update(delta_time); (*it)->Update(delta_time);
if ((*it)->WantsToQuit()) { if ((*it)->IsQuitting()) {
(*it)->Quiting();
delete (*it); delete (*it);
it = events.erase(it); it = events.erase(it);
} else { } else {