Sfoglia il codice sorgente

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

Michele Caini 6 anni fa
parent
commit
2cc1f044df
2 ha cambiato i file con 43 aggiunte e 15 eliminazioni
  1. 26 13
      src/entt/entity/actor.hpp
  2. 17 2
      test/entt/entity/actor.cpp

+ 26 - 13
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<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;
 };
 
 

+ 17 - 2
test/entt/entity/actor.cpp

@@ -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{};