examples/queries/
query_group_by.rs1use crate::z_ignore_test_common::*;
2
3use flecs_ecs::prelude::*;
4
5#[derive(Debug, Component)]
6pub struct Position {
7 pub x: f32,
8 pub y: f32,
9}
10
11#[derive(Component)]
12pub struct Tag;
13
14#[derive(Component)]
15pub struct First;
16
17#[derive(Component)]
18pub struct Second;
19
20#[derive(Component)]
21pub struct Third;
22
23#[derive(Component)]
24pub struct Group;
25
26fn main() {
27 let world = World::new();
28
29 world.component::<First>();
30 world.component::<Second>();
31 world.component::<Third>();
32
33 let query = world.query::<&Position>().group_by(Group).build();
34
35 world
36 .entity()
37 .add((Group, Third))
38 .set(Position { x: 1.0, y: 1.0 });
39 world
40 .entity()
41 .add((Group, Second))
42 .set(Position { x: 2.0, y: 2.0 });
43 world
44 .entity()
45 .add((Group, First))
46 .set(Position { x: 3.0, y: 3.0 });
47
48 world
49 .entity()
50 .add((Group, Third))
51 .set(Position { x: 4.0, y: 4.0 })
52 .add(Tag);
53 world
54 .entity()
55 .add((Group, Second))
56 .set(Position { x: 5.0, y: 5.0 })
57 .add(Tag);
58 world
59 .entity()
60 .add((Group, First))
61 .set(Position { x: 6.0, y: 6.0 })
62 .add(Tag);
63
64 println!();
65
66 query.run(|mut it| {
67 while it.next() {
68 let group = world.entity_from_id(it.group_id());
69 let pos = it.field::<Position>(0);
70
71 println!(
72 "Group: {:?} - Table: [{:?}]",
73 group.path().unwrap(),
74 it.archetype()
75 );
76
77 for i in it.iter() {
78 println!(" [{:?}]", pos[i]);
79 }
80
81 println!();
82 }
83 });
84
85 }
104
105#[cfg(feature = "flecs_nightly_tests")]
106#[test]
107fn test() {
108 let output_capture = OutputCapture::capture().unwrap();
109 main();
110 output_capture.test("query_group_by".to_string());
111}