1#include <multiple_queries.h>
2#include <iostream>
3
4
5
6
7
8struct Position {
9 float x, y;
10};
11
12struct Bee {
13 static const int MaxRange = 10;
15};
16
17struct Flower { };
18
19float randf(int n) {
20 return static_cast<float>(rand() % n);
21}
22
23float pow2(float n) {
24 return n * n;
25}
26
27int main(int, char *[]) {
29
30
31
32
33
34
35
36
38 ecs.
query<
const Position, Bee>(
"BeeQuery");
39
42 .with<Flower>()
43 .build();
44
45
46 ecs.
system(
"FlowersAndTheBees")
47
48
51 float dsqr_min = 2 * pow2(Bee::MaxRange);
52
54
55 flowers.each([&](
flecs::entity flower,
const Position& p_flower) {
56 float dsqr = pow2(p_bee.x - p_flower.x) +
57 pow2(p_bee.y - p_flower.y);
58
59 if (dsqr < dsqr_min) {
60 dsqr_min = dsqr;
61 b.flower = flower;
62 }
63 });
64
65 if (b.flower) {
66 std::cout <<
"Bee " << bee.
path()
67 << " picked flower " << b.flower.path()
68 << std::endl;
69 }
70 });
71 });
72
73
78 if (bee1 > bee2) {
79 if (b1.flower && b1.flower == b2.flower) {
80 std::cout <<
"Bee " << bee1.
path()
81 <<
" and bee " << bee2.
path()
82 << " bumped into each other"
83 << std::endl;
84 }
85 }
86 });
87 });
88 });
89
90
91 for (int i = 0; i < 5; i ++) {
93 .add<Bee>()
94 .set(Position{ randf(20), randf(20) });
95 }
96
97 for (int i = 0; i < 10; i ++) {
99 .add<Flower>()
100 .set(Position{ randf(20), randf(20) });
101 }
102
104
105
106
107
108
109
110
111
112
113}
flecs::system system(flecs::entity e) const
Upcast an entity to a system.
conditional_t< sizeof...(Args)==0 &&_::is_sparse_query< Comps... >::value, flecs::sparse_query< Comps... >, flecs::query< Comps... > > query(Args &&... args) const
Create a query.
flecs::query_builder< Comps... > query_builder(Args &&... args) const
Create a query builder.
flecs::entity entity(Args &&... args) const
Create an entity.
bool progress(ecs_ftime_t delta_time=0.0) const
Progress the world one tick.
flecs::string path(const char *sep="::", const char *init_sep="::") const
Return the entity path.
Class for iterating over query results.
void each(Func &&func) const
Each iterator.
system & run(Func &&func)
Replace the system's run callback.