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

@ -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<std::mutex> unique_lock(mutex);
tick_delta_time = delta_time;

View File

@ -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.

View File

@ -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<typename T>
bool HasComponent() const {

View File

@ -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:

View File

@ -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<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 {
entity->Update(delta_time);
if (entity->IsDestroying()) {
quit = true;
}
}
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;
}
const bool &simpleengine::Component::IsGettingDestroyed() const {
const bool& simpleengine::Component::IsDestroying() const {
return destroying;
}

View File

@ -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>& component : components) {
component->Update(delta_time);
// Update each component and if they are being destroyed, destroy them.
for (std::vector<std::shared_ptr<Component>>::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>& 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;
}

View File

@ -51,9 +51,7 @@ void simpleengine::Game::Update() {
for (std::vector<Event*>::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 {