From 2cc1f044df61faae818e0f3957b86d68d22b135a Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Fri, 23 Aug 2019 15:54:53 +0200 Subject: [PATCH] constructor to create actors from existing entities (close #296) --- src/entt/entity/actor.hpp | 39 +++++++++++++++++++++++++------------- test/entt/entity/actor.cpp | 19 +++++++++++++++++-- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/entt/entity/actor.hpp b/src/entt/entity/actor.hpp index ccbae16c2..b1889e685 100644 --- a/src/entt/entity/actor.hpp +++ b/src/entt/entity/actor.hpp @@ -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 + template bool has() const ENTT_NOEXCEPT { - return reg->template has(entt); + return (reg->template has(entt) && ...); } /** @@ -183,8 +196,8 @@ struct basic_actor { } private: - registry_type *reg; entity_type entt; + registry_type *reg; }; diff --git a/test/entt/entity/actor.cpp b/test/entt/entity/actor.cpp index b23ee9cee..ff4e8f79f 100644 --- a/test/entt/entity/actor.cpp +++ b/test/entt/entity/actor.cpp @@ -29,8 +29,7 @@ TEST(Actor, Component) { ASSERT_FALSE(registry.empty()); ASSERT_FALSE(registry.empty()); - ASSERT_TRUE(actor.has()); - ASSERT_TRUE(actor.has()); + ASSERT_TRUE((actor.has())); ASSERT_FALSE(actor.has()); actor.remove(); @@ -40,6 +39,22 @@ TEST(Actor, Component) { ASSERT_FALSE(actor.has()); } +TEST(Actor, FromEntity) { + entt::registry registry; + const auto entity = registry.create(); + + registry.assign(entity, 42); + registry.assign(entity, 'c'); + + entt::actor actor{entity, registry}; + + ASSERT_TRUE(actor); + ASSERT_EQ(entity, actor.entity()); + ASSERT_TRUE((actor.has())); + ASSERT_EQ(actor.get(), 42); + ASSERT_EQ(actor.get(), 'c'); +} + TEST(Actor, EntityLifetime) { entt::registry registry; entt::actor actor{};