Riyyi
4 years ago
6 changed files with 80 additions and 146 deletions
@ -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
|
Loading…
Reference in new issue