2020-07-03 03:48:58 +00:00
|
|
|
//
|
|
|
|
// Created by SeanOMik on 7/2/2020.
|
|
|
|
// Github: https://github.com/SeanOMik
|
|
|
|
// Email: seanomik@gmail.com
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef SIMPLEENGINE_ENTITY_H
|
|
|
|
#define SIMPLEENGINE_ENTITY_H
|
|
|
|
|
|
|
|
#include <SFML/Graphics.hpp>
|
|
|
|
|
2021-03-02 04:08:10 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <vcruntime_typeinfo.h>
|
2020-07-03 20:26:44 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <memory>
|
2020-07-13 04:16:59 +00:00
|
|
|
#include <assert.h>
|
2020-07-03 18:31:17 +00:00
|
|
|
|
2020-07-03 03:48:58 +00:00
|
|
|
namespace simpleengine {
|
2020-07-03 18:31:17 +00:00
|
|
|
class Component;
|
|
|
|
class Game;
|
|
|
|
class Event;
|
2020-07-03 20:26:44 +00:00
|
|
|
|
2020-07-05 05:33:29 +00:00
|
|
|
// @TODO Create a Destructible class that replaces Entity::Destroying, Entity::DestroyLater, and Entity::IsGettingDestroyed.
|
|
|
|
class Entity {
|
2020-07-03 18:31:17 +00:00
|
|
|
friend class Game;
|
|
|
|
friend class Event;
|
2020-07-03 03:48:58 +00:00
|
|
|
public:
|
2020-11-04 18:57:44 +00:00
|
|
|
explicit Entity(sf::Transformable& transformable);
|
2020-07-03 03:48:58 +00:00
|
|
|
virtual ~Entity() = default;
|
2020-07-03 20:26:44 +00:00
|
|
|
Entity(const Entity& entity) = delete;
|
2020-07-03 03:48:58 +00:00
|
|
|
|
2020-07-13 03:35:11 +00:00
|
|
|
virtual void Move(const float& delta_time, const float& dir_x, const float& dir_y);
|
|
|
|
virtual void Move(const float& delta_time, const sf::Vector2f& offset);
|
|
|
|
virtual void Move(const sf::Vector2f& offset);
|
2020-07-04 21:42:11 +00:00
|
|
|
|
2020-07-07 05:50:39 +00:00
|
|
|
virtual void Render(sf::RenderTarget* target);
|
|
|
|
virtual void Update(const float& delta_time);
|
2020-07-03 18:31:17 +00:00
|
|
|
|
2021-03-12 23:08:20 +00:00
|
|
|
virtual void Destroy();
|
|
|
|
const bool& IsDestroying() const;
|
2020-07-03 18:31:17 +00:00
|
|
|
|
2020-07-07 05:50:39 +00:00
|
|
|
template<typename T>
|
2021-03-02 04:08:10 +00:00
|
|
|
bool HasComponent() const {
|
|
|
|
for (std::shared_ptr<Component> comp : components) {
|
2020-07-07 05:50:39 +00:00
|
|
|
if (dynamic_cast<T*>(comp.get())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-02 04:08:10 +00:00
|
|
|
template<typename T>
|
|
|
|
std::shared_ptr<T> GetComponent() const {
|
|
|
|
for (std::shared_ptr<Component> comp : components) {
|
|
|
|
if (dynamic_cast<T*>(comp.get())) {
|
|
|
|
return dynamic_pointer_cast<T>(comp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:31:17 +00:00
|
|
|
void UpdateComponents(const float& delta_time);
|
2020-07-07 05:50:39 +00:00
|
|
|
void RenderComponents(sf::RenderTarget* target);
|
2020-07-13 04:16:59 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void AddComponent(std::shared_ptr<T> component) {
|
|
|
|
static_assert(std::is_base_of_v<Component, T>, "Component class must derive from simpleengine::Component");
|
|
|
|
|
|
|
|
// Only allow one type of the same component.
|
|
|
|
assert(!HasComponent<T>());
|
|
|
|
components.push_back(component);
|
|
|
|
}
|
2020-07-13 03:41:10 +00:00
|
|
|
|
2020-11-04 18:57:44 +00:00
|
|
|
inline sf::Transformable& GetTransformable() {
|
|
|
|
return transformable;
|
|
|
|
}
|
2020-07-04 18:55:40 +00:00
|
|
|
protected:
|
2020-11-04 18:57:44 +00:00
|
|
|
sf::Transformable& transformable;
|
2020-07-13 04:16:59 +00:00
|
|
|
std::vector<std::shared_ptr<Component>> components;
|
2020-07-03 18:31:17 +00:00
|
|
|
bool destroying = false;
|
2020-07-03 03:48:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //GAMEENGINE_ENTITY_H
|