]> git.seodisparate.com - EntityComponentMetaSystem/commitdiff
Minor fixes/changes
authorStephen Seo <seo.disparate@gmail.com>
Mon, 14 Mar 2016 09:16:09 +0000 (18:16 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 14 Mar 2016 09:16:09 +0000 (18:16 +0900)
src/EC/Bitset.hpp
src/EC/Manager.hpp
src/test/ECTest.cpp

index 1288cee0b6aa4cd15e4cda8e90f8ec54b1cc5f57..e97dd86b4c9dad4b482aa967d1eddc8abcf50769 100644 (file)
@@ -16,12 +16,24 @@ namespace EC
     {
         using Combined = EC::Meta::Combine<ComponentsList, TagsList>;
 
+        template <typename Component>
+        constexpr auto getComponentBit() const
+        {
+            return (*this)[EC::Meta::IndexOf<Component, Combined>::value];
+        }
+
         template <typename Component>
         constexpr auto getComponentBit()
         {
             return (*this)[EC::Meta::IndexOf<Component, Combined>::value];
         }
 
+        template <typename Tag>
+        constexpr auto getTagBit() const
+        {
+            return (*this)[EC::Meta::IndexOf<Tag, Combined>::value];
+        }
+
         template <typename Tag>
         constexpr auto getTagBit()
         {
index 978de20ade1425af87128bebfef413bf3c275162..a8edd091005ab73b663181a2b4214b8d282d8fbe 100644 (file)
@@ -98,18 +98,48 @@ namespace EC
             return std::get<std::vector<Component> >(componentsStorage).at(std::get<std::size_t>(entities.at(index)));
         }
 
+        template <typename Component>
+        bool hasComponent(std::size_t index) const
+        {
+            return std::get<BitsetType>(entities.at(index)).template getComponentBit<Component>();
+        }
+
+        template <typename Tag>
+        bool hasTag(std::size_t index) const
+        {
+            return std::get<BitsetType>(entities.at(index)).template getTagBit<Tag>();
+        }
+
         void cleanup()
         {
+            if(currentSize == 0)
+            {
+                return;
+            }
+
             std::size_t rhs = currentSize - 1;
             std::size_t lhs = 0;
 
             while(lhs < rhs)
             {
-                if(!std::get<bool>(entities[lhs]))
+                while(!std::get<bool>(entities[rhs]))
+                {
+                    --rhs;
+                    if(rhs == 0)
+                    {
+                        currentSize = 0;
+                        return;
+                    }
+                }
+                if(lhs >= rhs)
+                {
+                    break;
+                }
+                else if(!std::get<bool>(entities[lhs]))
                 {
                     // lhs is marked for deletion
                     // swap lhs entity with rhs entity
-                    std::swap(entities[lhs], entities[rhs]);
+                    std::swap(entities[lhs], entities.at(rhs));
 
                     // clear deleted bitset
                     std::get<BitsetType>(entities[rhs]).reset();
index 03e01e9b7e7179e9c9160ccecd45d9f973eb6436..08f7f8ae136edc416381fe93a5fd6674a4e25b5d 100644 (file)
@@ -99,6 +99,16 @@ TEST(EC, Manager)
         EXPECT_EQ(pos.y, 7);
     }
 
+    {
+        bool has = manager.hasComponent<C0>(e1);
+
+        EXPECT_TRUE(has);
+
+        has = manager.hasTag<T0>(e1);
+
+        EXPECT_TRUE(has);
+    }
+
     manager.deleteEntity(e0);
     manager.cleanup();
 
@@ -119,5 +129,9 @@ TEST(EC, Manager)
     manager.forMatchingSignature<EC::Meta::TypeList<T0> >(updateTagOnly);
 
     EXPECT_EQ(2, count);
+
+    manager.deleteEntity(e1);
+    manager.deleteEntity(e2);
+    manager.cleanup();
 }