Add queries/systems support
This commit is contained in:
		
							parent
							
								
									586f7b224e
								
							
						
					
					
						commit
						70e1d0bad4
					
				
					 2 changed files with 73 additions and 0 deletions
				
			
		|  | @ -1,4 +1,5 @@ | ||||||
| #include <catch2/catch_test_macros.hpp> | #include <catch2/catch_test_macros.hpp> | ||||||
|  | #include <concepts> | ||||||
| #include <string> | #include <string> | ||||||
| #define CATCH_CONFIG_MAIN | #define CATCH_CONFIG_MAIN | ||||||
| #include <catch2/catch_all.hpp> | #include <catch2/catch_all.hpp> | ||||||
|  | @ -202,4 +203,60 @@ TEST_CASE("Create a simple system that processes entities with a specific compon | ||||||
| 
 | 
 | ||||||
|     REQUIRE(e0.get<Component>().value == 0); |     REQUIRE(e0.get<Component>().value == 0); | ||||||
|     REQUIRE(e1.get<Component>().value == 20); |     REQUIRE(e1.get<Component>().value == 20); | ||||||
|  |     | ||||||
|  |     /*
 | ||||||
|  |      * Really wanna deduce it to w.query([](Component&){}), | ||||||
|  |      * but I have some troubles with it | ||||||
|  |      */ | ||||||
|  |     w.query<Component>([](Component& c)  | ||||||
|  |     { | ||||||
|  |         c.value++; | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     REQUIRE(e0.get<Component>().value == 1); | ||||||
|  |     REQUIRE(e1.get<Component>().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<C0>().value == 0); | ||||||
|  |     REQUIRE(e0.get<C1>().value == 10); | ||||||
|  | 
 | ||||||
|  |     w.query<C0, C1>([&count](C0& c0, C1& c1) | ||||||
|  |     { | ||||||
|  |         c0.value++; | ||||||
|  |         c1.value++; | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     REQUIRE(e0.get<C0>().value == 1); | ||||||
|  |     REQUIRE(e0.get<C1>().value == 11); | ||||||
|  | 
 | ||||||
|  |     REQUIRE(e1.get<C0>().value == 0); | ||||||
|  |     REQUIRE(e2.get<C1>().value == 10); | ||||||
|  | 
 | ||||||
|  |     REQUIRE_FALSE(e1.has<C1>()); | ||||||
|  |     REQUIRE_FALSE(e2.has<C0>()); | ||||||
| } | } | ||||||
|  |  | ||||||
							
								
								
									
										16
									
								
								zecsy.hpp
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								zecsy.hpp
									
									
									
									
									
								
							|  | @ -2,6 +2,7 @@ | ||||||
| #include <catch2/internal/catch_console_colour.hpp> | #include <catch2/internal/catch_console_colour.hpp> | ||||||
| #include <cstdint> | #include <cstdint> | ||||||
| #include <format> | #include <format> | ||||||
|  | #include <functional> | ||||||
| #include <queue> | #include <queue> | ||||||
| #include <stdexcept> | #include <stdexcept> | ||||||
| #include <typeindex> | #include <typeindex> | ||||||
|  | @ -121,6 +122,9 @@ namespace zecsy | ||||||
| 
 | 
 | ||||||
|         template<typename... T> |         template<typename... T> | ||||||
|         void remove(entity_id e); |         void remove(entity_id e); | ||||||
|  | 
 | ||||||
|  |         template<typename... T, typename Callable> | ||||||
|  |         void query(Callable&& system); | ||||||
|     private: |     private: | ||||||
|         entities_set alive_entities; |         entities_set alive_entities; | ||||||
|         entity_id entity_counter = 0; |         entity_id entity_counter = 0; | ||||||
|  | @ -321,4 +325,16 @@ namespace zecsy | ||||||
|         remove<Second>(e); |         remove<Second>(e); | ||||||
|         (remove<Rest>(e), ...); |         (remove<Rest>(e), ...); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     template<typename... T, typename Callable> | ||||||
|  |     inline void world::query(Callable&& system) | ||||||
|  |     { | ||||||
|  |         for(auto e: alive_entities) | ||||||
|  |         { | ||||||
|  |             if((has<T...>(e))) | ||||||
|  |             { | ||||||
|  |                 system(get<T>(e)...); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
| }; | }; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue