From 635eed34a4a069a6ba2cf9b3f9dbb2d864a4b651 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 14 Mar 2016 18:16:09 +0900 Subject: [PATCH] Minor fixes/changes --- src/EC/Bitset.hpp | 12 ++++++++++++ src/EC/Manager.hpp | 34 ++++++++++++++++++++++++++++++++-- src/test/ECTest.cpp | 14 ++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/EC/Bitset.hpp b/src/EC/Bitset.hpp index 1288cee..e97dd86 100644 --- a/src/EC/Bitset.hpp +++ b/src/EC/Bitset.hpp @@ -16,12 +16,24 @@ namespace EC { using Combined = EC::Meta::Combine; + template + constexpr auto getComponentBit() const + { + return (*this)[EC::Meta::IndexOf::value]; + } + template constexpr auto getComponentBit() { return (*this)[EC::Meta::IndexOf::value]; } + template + constexpr auto getTagBit() const + { + return (*this)[EC::Meta::IndexOf::value]; + } + template constexpr auto getTagBit() { diff --git a/src/EC/Manager.hpp b/src/EC/Manager.hpp index 978de20..a8edd09 100644 --- a/src/EC/Manager.hpp +++ b/src/EC/Manager.hpp @@ -98,18 +98,48 @@ namespace EC return std::get >(componentsStorage).at(std::get(entities.at(index))); } + template + bool hasComponent(std::size_t index) const + { + return std::get(entities.at(index)).template getComponentBit(); + } + + template + bool hasTag(std::size_t index) const + { + return std::get(entities.at(index)).template getTagBit(); + } + void cleanup() { + if(currentSize == 0) + { + return; + } + std::size_t rhs = currentSize - 1; std::size_t lhs = 0; while(lhs < rhs) { - if(!std::get(entities[lhs])) + while(!std::get(entities[rhs])) + { + --rhs; + if(rhs == 0) + { + currentSize = 0; + return; + } + } + if(lhs >= rhs) + { + break; + } + else if(!std::get(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(entities[rhs]).reset(); diff --git a/src/test/ECTest.cpp b/src/test/ECTest.cpp index 03e01e9..08f7f8a 100644 --- a/src/test/ECTest.cpp +++ b/src/test/ECTest.cpp @@ -99,6 +99,16 @@ TEST(EC, Manager) EXPECT_EQ(pos.y, 7); } + { + bool has = manager.hasComponent(e1); + + EXPECT_TRUE(has); + + has = manager.hasTag(e1); + + EXPECT_TRUE(has); + } + manager.deleteEntity(e0); manager.cleanup(); @@ -119,5 +129,9 @@ TEST(EC, Manager) manager.forMatchingSignature >(updateTagOnly); EXPECT_EQ(2, count); + + manager.deleteEntity(e1); + manager.deleteEntity(e2); + manager.cleanup(); }