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
|
#ifndef SIMPLEENGINE_COMPONENT_H
|
||||||
#define SIMPLEENGINE_COMPONENT_H
|
#define SIMPLEENGINE_COMPONENT_H
|
||||||
|
|
||||||
|
#include "destructable.h"
|
||||||
|
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
namespace simpleengine {
|
namespace simpleengine {
|
||||||
class Entity;
|
class Entity;
|
||||||
class Component {
|
class Component : public simpleengine::Destructable {
|
||||||
public:
|
public:
|
||||||
explicit Component(Entity& owning_entity);
|
explicit Component(Entity& owning_entity) : owning_entity(owning_entity) {
|
||||||
virtual ~Component() = default;
|
|
||||||
|
|
||||||
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 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.
|
||||||
|
|
|
@ -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
|
#ifndef SIMPLEENGINE_ENTITY_H
|
||||||
#define SIMPLEENGINE_ENTITY_H
|
#define SIMPLEENGINE_ENTITY_H
|
||||||
|
|
||||||
|
#include "destructable.h"
|
||||||
|
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
@ -20,8 +22,7 @@ namespace simpleengine {
|
||||||
class Game;
|
class Game;
|
||||||
class Event;
|
class Event;
|
||||||
|
|
||||||
// @TODO Create a Destructible class that replaces Entity::Destroying, Entity::DestroyLater, and Entity::IsGettingDestroyed.
|
class Entity : public simpleengine::Destructable {
|
||||||
class Entity {
|
|
||||||
friend class Game;
|
friend class Game;
|
||||||
friend class Event;
|
friend class Event;
|
||||||
public:
|
public:
|
||||||
|
@ -36,9 +37,6 @@ 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);
|
||||||
|
|
||||||
virtual void Destroy();
|
|
||||||
const bool& IsDestroying() const;
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
bool HasComponent() const {
|
bool HasComponent() const {
|
||||||
for (std::shared_ptr<Component> comp : components) {
|
for (std::shared_ptr<Component> comp : components) {
|
||||||
|
|
|
@ -7,27 +7,16 @@
|
||||||
#ifndef SIMPLEENGINE_EVENT_H
|
#ifndef SIMPLEENGINE_EVENT_H
|
||||||
#define SIMPLEENGINE_EVENT_H
|
#define SIMPLEENGINE_EVENT_H
|
||||||
|
|
||||||
|
#include "destructable.h"
|
||||||
|
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
namespace simpleengine {
|
namespace simpleengine {
|
||||||
class Event {
|
class Event : public simpleengine::Destructable {
|
||||||
public:
|
public:
|
||||||
explicit Event(sf::RenderWindow* window = nullptr) : window(window) {}
|
explicit Event(sf::RenderWindow* window = nullptr) : window(window) {}
|
||||||
virtual ~Event() = default;
|
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 Update(const float& delta_time) = 0;
|
||||||
virtual void Render(sf::RenderTarget* target) = 0;
|
virtual void Render(sf::RenderTarget* target) = 0;
|
||||||
protected:
|
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;
|
|
||||||
}
|
|
|
@ -49,11 +49,3 @@ void simpleengine::Entity::RenderComponents(sf::RenderTarget* target) {
|
||||||
component->Render(target);
|
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(); ) {
|
for (std::vector<Event*>::iterator it = events.begin(); it != events.end(); ) {
|
||||||
(*it)->Update(delta_time);
|
(*it)->Update(delta_time);
|
||||||
|
|
||||||
if ((*it)->IsQuitting()) {
|
if ((*it)->IsDestroying()) {
|
||||||
delete (*it);
|
delete (*it);
|
||||||
it = events.erase(it);
|
it = events.erase(it);
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue