From 132860e1791ae38556d736dcc3c7f992b74e740f Mon Sep 17 00:00:00 2001 From: Riyyi Date: Wed, 13 Jan 2021 14:31:50 +0100 Subject: [PATCH] Add entity class --- inferno/src/inferno/scene/entity.cpp | 37 ++++++++++++++ inferno/src/inferno/scene/entity.h | 74 ++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 inferno/src/inferno/scene/entity.cpp create mode 100644 inferno/src/inferno/scene/entity.h diff --git a/inferno/src/inferno/scene/entity.cpp b/inferno/src/inferno/scene/entity.cpp new file mode 100644 index 0000000..1122e8a --- /dev/null +++ b/inferno/src/inferno/scene/entity.cpp @@ -0,0 +1,37 @@ +#include "inferno/scene/entity.h" + +namespace Inferno { + + Entity::Entity(const std::shared_ptr& registry) + : m_registry(registry) + { + m_entity = m_registry.lock()->create(); + } + + Entity::Entity(const std::shared_ptr& registry, entt::entity handle) + : m_registry(registry) + { + 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) + { + ASSERT(m_registry.lock()->valid(entt::entity(handle)), "Can't construct entity from invalid handle"); + m_entity = entt::entity(handle); + } + + void Entity::valid() const + { + ASSERT(m_registry.lock()->valid(m_entity)); + } + +// ---------------------------------------- + + 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 new file mode 100644 index 0000000..7283ff7 --- /dev/null +++ b/inferno/src/inferno/scene/entity.h @@ -0,0 +1,74 @@ +#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 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)...); + } + + 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); } + 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