examples/systems/
system_basics.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 Position, Velocity. Systems are like queries (see
21    // queries) with a function that can be ran or scheduled (see pipeline).
22
23    let s = world
24        .system::<(&mut Position, &Velocity)>()
25        .each_entity(|e, (p, v)| {
26            p.x += v.x;
27            p.y += v.y;
28            println!("{}: {{ {}, {} }}", e.name(), p.x, p.y);
29        });
30
31    // Create a few test entities for a Position, Velocity query
32    world
33        .entity_named("e1")
34        .set(Position { x: 10.0, y: 20.0 })
35        .set(Velocity { x: 1.0, y: 2.0 });
36
37    world
38        .entity_named("e2")
39        .set(Position { x: 10.0, y: 20.0 })
40        .set(Velocity { x: 3.0, y: 4.0 });
41
42    // This entity will not match as it does not have Position, Velocity
43    world.entity_named("e3").set(Position { x: 10.0, y: 20.0 });
44
45    // Run the system
46    s.run();
47
48    // Output:
49    //  e1: { 11, 22 }
50    //  e2: { 13, 24 }
51}
52
53#[cfg(feature = "flecs_nightly_tests")]
54#[test]
55fn test() {
56    let output_capture = OutputCapture::capture().unwrap();
57    main();
58    output_capture.test("system_basics".to_string());
59}