From e021743e6a09369e81b94aa8ec897ea928b1944e Mon Sep 17 00:00:00 2001 From: Riyyi Date: Mon, 18 Jan 2021 17:16:51 +0100 Subject: [PATCH] Remove entity class --- inferno/src/inferno/application.cpp | 1 - inferno/src/inferno/scene/entity.cpp | 46 --------------- inferno/src/inferno/scene/entity.h | 77 -------------------------- inferno/src/inferno/scene/scene.cpp | 48 ++++++++++------ inferno/src/inferno/scene/scene.h | 53 ++++++++++++++++-- inferno/src/inferno/systems/camera.cpp | 1 - 6 files changed, 80 insertions(+), 146 deletions(-) delete mode 100644 inferno/src/inferno/scene/entity.cpp delete mode 100644 inferno/src/inferno/scene/entity.h diff --git a/inferno/src/inferno/application.cpp b/inferno/src/inferno/application.cpp index b937aa5..acb9cf1 100644 --- a/inferno/src/inferno/application.cpp +++ b/inferno/src/inferno/application.cpp @@ -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" diff --git a/inferno/src/inferno/scene/entity.cpp b/inferno/src/inferno/scene/entity.cpp deleted file mode 100644 index 66fffca..0000000 --- a/inferno/src/inferno/scene/entity.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "inferno/scene/entity.h" - -namespace Inferno { - - Entity::Entity(const std::shared_ptr& registry) - : m_registry(registry) - { - expired(); - m_entity = m_registry.lock()->create(); - } - - Entity::Entity(const std::shared_ptr& 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& 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(entity); - } - -} diff --git a/inferno/src/inferno/scene/entity.h b/inferno/src/inferno/scene/entity.h deleted file mode 100644 index c61025e..0000000 --- a/inferno/src/inferno/scene/entity.h +++ /dev/null @@ -1,77 +0,0 @@ -#ifndef ENTITY_H -#define ENTITY_H - -#include // uint32_t, size_t -#include // std::shared_ptr, std::weak_ptr -#include // 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& registry); - Entity(const std::shared_ptr& registry, entt::entity handle); - Entity(const std::shared_ptr& registry, uint32_t handle); - - void expired() const; - void valid() const; - - template - [[nodiscard]] bool has() const - { - valid(); - return m_registry.lock()->has(m_entity); - } - - template - [[nodiscard]] bool any() const - { - valid(); - return m_registry.lock()->any(m_entity); - } - - template - T& add(P&&... parameters) const - { - valid(); - return m_registry.lock()->emplace_or_replace(m_entity, std::forward

(parameters)...); - }; - - template - size_t remove() const - { - valid(); - return m_registry.lock()->remove_if_exists(m_entity); - } - - template - T& get(P&&... parameters) const - { - valid(); - return m_registry.lock()->get_or_emplace(m_entity, std::forward

(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(m_entity); } - // Functor() - inline uint32_t operator ()() const { return static_cast(m_entity); } - - private: - entt::entity m_entity = entt::null; - - std::weak_ptr m_registry; - }; - -// ---------------------------------------- - - const LogStream& operator<<(const LogStream& stream, entt::entity handle); - -} - -#endif // ENTITY_H diff --git a/inferno/src/inferno/scene/scene.cpp b/inferno/src/inferno/scene/scene.cpp index 6297389..1aaa584 100644 --- a/inferno/src/inferno/scene/scene.cpp +++ b/inferno/src/inferno/scene/scene.cpp @@ -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(); - auto& cameraTransform = camera.get(); - cameraTransform.translate.z = 1.0f; + uint32_t camera = createEntity("Camera Entity"); + auto& cameraTransform = getComponent(camera); + cameraTransform.translate.z = -1.0f; + addComponent(camera, CameraType::Orthographic); + addComponent(camera).bind(); RenderSystem* renderSystem = new RenderSystem(); renderSystem->initialize(); @@ -41,18 +43,18 @@ namespace Inferno { // Construct entities // --------------------------------- - Entity quad = createEntity("Quad"); - quad.add(glm::vec4 { 1.0f, 1.0f, 1.0f, 1.0f }, m_texture); + uint32_t quad = createEntity("Quad"); + addComponent(quad, glm::vec4 { 1.0f, 1.0f, 1.0f, 1.0f }, m_texture); - Entity quad2 = createEntity("Quad 2"); - auto& quad2Transform = quad2.get(); + uint32_t quad2 = createEntity("Quad 2"); + auto& quad2Transform = getComponent(quad2); quad2Transform.translate.x = 1.1f; - quad2.add(glm::vec4 { 0.5f, 0.6f, 0.8f, 1.0f }, m_texture); + addComponent(quad2, glm::vec4 { 0.5f, 0.6f, 0.8f, 1.0f }, m_texture); - Entity quad3 = createEntity("Quad 3"); - auto& quad3Transform = quad3.get(); + uint32_t quad3 = createEntity("Quad 3"); + auto& quad3Transform = getComponent(quad3); quad3Transform.translate.x = 2.2f; - quad3.add(glm::vec4 { 1.0f, 1.0f, 1.0f, 1.0f }, m_texture2); + addComponent(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(name.empty() ? "Unnamed Entity" : name); - entity.add(); + uint32_t entity = static_cast(m_registry->create()); + addComponent(entity, name.empty() ? "Unnamed Entity" : name); + addComponent(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(entity); + } + } diff --git a/inferno/src/inferno/scene/scene.h b/inferno/src/inferno/scene/scene.h index 13746eb..8be485a 100644 --- a/inferno/src/inferno/scene/scene.h +++ b/inferno/src/inferno/scene/scene.h @@ -2,7 +2,7 @@ #define SCENE_H #include // uint32_t -#include // std::shared_ptr +#include // 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 + [[nodiscard]] bool hasComponent(uint32_t entity) const + { + validEntity(entity); + return m_registry->has(entt::entity { entity }); + } + + template + [[nodiscard]] bool anyComponent(uint32_t entity) const + { + validEntity(entity); + return m_registry->any(entt::entity { entity }); + } + + // @Todo Should replace be allowed? could trigger memory leaks with nativescript + template + T& addComponent(uint32_t entity, P&&... parameters) const + { + validEntity(entity); + return m_registry->emplace_or_replace(entt::entity { entity }, std::forward

(parameters)...); + }; + + template + size_t removeComponent(uint32_t entity) const + { + validEntity(entity); + return m_registry->remove_if_exists(entt::entity { entity }); + } + + // @Todo Should replace be allowed? could trigger memory leaks with nativescript + template + T& getComponent(uint32_t entity, P&&... parameters) const + { + validEntity(entity); + return m_registry->get_or_emplace(entt::entity { entity }, std::forward

(parameters)...); + } + + // const entt::registry& registry() const { return m_registry; } + std::shared_ptr registry() const { return m_registry; } + private: std::shared_ptr m_texture; std::shared_ptr m_texture2; @@ -33,6 +74,10 @@ namespace Inferno { std::shared_ptr m_registry; }; +// ---------------------------------------- + + const LogStream& operator<<(const LogStream& stream, entt::entity handle); + } #endif // SCENE_H diff --git a/inferno/src/inferno/systems/camera.cpp b/inferno/src/inferno/systems/camera.cpp index 0709359..f1c2ca7 100644 --- a/inferno/src/inferno/systems/camera.cpp +++ b/inferno/src/inferno/systems/camera.cpp @@ -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"