Browse Source

Add entity class

master
Riyyi 3 years ago
parent
commit
132860e179
  1. 37
      inferno/src/inferno/scene/entity.cpp
  2. 74
      inferno/src/inferno/scene/entity.h

37
inferno/src/inferno/scene/entity.cpp

@ -0,0 +1,37 @@
#include "inferno/scene/entity.h"
namespace Inferno {
Entity::Entity(const std::shared_ptr<entt::registry>& registry)
: m_registry(registry)
{
m_entity = m_registry.lock()->create();
}
Entity::Entity(const std::shared_ptr<entt::registry>& 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<entt::registry>& 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<uint32_t>(entity);
}
}

74
inferno/src/inferno/scene/entity.h

@ -0,0 +1,74 @@
#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 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)...);
}
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); }
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
Loading…
Cancel
Save