Created
December 5, 2023 18:06
-
-
Save klechenov/a3708b1385704871d4bbf04634c6d4fc to your computer and use it in GitHub Desktop.
Flecs order_by bug example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <flecs.h> | |
struct ExampleSharedComponent { | |
int zOrder; | |
}; | |
struct AsteroidPrefabTag { | |
}; | |
struct RocketPrefabTag { | |
}; | |
struct AsteroidTag { | |
}; | |
struct RocketTag { | |
}; | |
int main(int argc, char* argv[]) | |
{ | |
flecs::world ecs(argc, argv); | |
ecs.prefab<AsteroidPrefabTag>("PrefabAsteroid") | |
.add<AsteroidTag>() | |
.emplace<ExampleSharedComponent>(1); | |
ecs.prefab<RocketPrefabTag>("PrefabRocket") | |
.add<RocketTag>() | |
.emplace<ExampleSharedComponent>(2); | |
auto s = ecs.query_builder<>("ExampleSystem") | |
.with<ExampleSharedComponent>() | |
.with<AsteroidTag>() | |
.oper(flecs::Or) | |
.with<RocketTag>() | |
.order_by<const ExampleSharedComponent>( | |
[](flecs::entity_t e1, const ExampleSharedComponent* p1, | |
flecs::entity_t e2, const ExampleSharedComponent* p2) { | |
return (p1->zOrder > p2->zOrder) - (p1->zOrder < p2->zOrder); | |
}) | |
.build(); | |
for (size_t i = 0; i < 4; ++i) | |
{ | |
ecs.entity() | |
.is_a(ecs.prefab<RocketPrefabTag>()); | |
} | |
for (size_t i = 0; i < 4; ++i) | |
{ | |
ecs.entity() | |
.is_a(ecs.prefab<AsteroidPrefabTag>()); | |
} | |
s.each([](flecs::entity entity) { | |
if (entity.has<AsteroidTag>()) | |
{ | |
std::cout << "Asteroid" << std::endl; | |
} | |
if (entity.has<RocketTag>()) | |
{ | |
std::cout << "Rocket" << std::endl; | |
} | |
}); | |
std::cout << "" << std::endl; | |
std::cout << "" << std::endl; | |
std::cout << "" << std::endl; | |
std::cout << "" << std::endl; | |
for (size_t i = 0; i < 4; ++i) | |
{ | |
ecs.entity() | |
.is_a(ecs.prefab<RocketPrefabTag>()); | |
} | |
for (size_t i = 0; i < 4; ++i) | |
{ | |
ecs.entity() | |
.is_a(ecs.prefab<AsteroidPrefabTag>()); | |
} | |
s.each([](flecs::entity entity) { | |
if (entity.has<AsteroidTag>()) | |
{ | |
std::cout << "Asteroid" << std::endl; | |
} | |
if (entity.has<RocketTag>()) | |
{ | |
std::cout << "Rocket" << std::endl; | |
} | |
}); | |
ecs.app() | |
.enable_rest() | |
.enable_monitor() | |
.target_fps(60) | |
.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment