diff --git a/tests/zecsy.cpp b/tests/zecsy.cpp index ca2b8e7..c94ebba 100644 --- a/tests/zecsy.cpp +++ b/tests/zecsy.cpp @@ -15,7 +15,6 @@ TEST_CASE("Create a single entity and verify its existence") auto e = w.make_entity(); REQUIRE(w.is_alive(e)); - REQUIRE(e.is_alive()); } TEST_CASE("Destroy an entity and ensure it no longer exists in the world") @@ -26,7 +25,6 @@ TEST_CASE("Destroy an entity and ensure it no longer exists in the world") w.destroy_entity(e); REQUIRE_FALSE(w.is_alive(e)); - REQUIRE_FALSE(e.is_alive()); } TEST_CASE("Entity #0 should be reserved and never used") @@ -36,8 +34,7 @@ TEST_CASE("Entity #0 should be reserved and never used") auto e = w.make_entity(); REQUIRE(e != 0); - REQUIRE(entity() == 0); - REQUIRE_FALSE(entity().is_alive()); + REQUIRE_FALSE(w.is_alive(0)); } struct ChoosenOne @@ -52,7 +49,6 @@ TEST_CASE("Entity shouldn't have a component that wasn't attached to it") auto e = w.make_entity(); REQUIRE_FALSE(w.has(e)); - REQUIRE_FALSE(e.has()); } TEST_CASE("Attempt of getting non-owned component should throw") @@ -62,7 +58,6 @@ TEST_CASE("Attempt of getting non-owned component should throw") auto e = w.make_entity(); REQUIRE_THROWS(w.get(e)); - REQUIRE_THROWS(e.get()); } TEST_CASE("Attach a simple component to an entity and verify it is correctly associated") @@ -70,15 +65,13 @@ TEST_CASE("Attach a simple component to an entity and verify it is correctly ass world w; auto e1 = w.make_entity(); - e1.set(ChoosenOne{}); + w.set(e1, ChoosenOne{}); - REQUIRE(e1.has()); REQUIRE(w.has(e1)); auto e2 = w.make_entity(); w.set(e2, ChoosenOne{}); - REQUIRE(e2.has()); REQUIRE(w.has(e2)); } @@ -92,13 +85,13 @@ TEST_CASE("Retrieve a component from an entity and verify its data matches what world w; auto e = w.make_entity(); - e.set(Name{"zecsy!"}); + w.set(e, Name{"zecsy!"}); - REQUIRE(e.get().value == "zecsy!"); + REQUIRE(w.get(e).value == "zecsy!"); - e.get().value = "super-zecsy!"; + w.get(e).value = "super-zecsy!"; - REQUIRE(e.get().value == "super-zecsy!"); + REQUIRE(w.get(e).value == "super-zecsy!"); } TEST_CASE("Remove a component from an entity and verify it is no longer attached") @@ -106,12 +99,12 @@ TEST_CASE("Remove a component from an entity and verify it is no longer attached world w; auto e = w.make_entity(); - e.set(ChoosenOne{}); - REQUIRE_NOTHROW(e.remove()); + w.set(e, ChoosenOne{}); + REQUIRE_NOTHROW(w.remove(e)); - REQUIRE_FALSE(e.has()); + REQUIRE_FALSE(w.has(e)); - e.set(ChoosenOne{}); + w.set(e, ChoosenOne{}); REQUIRE_NOTHROW(w.remove(e)); REQUIRE_FALSE(w.has(e)); @@ -120,7 +113,7 @@ TEST_CASE("Remove a component from an entity and verify it is no longer attached TEST_CASE("Addresses of removed components should be reused") { world w; - std::vector entities; + std::vector entities; std::vector addr; const int N = 4; @@ -130,27 +123,27 @@ TEST_CASE("Addresses of removed components should be reused") for(int j = 0; j < N; ++j) { entities.emplace_back(w.make_entity()); - entities.back().set(); + w.set(entities.back()); } if(addr.empty()) { for(int j = 0; j < N; ++j) { - addr.emplace_back(&entities[j].get()); + addr.emplace_back(&w.get(entities[j])); } } else { for(int j = 0; j < N; ++j) { - REQUIRE(&entities[j].get() == addr[j]); + REQUIRE(&w.get(entities[j]) == addr[j]); } } for(auto e: entities) { - e.remove(); + w.remove(e); } entities.clear(); } @@ -161,17 +154,15 @@ TEST_CASE("Attach multiple components to an entity and verify all are correctly world w; auto e = w.make_entity(); - e.set(ChoosenOne{}, Name{"zecsy"}); + w.set(e, ChoosenOne{}, Name{"zecsy"}); - REQUIRE(e.has()); REQUIRE(w.has(e)); - e.remove(); + w.remove(e); - REQUIRE_FALSE(e.has()); REQUIRE_FALSE(w.has(e)); - REQUIRE_FALSE(e.has()); - REQUIRE_FALSE(e.has()); + REQUIRE_FALSE(w.has(e)); + REQUIRE_FALSE(w.has(e)); } TEST_CASE("Create a simple system that processes entities with a specific component and verify it executes correctly") @@ -184,11 +175,11 @@ TEST_CASE("Create a simple system that processes entities with a specific compon world w; auto e0 = w.make_entity(), e1 = w.make_entity(); - e0.set(); //or e0.set(Component{}) - e1.set(Component{20}); + w.set(e0); //or e0.set(Component{}) + w.set(e1, Component{20}); - REQUIRE(e0.get().value == 0); - REQUIRE(e1.get().value == 20); + REQUIRE(w.get(e0).value == 0); + REQUIRE(w.get(e1).value == 20); /* * Really wanna deduce it to w.query([](Component&){}), @@ -199,8 +190,8 @@ TEST_CASE("Create a simple system that processes entities with a specific compon c.value++; }); - REQUIRE(e0.get().value == 1); - REQUIRE(e1.get().value == 21); + REQUIRE(w.get(e0).value == 1); + REQUIRE(w.get(e1).value == 21); } TEST_CASE("Test a system’s ability to query and process only entities with a specific combination of components") @@ -218,18 +209,18 @@ TEST_CASE("Test a system’s ability to query and process only entities with a s world w; auto e0 = w.make_entity(); - e0.set(C0{}, C1{}); + w.set(e0, C0{}, C1{}); auto e1 = w.make_entity(); - e1.set(C0{}); + w.set(e1, C0{}); auto e2 = w.make_entity(); - e2.set(C1{}); + w.set(e2, C1{}); int count = 0; - REQUIRE(e0.get().value == 0); - REQUIRE(e0.get().value == 10); + REQUIRE(w.get(e0).value == 0); + REQUIRE(w.get(e0).value == 10); w.query([&count](C0& c0, C1& c1) { @@ -237,12 +228,12 @@ TEST_CASE("Test a system’s ability to query and process only entities with a s c1.value++; }); - REQUIRE(e0.get().value == 1); - REQUIRE(e0.get().value == 11); + REQUIRE(w.get(e0).value == 1); + REQUIRE(w.get(e0).value == 11); - REQUIRE(e1.get().value == 0); - REQUIRE(e2.get().value == 10); + REQUIRE(w.get(e1).value == 0); + REQUIRE(w.get(e2).value == 10); - REQUIRE_FALSE(e1.has()); - REQUIRE_FALSE(e2.has()); + REQUIRE_FALSE(w.has(e1)); + REQUIRE_FALSE(w.has(e2)); } diff --git a/zecsy.hpp b/zecsy.hpp index 7008783..0a2faf7 100644 --- a/zecsy.hpp +++ b/zecsy.hpp @@ -1,8 +1,6 @@ #pragma once -#include #include #include -#include #include #include #include @@ -22,36 +20,7 @@ namespace zecsy */ using entities_set = std::set; - class entity final - { - public: - entity(class world* w, entity_id id); - - entity() = default; - operator entity_id() const; - - bool is_alive() const; - - template - bool has() const; - - template - T& get(); - - template - void set(); - - template - void set(const T&... comps); - - template - void remove(); - private: - entity_id id = 0; - class world* w = nullptr; - }; - - class component_storage + class component_storage final { public: template @@ -104,7 +73,7 @@ namespace zecsy class world final { public: - entity make_entity(); + entity_id make_entity(); void destroy_entity(entity_id e); bool is_alive(entity_id e) const; @@ -131,27 +100,12 @@ namespace zecsy component_storage storage; }; - inline entity::entity(class world* w, entity_id id): w(w), id(id) - { - - } - - inline entity::operator entity_id() const - { - return id; - } - - inline bool entity::is_alive() const - { - return w && w->is_alive(id); - } - - inline entity world::make_entity() + inline entity_id world::make_entity() { auto id = ++entity_counter; alive_entities.emplace(id); - return entity(this, id); + return id; } inline void world::destroy_entity(entity_id e) @@ -191,30 +145,12 @@ namespace zecsy return *ptr; } - template - inline bool entity::has() const - { - return w->has(id); - } - template inline T& world::get(entity_id e) { return storage.get(e); } - template - inline T& entity::get() - { - return w->get(id); - } - - template - inline void entity::set() - { - (set(T{}), ...); - } - template inline void component_storage::set(entity_id e, const T& comp) { @@ -246,6 +182,12 @@ namespace zecsy indices_dict[typeid(T)][e] = index; } + template + inline void component_storage::set(entity_id e) + { + set(e, T{}); + } + template inline void world::set(entity_id e) { @@ -258,12 +200,6 @@ namespace zecsy storage.set(e, comps...); } - template - inline void entity::set(const T&... comps) - { - w->set(id, comps...); - } - template inline void component_storage::remove(entity_id e) { @@ -283,12 +219,6 @@ namespace zecsy storage.remove(e); } - template - inline void entity::remove() - { - w->remove(id); - } - template inline bool world::has(entity_id e) const {