Queries also provide entity_id

This commit is contained in:
NukeBird 2025-02-20 00:17:11 +03:00
parent d1bafb4c46
commit 27cf20fac3
3 changed files with 8 additions and 12 deletions

View file

@ -1,6 +1,6 @@
#pragma once
#include <functional>
#include <vector>
#include <vector>
namespace zecsy
{

View file

@ -2,8 +2,8 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch_all.hpp>
#include "../zecsy.hpp"
#include "../system_scheduler.hpp"
#include "../zecsy.hpp"
using namespace zecsy;
@ -193,7 +193,7 @@ TEST_CASE("Create a simple system that processes entities with a specific "
* Really wanna deduce it to w.query([](Component&){}),
* but I have some troubles with it
*/
w.query<Component>([](Component& c) { c.value++; });
w.query<Component>([](entity_id e, Component& c) { c.value++; });
REQUIRE(w.get<Component>(e0).value == 1);
REQUIRE(w.get<Component>(e1).value == 21);
@ -223,20 +223,16 @@ TEST_CASE("Test a systems ability to query and process only entities with a "
auto e2 = w.make_entity();
w.set(e2, C1{});
int count = 0;
REQUIRE(w.get<C0>(e0).value == 0);
REQUIRE(w.get<C1>(e0).value == 10);
w.query<C0, C1>([&count](C0& c0, C1& c1)
w.query<C0, C1>([e0](entity_id e, C0& c0, C1& c1)
{
REQUIRE(e == e0);
c0.value++;
c1.value++;
count++;
});
REQUIRE(count == 1);
REQUIRE(w.get<C0>(e0).value == 1);
REQUIRE(w.get<C1>(e0).value == 11);

View file

@ -63,7 +63,7 @@ namespace zecsy
void remove(entity_id e);
template<Component... T>
void query(std::invocable<T&...> auto&& system);
void query(std::invocable<entity_id, T&...> auto&& system);
private:
using comp_id = size_t;
@ -228,13 +228,13 @@ namespace zecsy
}
template<Component... T>
inline void world::query(std::invocable<T&...> auto&& system)
inline void world::query(std::invocable<entity_id, T&...> auto&& system)
{
for(auto e: alive_entities)
{
if((has<T...>(e)))
{
system(get<T>(e)...);
system(e, get<T>(e)...);
}
}
}