Files
entt/test/example/custom_identifier.cpp
Michele Caini 3e7dc7af29 entity:
* review of entt_traits design
* added static constexpr member function entt_traits::to_integral
* added static constexpr member function entt_traits::to_entity
* added static constexpr member function entt_traits::to_version
* added static constexpr member function entt_traits::to_type
* custom class identifiers must expose member type entity_field
* it's no longer required to specialize entt_traits (breaking change)
2021-05-28 08:59:50 +02:00

56 lines
1.2 KiB
C++

#include <type_traits>
#include <gtest/gtest.h>
#include <entt/entity/entity.hpp>
#include <entt/entity/registry.hpp>
struct entity_id {
using entity_type = std::uint32_t;
static constexpr auto null = entt::null;
constexpr entity_id(entity_type value = null)
: entt{value}
{}
constexpr entity_id(const entity_id &other)
: entt{other.entt}
{}
constexpr operator entity_type() const {
return entt;
}
private:
entity_type entt;
};
TEST(Example, CustomIdentifier) {
entt::basic_registry<entity_id> registry{};
entity_id entity{};
ASSERT_FALSE(registry.valid(entity));
ASSERT_TRUE(entity == entt::null);
entity = registry.create();
ASSERT_TRUE(registry.valid(entity));
ASSERT_TRUE(entity != entt::null);
ASSERT_FALSE((registry.all_of<int, char>(entity)));
ASSERT_EQ(registry.try_get<int>(entity), nullptr);
registry.emplace<int>(entity, 42);
ASSERT_TRUE((registry.any_of<int, char>(entity)));
ASSERT_EQ(registry.get<int>(entity), 42);
registry.destroy(entity);
ASSERT_FALSE(registry.valid(entity));
ASSERT_TRUE(entity != entt::null);
entity = registry.create();
ASSERT_TRUE(registry.valid(entity));
ASSERT_TRUE(entity != entt::null);
}