diff --git a/tests/zecsy.cpp b/tests/zecsy.cpp index ac863bd..9caf72f 100644 --- a/tests/zecsy.cpp +++ b/tests/zecsy.cpp @@ -1,4 +1,5 @@ #include +#include #include #define CATCH_CONFIG_MAIN #include @@ -202,4 +203,60 @@ TEST_CASE("Create a simple system that processes entities with a specific compon REQUIRE(e0.get().value == 0); REQUIRE(e1.get().value == 20); + + /* + * Really wanna deduce it to w.query([](Component&){}), + * but I have some troubles with it + */ + w.query([](Component& c) + { + c.value++; + }); + + REQUIRE(e0.get().value == 1); + REQUIRE(e1.get().value == 21); +} + +TEST_CASE("Test a system’s ability to query and process only entities with a specific combination of components") +{ + struct C0 + { + int value = 0; + }; + + struct C1 + { + int value = 10; + }; + + world w; + + auto e0 = w.make_entity(); + e0.set(C0{}, C1{}); + + auto e1 = w.make_entity(); + e1.set(C0{}); + + auto e2 = w.make_entity(); + e2.set(C1{}); + + int count = 0; + + REQUIRE(e0.get().value == 0); + REQUIRE(e0.get().value == 10); + + w.query([&count](C0& c0, C1& c1) + { + c0.value++; + c1.value++; + }); + + REQUIRE(e0.get().value == 1); + REQUIRE(e0.get().value == 11); + + REQUIRE(e1.get().value == 0); + REQUIRE(e2.get().value == 10); + + REQUIRE_FALSE(e1.has()); + REQUIRE_FALSE(e2.has()); } diff --git a/zecsy.hpp b/zecsy.hpp index 4f28b08..7008783 100644 --- a/zecsy.hpp +++ b/zecsy.hpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -121,6 +122,9 @@ namespace zecsy template void remove(entity_id e); + + template + void query(Callable&& system); private: entities_set alive_entities; entity_id entity_counter = 0; @@ -321,4 +325,16 @@ namespace zecsy remove(e); (remove(e), ...); } + + template + inline void world::query(Callable&& system) + { + for(auto e: alive_entities) + { + if((has(e))) + { + system(get(e)...); + } + } + } };