-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathquery_basics.rs
85 lines (70 loc) · 2.39 KB
/
query_basics.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use crate::z_ignore_test_common::*;
use flecs_ecs::prelude::*;
#[derive(Debug, Component)]
pub struct Position {
pub x: f32,
pub y: f32,
}
#[derive(Debug, Component)]
pub struct Velocity {
pub x: f32,
pub y: f32,
}
fn main() {
let world = World::new();
// Create a query for Position, Velocity. Queries are the fastest way to
// iterate entities as they cache results.
let query = world.new_query::<(&mut Position, &Velocity)>();
// Create a few test entities for a Position, Velocity query
world
.entity_named("e1")
.set(Position { x: 10.0, y: 20.0 })
.set(Velocity { x: 1.0, y: 2.0 });
world
.entity_named("e2")
.set(Position { x: 10.0, y: 20.0 })
.set(Velocity { x: 3.0, y: 4.0 });
// This entity will not match as it does not have Position, Velocity
world.entity_named("e3").set(Position { x: 10.0, y: 20.0 });
// The next lines show the different ways in which a query can be iterated.
// `The each_entity()` function iterates each entity individually and accepts an
// entity argument plus arguments for each query component:
query.each_entity(|e, (pos, vel)| {
pos.x += vel.x;
pos.y += vel.y;
println!("{}: [{:?}]", e.name(), pos);
});
// There's an equivalent function that does not include the entity argument
query.each(|(pos, vel)| {
pos.x += vel.x;
pos.y += vel.y;
println!("[{:?}]", pos);
});
// Run is a bit more verbose, but allows for more control over how entities
// are iterated as it provides multiple entities in the same callback.
query.run(|mut it| {
while it.next() {
let mut p = it.field_mut::<Position>(0).unwrap();
let v = it.field::<Velocity>(1).unwrap();
for i in it.iter() {
p[i].x += v[i].x;
p[i].y += v[i].y;
println!("[{:?}]", p[i]);
}
}
});
// Output:
// e1: [Position { x: 11.0, y: 22.0 }]
// e2: [Position { x: 13.0, y: 24.0 }]
// [Position { x: 12.0, y: 24.0 }]
// [Position { x: 16.0, y: 28.0 }]
// [Position { x: 13.0, y: 26.0 }]
// [Position { x: 19.0, y: 32.0 }]
}
#[cfg(feature = "flecs_nightly_tests")]
#[test]
fn test() {
let output_capture = OutputCapture::capture().unwrap();
main();
output_capture.test("query_basics".to_string());
}