Create a Destructable classes to replace IsQuitting and Quit
This commit is contained in:
parent
8e914acab6
commit
15421ff7e0
|
@ -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.
|
||||
|
|
|
@ -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
|
|
@ -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) {
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue