Преглед изворни кода

added registry::reset<Comp>(entity)

Michele Caini пре 9 година
родитељ
комит
ad8bed937e
3 измењених фајлова са 14 додато и 0 уклоњено
  1. 1 0
      README.md
  2. 7 0
      src/registry.hpp
  3. 6 0
      test/registry.cpp

+ 1 - 0
README.md

@@ -179,6 +179,7 @@ Once you have created a registry, the followings are the exposed member function
 * `clone(entity)`: clones an entity and all its components, then returns the new entity identifier.
 * `copy<Component>(from, to)`: copies a component from an entity to another one (both the entities must already have been assigned the component, undefined behaviour otherwise).
 * `copy(from, to)`: copies all the components and their contents from an entity to another one (comoonents are created or destroyed if needed).
+* `reset<Component>(entity)`: removes the given component from the entity if assigned.
 * `reset<Component>()`: destroys all the instances of `Component`.
 * `reset()`: resets the pool and destroys all the entities and their components.
 * `view<Components...>()`: gets a view of the entities that have the given components (see below for further details).

+ 7 - 0
src/registry.hpp

@@ -353,6 +353,13 @@ public:
         (void)accumulator;
     }
 
+    template<typename Comp>
+    void reset(entity_type entity) {
+        if(pool.template has<Comp>(entity)) {
+            pool.template destroy<Comp>(entity);
+        }
+    }
+
     template<typename Comp>
     void reset() {
         pool.template reset<Comp>();

+ 6 - 0
test/registry.cpp

@@ -103,6 +103,12 @@ TEST(DefaultRegistry, Functionalities) {
 
     ASSERT_TRUE(registry.empty<int>());
     ASSERT_TRUE(registry.empty<char>());
+
+    e1 = registry.create<int>();
+
+    ASSERT_NO_THROW(registry.reset<int>(e1));
+    ASSERT_NO_THROW(registry.reset<int>(e2));
+    ASSERT_TRUE(registry.empty<int>());
 }
 
 TEST(DefaultRegistry, ViewSingleComponent) {