Remove entity class
This commit is contained in:
@@ -15,7 +15,6 @@
|
||||
#include "inferno/render/shader.h"
|
||||
#include "inferno/render/texture.h"
|
||||
#include "inferno/scene/components.h"
|
||||
#include "inferno/scene/entity.h"
|
||||
#include "inferno/scene/scene.h"
|
||||
#include "inferno/settings.h"
|
||||
#include "inferno/time.h"
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
#include "inferno/scene/entity.h"
|
||||
|
||||
namespace Inferno {
|
||||
|
||||
Entity::Entity(const std::shared_ptr<entt::registry>& registry)
|
||||
: m_registry(registry)
|
||||
{
|
||||
expired();
|
||||
m_entity = m_registry.lock()->create();
|
||||
}
|
||||
|
||||
Entity::Entity(const std::shared_ptr<entt::registry>& registry, entt::entity handle)
|
||||
: m_registry(registry)
|
||||
{
|
||||
expired();
|
||||
ASSERT(m_registry.lock()->valid(handle), "Can't construct entity from invalid handle");
|
||||
m_entity = handle;
|
||||
}
|
||||
|
||||
Entity::Entity(const std::shared_ptr<entt::registry>& registry, uint32_t handle)
|
||||
: m_registry(registry)
|
||||
{
|
||||
expired();
|
||||
ASSERT(m_registry.lock()->valid(entt::entity(handle)), "Can't construct entity from invalid handle");
|
||||
m_entity = entt::entity(handle);
|
||||
}
|
||||
|
||||
void Entity::expired() const
|
||||
{
|
||||
ASSERT(!m_registry.expired(), "Entity registry expired");
|
||||
}
|
||||
|
||||
void Entity::valid() const
|
||||
{
|
||||
expired();
|
||||
ASSERT(m_registry.lock()->valid(m_entity), "Entity is not valid");
|
||||
}
|
||||
|
||||
// ----------------------------------------
|
||||
|
||||
const LogStream& operator<<(const LogStream& stream, entt::entity entity)
|
||||
{
|
||||
return stream << static_cast<uint32_t>(entity);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
#ifndef ENTITY_H
|
||||
#define ENTITY_H
|
||||
|
||||
#include <cstdint> // uint32_t, size_t
|
||||
#include <memory> // std::shared_ptr, std::weak_ptr
|
||||
#include <utility> // std::forward
|
||||
|
||||
#include "entt/entity/registry.hpp" // ent::entity, entt::registry
|
||||
|
||||
#include "inferno/assertions.h"
|
||||
|
||||
namespace Inferno {
|
||||
|
||||
class Entity {
|
||||
public:
|
||||
Entity(const std::shared_ptr<entt::registry>& registry);
|
||||
Entity(const std::shared_ptr<entt::registry>& registry, entt::entity handle);
|
||||
Entity(const std::shared_ptr<entt::registry>& registry, uint32_t handle);
|
||||
|
||||
void expired() const;
|
||||
void valid() const;
|
||||
|
||||
template<typename... T>
|
||||
[[nodiscard]] bool has() const
|
||||
{
|
||||
valid();
|
||||
return m_registry.lock()->has<T...>(m_entity);
|
||||
}
|
||||
|
||||
template<typename... T>
|
||||
[[nodiscard]] bool any() const
|
||||
{
|
||||
valid();
|
||||
return m_registry.lock()->any<T...>(m_entity);
|
||||
}
|
||||
|
||||
template<typename T, typename... P>
|
||||
T& add(P&&... parameters) const
|
||||
{
|
||||
valid();
|
||||
return m_registry.lock()->emplace_or_replace<T>(m_entity, std::forward<P>(parameters)...);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
size_t remove() const
|
||||
{
|
||||
valid();
|
||||
return m_registry.lock()->remove_if_exists<T>(m_entity);
|
||||
}
|
||||
|
||||
template<typename T, typename... P>
|
||||
T& get(P&&... parameters) const
|
||||
{
|
||||
valid();
|
||||
return m_registry.lock()->get_or_emplace<T>(m_entity, std::forward<P>(parameters)...);
|
||||
}
|
||||
|
||||
// Casts
|
||||
inline operator bool() const { return m_entity != entt::null; }
|
||||
inline operator entt::entity() const { return m_entity; }
|
||||
inline operator uint32_t() const { return static_cast<uint32_t>(m_entity); }
|
||||
// Functor()
|
||||
inline uint32_t operator ()() const { return static_cast<uint32_t>(m_entity); }
|
||||
|
||||
private:
|
||||
entt::entity m_entity = entt::null;
|
||||
|
||||
std::weak_ptr<entt::registry> m_registry;
|
||||
};
|
||||
|
||||
// ----------------------------------------
|
||||
|
||||
const LogStream& operator<<(const LogStream& stream, entt::entity handle);
|
||||
|
||||
}
|
||||
|
||||
#endif // ENTITY_H
|
||||
@@ -1,7 +1,8 @@
|
||||
#include "inferno/log.h"
|
||||
#include "inferno/scene/components.h"
|
||||
#include "inferno/scene/entity.h"
|
||||
#include "inferno/scene/scene.h"
|
||||
#include "inferno/script/cameracontroller.h"
|
||||
#include "inferno/script/nativescript.h"
|
||||
#include "inferno/systems/camera.h"
|
||||
#include "inferno/systems/render.h"
|
||||
#include "inferno/systems/transform.h"
|
||||
@@ -23,10 +24,11 @@ namespace Inferno {
|
||||
cameraSystem->initialize();
|
||||
CameraSystem::the().setRegistry(m_registry);
|
||||
|
||||
Entity camera = createEntity("Camera Entity");
|
||||
camera.add<CameraComponent>();
|
||||
auto& cameraTransform = camera.get<TransformComponent>();
|
||||
cameraTransform.translate.z = 1.0f;
|
||||
uint32_t camera = createEntity("Camera Entity");
|
||||
auto& cameraTransform = getComponent<TransformComponent>(camera);
|
||||
cameraTransform.translate.z = -1.0f;
|
||||
addComponent<CameraComponent>(camera, CameraType::Orthographic);
|
||||
addComponent<NativeScriptComponent>(camera).bind<CameraController>();
|
||||
|
||||
RenderSystem* renderSystem = new RenderSystem();
|
||||
renderSystem->initialize();
|
||||
@@ -41,18 +43,18 @@ namespace Inferno {
|
||||
// Construct entities
|
||||
// ---------------------------------
|
||||
|
||||
Entity quad = createEntity("Quad");
|
||||
quad.add<SpriteComponent>(glm::vec4 { 1.0f, 1.0f, 1.0f, 1.0f }, m_texture);
|
||||
uint32_t quad = createEntity("Quad");
|
||||
addComponent<SpriteComponent>(quad, glm::vec4 { 1.0f, 1.0f, 1.0f, 1.0f }, m_texture);
|
||||
|
||||
Entity quad2 = createEntity("Quad 2");
|
||||
auto& quad2Transform = quad2.get<TransformComponent>();
|
||||
uint32_t quad2 = createEntity("Quad 2");
|
||||
auto& quad2Transform = getComponent<TransformComponent>(quad2);
|
||||
quad2Transform.translate.x = 1.1f;
|
||||
quad2.add<SpriteComponent>(glm::vec4 { 0.5f, 0.6f, 0.8f, 1.0f }, m_texture);
|
||||
addComponent<SpriteComponent>(quad2, glm::vec4 { 0.5f, 0.6f, 0.8f, 1.0f }, m_texture);
|
||||
|
||||
Entity quad3 = createEntity("Quad 3");
|
||||
auto& quad3Transform = quad3.get<TransformComponent>();
|
||||
uint32_t quad3 = createEntity("Quad 3");
|
||||
auto& quad3Transform = getComponent<TransformComponent>(quad3);
|
||||
quad3Transform.translate.x = 2.2f;
|
||||
quad3.add<SpriteComponent>(glm::vec4 { 1.0f, 1.0f, 1.0f, 1.0f }, m_texture2);
|
||||
addComponent<SpriteComponent>(quad3, glm::vec4 { 1.0f, 1.0f, 1.0f, 1.0f }, m_texture2);
|
||||
|
||||
dbg(Log::Info) << "Scene initialized";
|
||||
}
|
||||
@@ -77,11 +79,11 @@ namespace Inferno {
|
||||
TransformSystem::the().destroy();
|
||||
}
|
||||
|
||||
Entity Scene::createEntity(const std::string& name)
|
||||
uint32_t Scene::createEntity(const std::string& name)
|
||||
{
|
||||
Entity entity = Entity(m_registry);
|
||||
entity.add<TagComponent>(name.empty() ? "Unnamed Entity" : name);
|
||||
entity.add<TransformComponent>();
|
||||
uint32_t entity = static_cast<uint32_t>(m_registry->create());
|
||||
addComponent<TagComponent>(entity, name.empty() ? "Unnamed Entity" : name);
|
||||
addComponent<TransformComponent>(entity);
|
||||
|
||||
return entity;
|
||||
}
|
||||
@@ -101,4 +103,16 @@ namespace Inferno {
|
||||
return CameraSystem::the().projectionView();
|
||||
}
|
||||
|
||||
void Scene::validEntity(uint32_t entity) const
|
||||
{
|
||||
ASSERT(m_registry->valid(entt::entity {entity}), "Entity is not valid");
|
||||
}
|
||||
|
||||
// ----------------------------------------
|
||||
|
||||
const LogStream& operator<<(const LogStream& stream, entt::entity entity)
|
||||
{
|
||||
return stream << static_cast<uint32_t>(entity);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#define SCENE_H
|
||||
|
||||
#include <cstdint> // uint32_t
|
||||
#include <memory> // std::shared_ptr
|
||||
#include <memory> // std::shared_ptr
|
||||
|
||||
#include "entt/entity/registry.hpp" // entt::entity, entt::registry
|
||||
#include "glm/ext/matrix_float4x4.hpp" // glm::mat4
|
||||
@@ -20,12 +20,53 @@ namespace Inferno {
|
||||
void render();
|
||||
void destroy();
|
||||
|
||||
Entity createEntity(const std::string& name = "");
|
||||
Entity createEntity(entt::entity handle);
|
||||
Entity createEntity(uint32_t handle);
|
||||
uint32_t createEntity(const std::string& name = "");
|
||||
void destroyEntity(uint32_t entity);
|
||||
|
||||
glm::mat4 cameraProjectionView();
|
||||
|
||||
void validEntity(uint32_t entity) const;
|
||||
|
||||
template<typename... T>
|
||||
[[nodiscard]] bool hasComponent(uint32_t entity) const
|
||||
{
|
||||
validEntity(entity);
|
||||
return m_registry->has<T...>(entt::entity { entity });
|
||||
}
|
||||
|
||||
template<typename... T>
|
||||
[[nodiscard]] bool anyComponent(uint32_t entity) const
|
||||
{
|
||||
validEntity(entity);
|
||||
return m_registry->any<T...>(entt::entity { entity });
|
||||
}
|
||||
|
||||
// @Todo Should replace be allowed? could trigger memory leaks with nativescript
|
||||
template<typename T, typename... P>
|
||||
T& addComponent(uint32_t entity, P&&... parameters) const
|
||||
{
|
||||
validEntity(entity);
|
||||
return m_registry->emplace_or_replace<T>(entt::entity { entity }, std::forward<P>(parameters)...);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
size_t removeComponent(uint32_t entity) const
|
||||
{
|
||||
validEntity(entity);
|
||||
return m_registry->remove_if_exists<T>(entt::entity { entity });
|
||||
}
|
||||
|
||||
// @Todo Should replace be allowed? could trigger memory leaks with nativescript
|
||||
template<typename T, typename... P>
|
||||
T& getComponent(uint32_t entity, P&&... parameters) const
|
||||
{
|
||||
validEntity(entity);
|
||||
return m_registry->get_or_emplace<T>(entt::entity { entity }, std::forward<P>(parameters)...);
|
||||
}
|
||||
|
||||
// const entt::registry& registry() const { return m_registry; }
|
||||
std::shared_ptr<entt::registry> registry() const { return m_registry; }
|
||||
|
||||
private:
|
||||
std::shared_ptr<Texture> m_texture;
|
||||
std::shared_ptr<Texture> m_texture2;
|
||||
@@ -33,6 +74,10 @@ namespace Inferno {
|
||||
std::shared_ptr<entt::registry> m_registry;
|
||||
};
|
||||
|
||||
// ----------------------------------------
|
||||
|
||||
const LogStream& operator<<(const LogStream& stream, entt::entity handle);
|
||||
|
||||
}
|
||||
|
||||
#endif // SCENE_H
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
#include "inferno/input.h"
|
||||
#include "inferno/inputcodes.h"
|
||||
#include "inferno/log.h"
|
||||
#include "inferno/scene/entity.h"
|
||||
#include "inferno/systems/camera.h"
|
||||
#include "inferno/window.h"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user