#include void System_iter(void) { flecs::world world; auto entity = world.entity() .set({10, 20}) .set({1, 2}); world.system() .iter([](flecs::iter&it, Position *p, Velocity *v) { for (auto i : it) { p[i].x += v[i].x; p[i].y += v[i].y; } }); world.progress(); const Position *p = entity.get(); test_int(p->x, 11); test_int(p->y, 22); const Velocity *v = entity.get(); test_int(v->x, 1); test_int(v->y, 2); } void System_iter_const(void) { flecs::world world; auto entity = world.entity() .set({10, 20}) .set({1, 2}); world.system() .iter([](flecs::iter&it, Position *p, const Velocity* v) { for (auto i : it) { p[i].x += v[i].x; p[i].y += v[i].y; } }); world.progress(); const Position *p = entity.get(); test_int(p->x, 11); test_int(p->y, 22); const Velocity *v = entity.get(); test_int(v->x, 1); test_int(v->y, 2); } void System_iter_shared(void) { flecs::world world; auto base = world.entity() .set({1, 2}); auto e1 = world.entity() .set({10, 20}) .add(flecs::IsA, base); auto e2 = world.entity() .set({10, 20}) .set({3, 4}); world.system().expr("Velocity(self|up)") .iter([](flecs::iter&it, Position *p) { auto v = it.field(2); if (!it.is_self(2)) { for (auto i : it) { p[i].x += v->x; p[i].y += v->y; } } else { for (auto i : it) { p[i].x += v[i].x; p[i].y += v[i].y; } } }); world.progress(); const Position *p = e1.get(); test_int(p->x, 11); test_int(p->y, 22); p = e2.get(); test_int(p->x, 13); test_int(p->y, 24); } void System_iter_optional(void) { flecs::world world; flecs::component(world, "Mass"); auto e1 = world.entity() .set({10, 20}) .set({1, 2}) .set({1}); auto e2 = world.entity() .set({30, 40}) .set({3, 4}) .set({1}); auto e3 = world.entity() .set({50, 60}); auto e4 = world.entity() .set({70, 80}); world.system() .iter([](flecs::iter& it, Position *p, Velocity *v, Mass *m) { if (it.is_set(2) && it.is_set(3)) { for (auto i : it) { p[i].x += v[i].x * m[i].value; p[i].y += v[i].y * m[i].value; } } else { for (auto i : it) { p[i].x ++; p[i].y ++; } } }); world.progress(); const Position *p = e1.get(); test_int(p->x, 11); test_int(p->y, 22); p = e2.get(); test_int(p->x, 33); test_int(p->y, 44); p = e3.get(); test_int(p->x, 51); test_int(p->y, 61); p = e4.get(); test_int(p->x, 71); test_int(p->y, 81); } void System_each(void) { flecs::world world; auto entity = world.entity() .set({10, 20}) .set({1, 2}); world.system() .each([](flecs::entity e, Position& p, Velocity& v) { p.x += v.x; p.y += v.y; }); world.progress(); const Position *p = entity.get(); test_int(p->x, 11); test_int(p->y, 22); } void System_each_const(void) { flecs::world world; auto entity = world.entity() .set({10, 20}) .set({1, 2}); world.system() .each([](flecs::entity e, Position& p, const Velocity& v) { p.x += v.x; p.y += v.y; }); world.progress(); const Position *p = entity.get(); test_int(p->x, 11); test_int(p->y, 22); } void System_each_shared(void) { flecs::world world; auto base = world.entity() .set({1, 2}); auto e1 = world.entity() .set({10, 20}) .add(flecs::IsA, base); auto e2 = world.entity() .set({10, 20}) .set({3, 4}); world.system() .each([](flecs::entity e, Position& p, const Velocity& v) { p.x += v.x; p.y += v.y; }); world.progress(); const Position *p = e1.get(); test_int(p->x, 11); test_int(p->y, 22); p = e2.get(); test_int(p->x, 13); test_int(p->y, 24); } void System_each_optional(void) { flecs::world world; flecs::component(world, "Mass"); auto e1 = world.entity() .set({10, 20}) .set({1, 2}) .set({1}); auto e2 = world.entity() .set({30, 40}) .set({3, 4}) .set({1}); auto e3 = world.entity() .set({50, 60}); auto e4 = world.entity() .set({70, 80}); world.system() .each([](flecs::entity e, Position& p, Velocity* v, Mass *m) { if (v && m) { p.x += v->x * m->value; p.y += v->y * m->value; } else { p.x ++; p.y ++; } }); world.progress(); const Position *p = e1.get(); test_int(p->x, 11); test_int(p->y, 22); p = e2.get(); test_int(p->x, 33); test_int(p->y, 44); p = e3.get(); test_int(p->x, 51); test_int(p->y, 61); p = e4.get(); test_int(p->x, 71); test_int(p->y, 81); } void System_signature(void) { flecs::world world; auto entity = world.entity() .set({10, 20}) .set({1, 2}); world.system<>().expr("Position, Velocity") .iter([](flecs::iter&it) { flecs::column p(it, 1); flecs::column v(it, 2); for (auto i : it) { p[i].x += v[i].x; p[i].y += v[i].y; } }); world.progress(); const Position *p = entity.get(); test_int(p->x, 11); test_int(p->y, 22); const Velocity *v = entity.get(); test_int(v->x, 1); test_int(v->y, 2); } void System_signature_const(void) { flecs::world world; auto entity = world.entity() .set({10, 20}) .set({1, 2}); world.system<>().expr("Position, [in] Velocity") .iter([](flecs::iter&it) { flecs::column p(it, 1); flecs::column v(it, 2); for (auto i : it) { p[i].x += v[i].x; p[i].y += v[i].y; } }); world.progress(); const Position *p = entity.get(); test_int(p->x, 11); test_int(p->y, 22); const Velocity *v = entity.get(); test_int(v->x, 1); test_int(v->y, 2); } void System_signature_shared(void) { flecs::world world; auto base = world.entity() .set({1, 2}); auto e1 = world.entity() .set({10, 20}) .add(flecs::IsA, base); auto e2 = world.entity() .set({10, 20}) .set({3, 4}); world.system<>().expr("Position, [in] Velocity(self|up)") .iter([](flecs::iter&it) { flecs::column p(it, 1); flecs::column v(it, 2); if (!it.is_self(2)) { for (auto i : it) { p[i].x += v->x; p[i].y += v->y; } } else { for (auto i : it) { p[i].x += v[i].x; p[i].y += v[i].y; } } }); world.progress(); const Position *p = e1.get(); test_int(p->x, 11); test_int(p->y, 22); p = e2.get(); test_int(p->x, 13); test_int(p->y, 24); } void System_signature_optional(void) { flecs::world world; flecs::component(world, "Mass"); auto e1 = world.entity() .set({10, 20}) .set({1, 2}) .set({1}); auto e2 = world.entity() .set({30, 40}) .set({3, 4}) .set({1}); auto e3 = world.entity() .set({50, 60}); auto e4 = world.entity() .set({70, 80}); world.system<>().expr("Position, ?Velocity, ?Mass") .iter([](flecs::iter& it) { flecs::column p(it, 1); flecs::column v(it, 2); flecs::column m(it, 3); if (it.is_set(2) && it.is_set(3)) { for (auto i : it) { p[i].x += v[i].x * m[i].value; p[i].y += v[i].y * m[i].value; } } else { for (auto i : it) { p[i].x ++; p[i].y ++; } } }); world.progress(); const Position *p = e1.get(); test_int(p->x, 11); test_int(p->y, 22); p = e2.get(); test_int(p->x, 33); test_int(p->y, 44); p = e3.get(); test_int(p->x, 51); test_int(p->y, 61); p = e4.get(); test_int(p->x, 71); test_int(p->y, 81); } void System_copy_name_on_create(void) { flecs::world world; char name[6]; strcpy(name, "Hello"); auto system_1 = world.system(name) .iter([](flecs::iter&it, Position *p) {}); strcpy(name, "World"); auto system_2 = world.system(name) .iter([](flecs::iter&it, Position *p) {}); test_assert(system_1.id() != system_2.id()); } void System_nested_system(void) { flecs::world world; auto system_1 = world.system("foo::bar") .iter([](flecs::iter&it, Position *p) {}); test_str(system_1.name().c_str(), "bar"); auto e = world.lookup("foo"); test_assert(e.id() != 0); test_str(e.name().c_str(), "foo"); auto se = e.lookup("bar"); test_assert(se.id() != 0); test_str(se.name().c_str(), "bar"); } void System_empty_signature(void) { flecs::world world; int count = 0; world.system<>() .iter([&](flecs::iter it) { count ++; }); world.progress(); test_int(count, 1); } struct MyTag { }; void System_iter_tag(void) { flecs::world world; int invoked = 0; world.system() .iter([&](flecs::iter it, MyTag*) { invoked ++; }); world.entity() .add(); world.progress(); test_int(invoked, 1); } void System_each_tag(void) { flecs::world world; int invoked = 0; world.system() .each([&](flecs::entity e, MyTag) { invoked ++; }); world.entity() .add(); world.progress(); test_int(invoked, 1); } void System_set_interval(void) { flecs::world world; auto sys = world.system<>() .kind(0) .interval(1.0f) .iter([&](flecs::iter& it) { }); float i = sys.interval(); test_int(i, 1.0f); sys.interval(2.0f); i = sys.interval(); test_int(i, 2.0f); } void System_order_by_type(void) { flecs::world world; world.entity().set({3, 0}); world.entity().set({1, 0}); world.entity().set({5, 0}); world.entity().set({2, 0}); world.entity().set({4, 0}); float last_val = 0; int32_t count = 0; auto sys = world.system() .order_by( [](flecs::entity_t e1, const Position *p1, flecs::entity_t e2, const Position *p2) { return (p1->x > p2->x) - (p1->x < p2->x); }) .each([&](flecs::entity e, const Position& p) { test_assert(p.x > last_val); last_val = p.x; count ++; }); sys.run(); test_int(count, 5); } void System_order_by_id(void) { flecs::world world; auto pos = world.component(); world.entity().set({3, 0}); world.entity().set({1, 0}); world.entity().set({5, 0}); world.entity().set({2, 0}); world.entity().set({4, 0}); float last_val = 0; int32_t count = 0; auto sys = world.system() .order_by(pos, [](flecs::entity_t e1, const void *p1, flecs::entity_t e2, const void *p2) { return (static_cast(p1)->x > static_cast(p2)->x) - (static_cast(p1)->x < static_cast(p2)->x); }) .each([&](flecs::entity e, const Position& p) { test_assert(p.x > last_val); last_val = p.x; count ++; }); sys.run(); test_int(count, 5); } void System_order_by_type_after_create(void) { flecs::world world; world.entity().set({3, 0}); world.entity().set({1, 0}); world.entity().set({5, 0}); world.entity().set({2, 0}); world.entity().set({4, 0}); float last_val = 0; int32_t count = 0; auto sys = world.system() .order_by([](flecs::entity_t e1, const Position *p1, flecs::entity_t e2, const Position *p2) { return (p1->x > p2->x) - (p1->x < p2->x); }) .each([&](flecs::entity e, const Position& p) { test_assert(p.x > last_val); last_val = p.x; count ++; }); sys.run(); test_int(count, 5); } void System_order_by_id_after_create(void) { flecs::world world; auto pos = world.component(); world.entity().set({3, 0}); world.entity().set({1, 0}); world.entity().set({5, 0}); world.entity().set({2, 0}); world.entity().set({4, 0}); float last_val = 0; int32_t count = 0; auto sys = world.system() .order_by(pos, [](flecs::entity_t e1, const void *p1, flecs::entity_t e2, const void *p2) { return (static_cast(p1)->x > static_cast(p2)->x) - (static_cast(p1)->x < static_cast(p2)->x); }) .each([&](flecs::entity e, const Position& p) { test_assert(p.x > last_val); last_val = p.x; count ++; }); sys.run(); test_int(count, 5); } void System_get_query(void) { flecs::world world; world.entity().set({0, 0}); world.entity().set({1, 0}); world.entity().set({2, 0}); int32_t count = 0; auto sys = world.system() .each([&](flecs::entity e, const Position& p) { // Not used }); auto q = sys.query(); q.iter([&](flecs::iter &it) { auto pos = it.field(1); for (auto i : it) { test_int(i, pos[i].x); count ++; } }); test_int(count, 3); } void System_add_from_each(void) { flecs::world world; auto e1 = world.entity().set({0, 0}); auto e2 = world.entity().set({1, 0}); auto e3 = world.entity().set({2, 0}); world.system() .each([](flecs::entity e, const Position& p) { e.add(); // Add is deferred test_assert(!e.has()); }); world.progress(); test_assert(e1.has()); test_assert(e2.has()); test_assert(e3.has()); } void System_delete_from_each(void) { flecs::world world; auto e1 = world.entity().set({0, 0}); auto e2 = world.entity().set({1, 0}); auto e3 = world.entity().set({2, 0}); world.system() .each([](flecs::entity e, const Position& p) { e.destruct(); // Delete is deferred test_assert(e.is_alive()); }); world.progress(); test_assert(!e1.is_alive()); test_assert(!e2.is_alive()); test_assert(!e3.is_alive()); } struct Entity { flecs::entity e; }; void System_add_from_each_world_handle(void) { flecs::world world; auto e1 = world.entity().set({world.entity()}); auto e2 = world.entity().set({world.entity()}); auto e3 = world.entity().set({world.entity()}); world.system() .each([](flecs::entity e, const Entity& c) { c.e.mut(e).add(); }); world.progress(); test_assert(e1.get()->e.has()); test_assert(e2.get()->e.has()); test_assert(e3.get()->e.has()); } void System_new_from_each(void) { flecs::world world; auto e1 = world.entity().set({0, 0}); auto e2 = world.entity().set({0, 0}); auto e3 = world.entity().set({0, 0}); world.system() .each([](flecs::entity e, const Position& p) { e.set({ e.world().entity().add() }); }); world.progress(); test_assert(e1.has()); test_assert(e2.has()); test_assert(e3.has()); test_assert(e1.get()->e.has()); test_assert(e2.get()->e.has()); test_assert(e3.get()->e.has()); } void System_add_from_iter(void) { flecs::world world; auto e1 = world.entity().set({0, 0}); auto e2 = world.entity().set({1, 0}); auto e3 = world.entity().set({2, 0}); world.system() .iter([](flecs::iter& it, const Position* p) { for (auto i : it) { it.entity(i).add(); test_assert(!it.entity(i).has()); } }); world.progress(); test_assert(e1.has()); test_assert(e2.has()); test_assert(e3.has()); } void System_delete_from_iter(void) { flecs::world world; auto e1 = world.entity().set({0, 0}); auto e2 = world.entity().set({1, 0}); auto e3 = world.entity().set({2, 0}); world.system() .iter([](const flecs::iter& it, const Position* p) { for (auto i : it) { it.entity(i).destruct(); // Delete is deferred test_assert(it.entity(i).is_alive()); } }); world.progress(); test_assert(!e1.is_alive()); test_assert(!e2.is_alive()); test_assert(!e3.is_alive()); } void System_add_from_iter_world_handle(void) { flecs::world world; auto e1 = world.entity().set({world.entity()}); auto e2 = world.entity().set({world.entity()}); auto e3 = world.entity().set({world.entity()}); world.system() .iter([](const flecs::iter& it, const Entity* c) { for (auto i : it) { c[i].e.mut(it).add(); } }); world.progress(); test_assert(e1.get()->e.has()); test_assert(e2.get()->e.has()); test_assert(e3.get()->e.has()); } void System_new_from_iter(void) { flecs::world world; auto e1 = world.entity().set({0, 0}); auto e2 = world.entity().set({0, 0}); auto e3 = world.entity().set({0, 0}); world.system() .iter([](const flecs::iter& it, const Position* p) { for (auto i : it) { it.entity(i).set({ it.world().entity().add() }); } }); world.progress(); test_assert(e1.has()); test_assert(e2.has()); test_assert(e3.has()); test_assert(e1.get()->e.has()); test_assert(e2.get()->e.has()); test_assert(e3.get()->e.has()); } void System_each_w_mut_children_it(void) { flecs::world world; auto parent = world.entity().set({0, 0}); auto e1 = world.entity().set({0, 0}).child_of(parent); auto e2 = world.entity().set({0, 0}).child_of(parent); auto e3 = world.entity().set({0, 0}).child_of(parent); int32_t count = 0; world.system() .iter([&](const flecs::iter& it, const Position* p) { for (auto i : it) { it.entity(i).children([&](flecs::entity child) { child.add(); count ++; }); } }); world.progress(); test_int(count, 3); test_assert(e1.has()); test_assert(e2.has()); test_assert(e3.has()); } void System_readonly_children_iter(void) { flecs::world world; auto parent = world.entity(); world.entity().set({ parent }); world.entity().set({1, 0}).child_of(parent); world.entity().set({1, 0}).child_of(parent); world.entity().set({1, 0}).child_of(parent); int32_t count = 0; world.system() .iter([&](const flecs::iter& it, const Entity* c) { for (auto i : it) { c[i].e.children([&](flecs::entity child) { // Dummy code to ensure we can access the entity const Position *p = child.get(); test_int(p->x, 1); test_int(p->y, 0); count ++; }); } }); world.progress(); test_int(count, 3); } void System_rate_filter(void) { flecs::world world; int32_t root_count = 0, root_mult = 1, l1_a_count = 0, l1_a_mult = 1, l1_b_count = 0, l1_b_mult = 2, l1_c_count = 0, l1_c_mult = 3, l2_a_count = 0, l2_a_mult = 2, l2_b_count = 0, l2_b_mult = 4, frame_count = 0; auto root = world.system<>("root") .iter([&](flecs::iter& it) { root_count ++; }); auto l1_a = world.system<>("l1_a") .rate(root, 1) .iter([&](flecs::iter& it) { l1_a_count ++; }); auto l1_b = world.system<>("l1_b") .rate(root, 2) .iter([&](flecs::iter& it) { l1_b_count ++; }); world.system<>("l1_c") .rate(root, 3) .iter([&](flecs::iter& it) { l1_c_count ++; }); world.system<>("l2_a") .rate(l1_a, 2) .iter([&](flecs::iter& it) { l2_a_count ++; }); world.system<>("l2_b") .rate(l1_b, 2) .iter([&](flecs::iter& it) { l2_b_count ++; }); for (int i = 0; i < 30; i ++) { world.progress(); frame_count ++; test_int(root_count, frame_count / root_mult); test_int(l1_a_count, frame_count / l1_a_mult); test_int(l1_b_count, frame_count / l1_b_mult); test_int(l1_c_count, frame_count / l1_c_mult); test_int(l2_a_count, frame_count / l2_a_mult); test_int(l2_b_count, frame_count / l2_b_mult); } } void System_update_rate_filter(void) { flecs::world world; int32_t root_count = 0, root_mult = 1, l1_count = 0, l1_mult = 2, l2_count = 0, l2_mult = 6, frame_count = 0; auto root = world.system<>("root") .iter([&](flecs::iter& it) { root_count ++; }); auto l1 = world.system<>("l1") .rate(root, 2) .iter([&](flecs::iter& it) { l1_count ++; }); world.system<>("l2") .rate(l1, 3) .iter([&](flecs::iter& it) { l2_count ++; }); for (int i = 0; i < 12; i ++) { world.progress(); frame_count ++; test_int(root_count, frame_count / root_mult); test_int(l1_count, frame_count / l1_mult); test_int(l2_count, frame_count / l2_mult); } l1.rate(4); // Run twice as slow l1_mult *= 2; l2_mult *= 2; frame_count = 0; l1_count = 0; l2_count = 0; root_count = 0; for (int i = 0; i < 32; i ++) { world.progress(); frame_count ++; test_int(root_count, frame_count / root_mult); test_int(l1_count, frame_count / l1_mult); test_int(l2_count, frame_count / l2_mult); } } void System_test_auto_defer_each(void) { flecs::world world; struct Value { int value; }; auto e1 = world.entity().add().set({10}); auto e2 = world.entity().add().set({20}); auto e3 = world.entity().add().set({30}); auto s = world.system() .term() .each([](flecs::entity e, Value& v) { v.value ++; e.remove(); }); s.run(); test_assert(!e1.has()); test_assert(!e2.has()); test_assert(!e3.has()); test_assert(e1.has()); test_assert(e2.has()); test_assert(e3.has()); test_int(e1.get()->value, 11); test_int(e2.get()->value, 21); test_int(e3.get()->value, 31); } void System_test_auto_defer_iter(void) { flecs::world world; struct Value { int value; }; auto e1 = world.entity().add().set({10}); auto e2 = world.entity().add().set({20}); auto e3 = world.entity().add().set({30}); auto s = world.system() .term() .iter([](flecs::iter& it, Value *v) { for (auto i : it) { v[i].value ++; it.entity(i).remove(); } }); s.run(); test_assert(!e1.has()); test_assert(!e2.has()); test_assert(!e3.has()); test_assert(e1.has()); test_assert(e2.has()); test_assert(e3.has()); test_int(e1.get()->value, 11); test_int(e2.get()->value, 21); test_int(e3.get()->value, 31); } void System_custom_pipeline(void) { flecs::world world; auto PreFrame = world.entity().add(flecs::Phase); auto OnFrame = world.entity().add(flecs::Phase).depends_on(PreFrame); auto PostFrame = world.entity().add(flecs::Phase).depends_on(OnFrame); auto Tag = world.entity(); flecs::entity pip = world.pipeline() .term(flecs::System) .term(flecs::Phase).cascade(flecs::DependsOn) .term(Tag) .build(); int count = 0; world.system() .kind(PostFrame) .iter([&](flecs::iter it) { test_int(count, 2); count ++; }) .add(Tag); world.system() .kind(OnFrame) .iter([&](flecs::iter it) { test_int(count, 1); count ++; }) .add(Tag); world.system() .kind(PreFrame) .iter([&](flecs::iter it) { test_int(count, 0); count ++; }) .add(Tag); test_int(count, 0); world.set_pipeline(pip); world.progress(); test_int(count, 3); } void System_custom_pipeline_w_kind(void) { flecs::world world; auto Tag = world.entity(); flecs::entity pip = world.pipeline() .term(flecs::System) .term(Tag) .build(); int count = 0; world.system() .kind(Tag) .iter([&](flecs::iter it) { test_int(count, 0); count ++; }); world.system() .kind(Tag) .iter([&](flecs::iter it) { test_int(count, 1); count ++; }); world.system() .kind(Tag) .iter([&](flecs::iter it) { test_int(count, 2); count ++; }); test_int(count, 0); world.set_pipeline(pip); world.progress(); test_int(count, 3); } void System_instanced_query_w_singleton_each(void) { flecs::world ecs; ecs.set({1, 2}); auto e1 = ecs.entity().set({10, 20}); e1.set({e1}); auto e2 = ecs.entity().set({20, 30}); e2.set({e2}); auto e3 = ecs.entity().set({30, 40}); e3.set({e3}); auto e4 = ecs.entity().set({40, 50}); e4.set({e4}); auto e5 = ecs.entity().set({50, 60}); e5.set({e5}); e4.add(); e5.add(); int32_t count = 0; auto s = ecs.system() .arg(3).singleton() .instanced() .each([&](flecs::entity e, Self& s, Position&p, const Velocity& v) { test_assert(e == s.value); p.x += v.x; p.y += v.y; count ++; }); s.run(); test_int(count, 5); test_assert(e1.get([](const Position& p) { test_int(p.x, 11); test_int(p.y, 22); })); test_assert(e2.get([](const Position& p) { test_int(p.x, 21); test_int(p.y, 32); })); test_assert(e3.get([](const Position& p) { test_int(p.x, 31); test_int(p.y, 42); })); test_assert(e4.get([](const Position& p) { test_int(p.x, 41); test_int(p.y, 52); })); test_assert(e5.get([](const Position& p) { test_int(p.x, 51); test_int(p.y, 62); })); } void System_instanced_query_w_base_each(void) { flecs::world ecs; auto base = ecs.entity().set({1, 2}); auto e1 = ecs.entity().is_a(base).set({10, 20}); e1.set({e1}); auto e2 = ecs.entity().is_a(base).set({20, 30}); e2.set({e2}); auto e3 = ecs.entity().is_a(base).set({30, 40}); e3.set({e3}); auto e4 = ecs.entity().is_a(base).set({40, 50}).add(); e4.set({e4}); auto e5 = ecs.entity().is_a(base).set({50, 60}).add(); e5.set({e5}); auto e6 = ecs.entity().set({60, 70}).set({2, 3}); e6.set({e6}); auto e7 = ecs.entity().set({70, 80}).set({4, 5}); e7.set({e7}); int32_t count = 0; auto s = ecs.system() .instanced() .each([&](flecs::entity e, Self& s, Position&p, const Velocity& v) { test_assert(e == s.value); p.x += v.x; p.y += v.y; count ++; }); s.run(); test_int(count, 7); test_assert(e1.get([](const Position& p) { test_int(p.x, 11); test_int(p.y, 22); })); test_assert(e2.get([](const Position& p) { test_int(p.x, 21); test_int(p.y, 32); })); test_assert(e3.get([](const Position& p) { test_int(p.x, 31); test_int(p.y, 42); })); test_assert(e4.get([](const Position& p) { test_int(p.x, 41); test_int(p.y, 52); })); test_assert(e5.get([](const Position& p) { test_int(p.x, 51); test_int(p.y, 62); })); test_assert(e6.get([](const Position& p) { test_int(p.x, 62); test_int(p.y, 73); })); test_assert(e7.get([](const Position& p) { test_int(p.x, 74); test_int(p.y, 85); })); } void System_un_instanced_query_w_singleton_each(void) { flecs::world ecs; ecs.set({1, 2}); auto e1 = ecs.entity().set({10, 20}); e1.set({e1}); auto e2 = ecs.entity().set({20, 30}); e2.set({e2}); auto e3 = ecs.entity().set({30, 40}); e3.set({e3}); auto e4 = ecs.entity().set({40, 50}); e4.set({e4}); auto e5 = ecs.entity().set({50, 60}); e5.set({e5}); e4.add(); e5.add(); int32_t count = 0; auto s = ecs.system() .arg(3).singleton() .each([&](flecs::entity e, Self& s, Position&p, const Velocity& v) { test_assert(e == s.value); p.x += v.x; p.y += v.y; count ++; }); s.run(); test_int(count, 5); test_assert(e1.get([](const Position& p) { test_int(p.x, 11); test_int(p.y, 22); })); test_assert(e2.get([](const Position& p) { test_int(p.x, 21); test_int(p.y, 32); })); test_assert(e3.get([](const Position& p) { test_int(p.x, 31); test_int(p.y, 42); })); test_assert(e4.get([](const Position& p) { test_int(p.x, 41); test_int(p.y, 52); })); test_assert(e5.get([](const Position& p) { test_int(p.x, 51); test_int(p.y, 62); })); } void System_un_instanced_query_w_base_each(void) { flecs::world ecs; auto base = ecs.entity().set({1, 2}); auto e1 = ecs.entity().is_a(base).set({10, 20}); e1.set({e1}); auto e2 = ecs.entity().is_a(base).set({20, 30}); e2.set({e2}); auto e3 = ecs.entity().is_a(base).set({30, 40}); e3.set({e3}); auto e4 = ecs.entity().is_a(base).set({40, 50}).add(); e4.set({e4}); auto e5 = ecs.entity().is_a(base).set({50, 60}).add(); e5.set({e5}); auto e6 = ecs.entity().set({60, 70}).set({2, 3}); e6.set({e6}); auto e7 = ecs.entity().set({70, 80}).set({4, 5}); e7.set({e7}); int32_t count = 0; auto s = ecs.system() .each([&](flecs::entity e, Self& s, Position&p, const Velocity& v) { test_assert(e == s.value); p.x += v.x; p.y += v.y; count ++; }); s.run(); test_int(count, 7); test_assert(e1.get([](const Position& p) { test_int(p.x, 11); test_int(p.y, 22); })); test_assert(e2.get([](const Position& p) { test_int(p.x, 21); test_int(p.y, 32); })); test_assert(e3.get([](const Position& p) { test_int(p.x, 31); test_int(p.y, 42); })); test_assert(e4.get([](const Position& p) { test_int(p.x, 41); test_int(p.y, 52); })); test_assert(e5.get([](const Position& p) { test_int(p.x, 51); test_int(p.y, 62); })); test_assert(e6.get([](const Position& p) { test_int(p.x, 62); test_int(p.y, 73); })); test_assert(e7.get([](const Position& p) { test_int(p.x, 74); test_int(p.y, 85); })); } void System_instanced_query_w_singleton_iter(void) { flecs::world ecs; ecs.set({1, 2}); auto e1 = ecs.entity().set({10, 20}); e1.set({e1}); auto e2 = ecs.entity().set({20, 30}); e2.set({e2}); auto e3 = ecs.entity().set({30, 40}); e3.set({e3}); auto e4 = ecs.entity().set({40, 50}); e4.set({e4}); auto e5 = ecs.entity().set({50, 60}); e5.set({e5}); e4.add(); e5.add(); int32_t count = 0; auto s = ecs.system() .arg(3).singleton() .instanced() .iter([&](flecs::iter it, Self* s, Position* p, const Velocity* v) { test_assert(it.count() > 1); for (auto i : it) { p[i].x += v->x; p[i].y += v->y; test_assert(it.entity(i) == s[i].value); count ++; } }); s.run(); test_int(count, 5); test_assert(e1.get([](const Position& p) { test_int(p.x, 11); test_int(p.y, 22); })); test_assert(e2.get([](const Position& p) { test_int(p.x, 21); test_int(p.y, 32); })); test_assert(e3.get([](const Position& p) { test_int(p.x, 31); test_int(p.y, 42); })); test_assert(e4.get([](const Position& p) { test_int(p.x, 41); test_int(p.y, 52); })); test_assert(e5.get([](const Position& p) { test_int(p.x, 51); test_int(p.y, 62); })); } void System_instanced_query_w_base_iter(void) { flecs::world ecs; auto base = ecs.entity().set({1, 2}); auto e1 = ecs.entity().is_a(base).set({10, 20}); e1.set({e1}); auto e2 = ecs.entity().is_a(base).set({20, 30}); e2.set({e2}); auto e3 = ecs.entity().is_a(base).set({30, 40}); e3.set({e3}); auto e4 = ecs.entity().is_a(base).set({40, 50}).add(); e4.set({e4}); auto e5 = ecs.entity().is_a(base).set({50, 60}).add(); e5.set({e5}); auto e6 = ecs.entity().set({60, 70}).set({2, 3}); e6.set({e6}); auto e7 = ecs.entity().set({70, 80}).set({4, 5}); e7.set({e7}); int32_t count = 0; auto s = ecs.system() .instanced() .iter([&](flecs::iter it, Self* s, Position* p, const Velocity* v) { test_assert(it.count() > 1); for (auto i : it) { if (it.is_self(3)) { p[i].x += v[i].x; p[i].y += v[i].y; } else { p[i].x += v->x; p[i].y += v->y; } test_assert(it.entity(i) == s[i].value); count ++; } }); s.run(); test_int(count, 7); test_assert(e1.get([](const Position& p) { test_int(p.x, 11); test_int(p.y, 22); })); test_assert(e2.get([](const Position& p) { test_int(p.x, 21); test_int(p.y, 32); })); test_assert(e3.get([](const Position& p) { test_int(p.x, 31); test_int(p.y, 42); })); test_assert(e4.get([](const Position& p) { test_int(p.x, 41); test_int(p.y, 52); })); test_assert(e5.get([](const Position& p) { test_int(p.x, 51); test_int(p.y, 62); })); test_assert(e6.get([](const Position& p) { test_int(p.x, 62); test_int(p.y, 73); })); test_assert(e7.get([](const Position& p) { test_int(p.x, 74); test_int(p.y, 85); })); } void System_un_instanced_query_w_singleton_iter(void) { flecs::world ecs; ecs.set({1, 2}); auto e1 = ecs.entity().set({10, 20}); e1.set({e1}); auto e2 = ecs.entity().set({20, 30}); e2.set({e2}); auto e3 = ecs.entity().set({30, 40}); e3.set({e3}); auto e4 = ecs.entity().set({40, 50}); e4.set({e4}); auto e5 = ecs.entity().set({50, 60}); e5.set({e5}); e4.add(); e5.add(); int32_t count = 0; auto s = ecs.system() .arg(3).singleton() .iter([&](flecs::iter it, Self* s, Position* p, const Velocity* v) { test_assert(it.count() == 1); for (auto i : it) { p[i].x += v[i].x; p[i].y += v[i].y; test_assert(it.entity(i) == s[i].value); count ++; } }); s.run(); test_int(count, 5); test_assert(e1.get([](const Position& p) { test_int(p.x, 11); test_int(p.y, 22); })); test_assert(e2.get([](const Position& p) { test_int(p.x, 21); test_int(p.y, 32); })); test_assert(e3.get([](const Position& p) { test_int(p.x, 31); test_int(p.y, 42); })); test_assert(e4.get([](const Position& p) { test_int(p.x, 41); test_int(p.y, 52); })); test_assert(e5.get([](const Position& p) { test_int(p.x, 51); test_int(p.y, 62); })); } void System_un_instanced_query_w_base_iter(void) { flecs::world ecs; auto base = ecs.entity().set({1, 2}); auto e1 = ecs.entity().is_a(base).set({10, 20}); e1.set({e1}); auto e2 = ecs.entity().is_a(base).set({20, 30}); e2.set({e2}); auto e3 = ecs.entity().is_a(base).set({30, 40}); e3.set({e3}); auto e4 = ecs.entity().is_a(base).set({40, 50}).add(); e4.set({e4}); auto e5 = ecs.entity().is_a(base).set({50, 60}).add(); e5.set({e5}); auto e6 = ecs.entity().set({60, 70}).set({2, 3}); e6.set({e6}); auto e7 = ecs.entity().set({70, 80}).set({4, 5}); e7.set({e7}); int32_t count = 0; auto s = ecs.system() .iter([&](flecs::iter it, Self* s, Position* p, const Velocity* v) { for (auto i : it) { p[i].x += v[i].x; p[i].y += v[i].y; test_assert(it.entity(i) == s[i].value); count ++; } }); s.run(); test_int(count, 7); test_assert(e1.get([](const Position& p) { test_int(p.x, 11); test_int(p.y, 22); })); test_assert(e2.get([](const Position& p) { test_int(p.x, 21); test_int(p.y, 32); })); test_assert(e3.get([](const Position& p) { test_int(p.x, 31); test_int(p.y, 42); })); test_assert(e4.get([](const Position& p) { test_int(p.x, 41); test_int(p.y, 52); })); test_assert(e5.get([](const Position& p) { test_int(p.x, 51); test_int(p.y, 62); })); test_assert(e6.get([](const Position& p) { test_int(p.x, 62); test_int(p.y, 73); })); test_assert(e7.get([](const Position& p) { test_int(p.x, 74); test_int(p.y, 85); })); } void System_create_w_no_template_args(void) { flecs::world world; auto entity = world.entity() .set({10, 20}); int32_t count = 0; auto s = world.system() .term() .each([&](flecs::entity e) { test_assert(e == entity); count ++; }); s.run(); test_int(count, 1); } struct PipelineType {}; struct First {}; struct Second {}; void System_system_w_type_kind_type_pipeline(void) { flecs::world world; world.component() .add(flecs::Phase) .depends_on( world.component() .add(flecs::Phase) ); world.pipeline() .term(flecs::System) .term(flecs::Phase).cascade(flecs::DependsOn) .build(); world.set_pipeline(); auto entity = world.entity().add(); int32_t s1_count = 0; int32_t s2_count = 0; world.system() .kind() .each([&](flecs::entity e, Tag) { test_assert(e == entity); test_int(s1_count, 0); test_int(s2_count, 1); s1_count ++; }); world.system() .kind() .each([&](flecs::entity e, Tag) { test_assert(e == entity); test_int(s1_count, 0); s2_count ++; }); world.progress(); test_int(s1_count, 1); test_int(s2_count, 1); } void System_default_ctor(void) { flecs::world world; flecs::system sys_var; int count = 0; auto sys = world.system() .each([&](flecs::entity e, Position& p) { test_int(p.x, 10); test_int(p.y, 20); count ++; }); world.entity().set({10, 20}); sys_var = sys; sys_var.run(); test_int(count, 1); } void System_entity_ctor(void) { flecs::world world; uint32_t invoked = 0; flecs::entity sys = world.system<>() .iter([&](flecs::iter& it) { invoked ++; }); auto sys_from_id = world.system(sys); sys_from_id.run(); test_int(invoked, 1); } void System_ensure_instanced_w_each(void) { flecs::world world; flecs::entity e1 = world.entity().set({10, 20}); int32_t count = 0; auto sys = world.system() .each([&](flecs::iter& it, size_t i, Position&) { test_assert(it.c_ptr()->flags & EcsIterIsInstanced); test_assert(it.entity(i) == e1); count ++; }); auto q = sys.query(); auto f = q.filter(); const ecs_filter_t *c_f = f; test_assert(c_f->flags & EcsIterIsInstanced); test_int(count, 0); sys.run(); test_int(count, 1); } void System_multithread_system_w_query_each(void) { flecs::world world; world.set_threads(2); flecs::entity e = world.entity() .set({10, 20}) .set({1, 2}); auto q = world.query(); world.system() .multi_threaded() .each([&](flecs::entity e, Position& p) { q.each(e, [&](Velocity& v) { p.x += v.x; p.y += v.y; }); }); world.progress(); const Position *p = e.get(); test_int(p->x, 11); test_int(p->y, 22); } void System_multithread_system_w_query_each_w_iter(void) { flecs::world world; world.set_threads(2); flecs::entity e = world.entity() .set({10, 20}) .set({1, 2}); auto q = world.query(); world.system() .multi_threaded() .each([&](flecs::iter& it, size_t, Position& p) { q.each(it, [&](Velocity& v) { p.x += v.x; p.y += v.y; }); }); world.progress(); const Position *p = e.get(); test_int(p->x, 11); test_int(p->y, 22); } void System_multithread_system_w_query_each_w_world(void) { flecs::world world; world.set_threads(2); flecs::entity e = world.entity() .set({10, 20}) .set({1, 2}); auto q = world.query(); world.system() .multi_threaded() .each([&](flecs::iter& it, size_t, Position& p) { q.each(it.world(), [&](Velocity& v) { p.x += v.x; p.y += v.y; }); }); world.progress(); const Position *p = e.get(); test_int(p->x, 11); test_int(p->y, 22); } void System_multithread_system_w_query_iter(void) { flecs::world world; world.set_threads(2); flecs::entity e = world.entity() .set({10, 20}) .set({1, 2}); auto q = world.query(); world.system() .multi_threaded() .each([&](flecs::entity e, Position& p) { q.iter(e, [&](flecs::iter& it, Velocity* v) { for (auto i : it) { p.x += v[i].x; p.y += v[i].y; } }); }); world.progress(); const Position *p = e.get(); test_int(p->x, 11); test_int(p->y, 22); } void System_multithread_system_w_query_iter_w_iter(void) { flecs::world world; world.set_threads(2); flecs::entity e = world.entity() .set({10, 20}) .set({1, 2}); auto q = world.query(); world.system() .multi_threaded() .each([&](flecs::iter& it, size_t, Position& p) { q.iter(it, [&](flecs::iter& it, Velocity* v) { for (auto i : it) { p.x += v[i].x; p.y += v[i].y; } }); }); world.progress(); const Position *p = e.get(); test_int(p->x, 11); test_int(p->y, 22); } void System_multithread_system_w_query_iter_w_world(void) { flecs::world world; world.set_threads(2); flecs::entity e = world.entity() .set({10, 20}) .set({1, 2}); auto q = world.query(); world.system() .multi_threaded() .each([&](flecs::iter& it, size_t, Position& p) { q.iter(it.world(), [&](flecs::iter& it, Velocity* v) { for (auto i : it) { p.x += v[i].x; p.y += v[i].y; } }); }); world.progress(); const Position *p = e.get(); test_int(p->x, 11); test_int(p->y, 22); } void System_run_callback(void) { flecs::world world; auto entity = world.entity() .set({10, 20}) .set({1, 2}); world.system() .run([](flecs::iter_t *it) { while (ecs_iter_next(it)) { it->callback(it); } }) .iter([](flecs::iter&it, Position *p, const Velocity* v) { for (auto i : it) { p[i].x += v[i].x; p[i].y += v[i].y; } }); world.progress(); const Position *p = entity.get(); test_int(p->x, 11); test_int(p->y, 22); const Velocity *v = entity.get(); test_int(v->x, 1); test_int(v->y, 2); } void System_startup_system(void) { flecs::world ecs; int32_t count_a = 0, count_b = 0; ecs.system() .kind(flecs::OnStart) .iter([&](flecs::iter& it) { test_assert(it.delta_time() == 0); count_a ++; }); ecs.system() .kind(flecs::OnUpdate) .iter([&](flecs::iter& it) { test_assert(it.delta_time() != 0); count_b ++; }); ecs.progress(); test_int(count_a, 1); test_int(count_b, 1); ecs.progress(); test_int(count_a, 1); test_int(count_b, 2); } void System_interval_tick_source(void) { flecs::world ecs; flecs::timer t = ecs.timer().interval(2.1); flecs::Timer *timer = t.get_mut(); timer->time = 0; int32_t sys_a_invoked = 0, sys_b_invoked = 0; ecs.system() .tick_source(t) .iter([&](flecs::iter& it) { sys_a_invoked ++; }); ecs.system() .tick_source(t) .iter([&](flecs::iter& it) { sys_b_invoked ++; }); ecs.progress(1.0); test_int(0, sys_a_invoked); test_int(0, sys_b_invoked); ecs.progress(1.0); test_int(0, sys_a_invoked); test_int(0, sys_b_invoked); ecs.progress(1.0); test_int(1, sys_a_invoked); test_int(1, sys_b_invoked); } void System_rate_tick_source(void) { flecs::world ecs; flecs::timer t = ecs.timer().rate(3); int32_t sys_a_invoked = 0, sys_b_invoked = 0; ecs.system() .tick_source(t) .iter([&](flecs::iter& it) { sys_a_invoked ++; }); ecs.system() .tick_source(t) .iter([&](flecs::iter& it) { sys_b_invoked ++; }); ecs.progress(1.0); test_int(0, sys_a_invoked); test_int(0, sys_b_invoked); ecs.progress(1.0); test_int(0, sys_a_invoked); test_int(0, sys_b_invoked); ecs.progress(1.0); test_int(1, sys_a_invoked); test_int(1, sys_b_invoked); } void System_nested_rate_tick_source(void) { flecs::world ecs; flecs::timer t_3 = ecs.timer().rate(3); flecs::timer t_6 = ecs.timer().rate(2, t_3); int32_t sys_a_invoked = 0, sys_b_invoked = 0; ecs.system() .tick_source(t_3) .iter([&](flecs::iter& it) { sys_a_invoked ++; }); ecs.system() .tick_source(t_6) .iter([&](flecs::iter& it) { sys_b_invoked ++; }); ecs.progress(1.0); test_int(0, sys_a_invoked); test_int(0, sys_b_invoked); ecs.progress(1.0); test_int(0, sys_a_invoked); test_int(0, sys_b_invoked); ecs.progress(1.0); test_int(1, sys_a_invoked); test_int(0, sys_b_invoked); ecs.progress(1.0); test_int(1, sys_a_invoked); test_int(0, sys_b_invoked); ecs.progress(1.0); test_int(1, sys_a_invoked); test_int(0, sys_b_invoked); ecs.progress(1.0); test_int(2, sys_a_invoked); test_int(1, sys_b_invoked); } void System_table_get(void) { flecs::world ecs; flecs::entity e1 = ecs.entity().set({10, 20}); flecs::entity e2 = ecs.entity().set({20, 30}); auto s = ecs.system() .with() .each([&](flecs::iter& iter, size_t index) { flecs::entity e = iter.entity(index); const Position *p = &iter.table().get()[index]; test_assert(p != nullptr); test_assert(e == e1 || e == e2); if (e == e1) { test_int(p->x, 10); test_int(p->y, 20); } else if (e == e2) { test_int(p->x, 20); test_int(p->y, 30); } }); s.run(); } void System_range_get(void) { flecs::world ecs; flecs::entity e1 = ecs.entity().set({10, 20}); flecs::entity e2 = ecs.entity().set({20, 30}); auto s = ecs.system() .with() .each([&](flecs::iter& iter, size_t index) { flecs::entity e = iter.entity(index); const Position *p = &iter.range().get()[index]; test_assert(p != nullptr); test_assert(e == e1 || e == e2); if (e == e1) { test_int(p->x, 10); test_int(p->y, 20); } else if (e == e2) { test_int(p->x, 20); test_int(p->y, 30); } }); s.run(); } void System_randomize_timers(void) { flecs::world ecs; flecs::entity s1 = ecs.system() .interval(1.0) .iter([](flecs::iter& it) { }); { const flecs::Timer *t = s1.get(); test_assert(t != nullptr); test_assert(t->time == 0); } ecs.randomize_timers(); flecs::entity s2 = ecs.system() .interval(1.0) .iter([](flecs::iter& it) { }); { const flecs::Timer *t = s1.get(); test_assert(t != nullptr); test_assert(t->time != 0); } { const flecs::Timer *t = s2.get(); test_assert(t != nullptr); test_assert(t->time != 0); } } void System_optional_pair_term(void) { flecs::world ecs; ecs.entity() .add() .emplace(1.0f, 2.0f); ecs.entity() .add(); int32_t with_pair = 0, without_pair = 0; ecs.system*>() .with() .each([&](flecs::entity e, Position* p) { if (p) { with_pair++; test_flt(1.0f, p->x); test_flt(2.0f, p->y); } else { without_pair++; } }); ecs.progress(1.0); test_int(1, with_pair); test_int(1, without_pair); }