examples/systems/
system_pipeline.rs

1use 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(Debug, Component)]
12pub struct Velocity {
13    pub x: f32,
14    pub y: f32,
15}
16
17fn main() {
18    let world = World::new();
19
20    // Create a system for moving an entity
21    world
22        .system::<(&mut Position, &Velocity)>()
23        .kind::<flecs::pipeline::OnUpdate>()
24        .each(|(p, v)| {
25            p.x += v.x;
26            p.y += v.y;
27        });
28
29    // Create a system for printing the entity position
30    world
31        .system::<&Position>()
32        .kind::<flecs::pipeline::PostUpdate>()
33        .each_entity(|e, p| {
34            println!("{}: {{ {}, {} }}", e.name(), p.x, p.y);
35        });
36
37    // Create a few test entities for a Position, Velocity query
38    world
39        .entity_named("e1")
40        .set(Position { x: 10.0, y: 20.0 })
41        .set(Velocity { x: 1.0, y: 2.0 });
42
43    world
44        .entity_named("e2")
45        .set(Position { x: 10.0, y: 20.0 })
46        .set(Velocity { x: 3.0, y: 4.0 });
47
48    // Run the default pipeline. This will run all systems ordered by their
49    // phase. Systems within the same phase are ran in declaration order. This
50    // function is usually called in a loop.
51    world.progress();
52
53    // Output:
54    //  e1: { 11, 22 }
55    //  e2: { 13, 24 }
56}
57
58#[cfg(feature = "flecs_nightly_tests")]
59#[test]
60fn test() {
61    let output_capture = OutputCapture::capture().unwrap();
62    main();
63    output_capture.test("system_pipeline".to_string());
64}