Create a Destructable classes to replace IsQuitting and Quit

This commit is contained in:
SeanOMik 2021-03-12 17:18:23 -06:00
parent 8e914acab6
commit 15421ff7e0
No known key found for this signature in database
GPG Key ID: CA09E5BE1F32728A
7 changed files with 40 additions and 54 deletions

View File

@ -7,19 +7,20 @@
#ifndef SIMPLEENGINE_COMPONENT_H
#define SIMPLEENGINE_COMPONENT_H
#include "destructable.h"
#include <SFML/Graphics.hpp>
#include <memory>
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.

View File

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

View File

@ -7,6 +7,8 @@
#ifndef SIMPLEENGINE_ENTITY_H
#define SIMPLEENGINE_ENTITY_H
#include "destructable.h"
#include <SFML/Graphics.hpp>
#include <stdexcept>
@ -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<typename T>
bool HasComponent() const {
for (std::shared_ptr<Component> comp : components) {

View File

@ -7,27 +7,16 @@
#ifndef SIMPLEENGINE_EVENT_H
#define SIMPLEENGINE_EVENT_H
#include "destructable.h"
#include <SFML/Graphics.hpp>
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:

View File

@ -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;
}

View File

@ -48,12 +48,4 @@ void simpleengine::Entity::RenderComponents(sf::RenderTarget* target) {
for (std::shared_ptr<Component>& component : components) {
component->Render(target);
}
}
void simpleengine::Entity::Destroy() {
destroying = true;
}
const bool& simpleengine::Entity::IsDestroying() const {
return destroying;
}
}

View File

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