constructor to create actors from existing entities (close #296)

This commit is contained in:
Michele Caini
2019-08-23 15:54:53 +02:00
parent c651392643
commit 2cc1f044df
2 changed files with 43 additions and 15 deletions

View File

@@ -15,10 +15,12 @@ namespace entt {
/**
* @brief Dedicated to those who aren't confident with entity-component systems.
* @brief Dedicated to those who aren't confident with the
* entity-component-system architecture.
*
* Tiny wrapper around a registry, for all those users that aren't confident
* with entity-component systems and prefer to iterate objects directly.
* with entity-component-system architecture and prefer to iterate objects
* directly.
*
* @tparam Entity A valid entity type (see entt_traits for more details).
*/
@@ -30,17 +32,28 @@ struct basic_actor {
using entity_type = Entity;
basic_actor() ENTT_NOEXCEPT
: reg{nullptr}, entt{entt::null}
: entt{entt::null}, reg{nullptr}
{}
/**
* @brief Constructs an actor by using the given registry.
* @param ref An entity-component system properly initialized.
* @brief Constructs an actor from a given registry.
* @param ref An instance of the registry class.
*/
explicit basic_actor(registry_type &ref)
: reg{&ref}, entt{ref.create()}
: entt{ref.create()}, reg{&ref}
{}
/**
* @brief Constructs an actor from a given entity.
* @param entity A valid entity identifier.
* @param ref An instance of the registry class.
*/
explicit basic_actor(entity_type entity, registry_type &ref)
: entt{entity}, reg{&ref}
{
ENTT_ASSERT(ref.valid(entity));
}
/*! @brief Default destructor. */
virtual ~basic_actor() {
if(*this) {
@@ -58,7 +71,7 @@ struct basic_actor {
* @param other The instance to move from.
*/
basic_actor(basic_actor &&other)
: reg{other.reg}, entt{other.entt}
: entt{other.entt}, reg{other.reg}
{
other.entt = null;
}
@@ -112,13 +125,13 @@ struct basic_actor {
}
/**
* @brief Checks if an actor has the given component.
* @tparam Component Type of the component for which to perform the check.
* @return True if the actor has the component, false otherwise.
* @brief Checks if an actor has the given components.
* @tparam Component Components for which to perform the check.
* @return True if the actor has all the components, false otherwise.
*/
template<typename Component>
template<typename... Component>
bool has() const ENTT_NOEXCEPT {
return reg->template has<Component>(entt);
return (reg->template has<Component>(entt) && ...);
}
/**
@@ -183,8 +196,8 @@ struct basic_actor {
}
private:
registry_type *reg;
entity_type entt;
registry_type *reg;
};

View File

@@ -29,8 +29,7 @@ TEST(Actor, Component) {
ASSERT_FALSE(registry.empty<int>());
ASSERT_FALSE(registry.empty());
ASSERT_TRUE(actor.has<int>());
ASSERT_TRUE(actor.has<char>());
ASSERT_TRUE((actor.has<int, char>()));
ASSERT_FALSE(actor.has<double>());
actor.remove<int>();
@@ -40,6 +39,22 @@ TEST(Actor, Component) {
ASSERT_FALSE(actor.has<int>());
}
TEST(Actor, FromEntity) {
entt::registry registry;
const auto entity = registry.create();
registry.assign<int>(entity, 42);
registry.assign<char>(entity, 'c');
entt::actor actor{entity, registry};
ASSERT_TRUE(actor);
ASSERT_EQ(entity, actor.entity());
ASSERT_TRUE((actor.has<int, char>()));
ASSERT_EQ(actor.get<int>(), 42);
ASSERT_EQ(actor.get<char>(), 'c');
}
TEST(Actor, EntityLifetime) {
entt::registry registry;
entt::actor actor{};