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
86
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,
}
#[derive(Component)]
pub struct Eats;
#[derive(Component)]
pub struct Apples;
fn main() {
// Create a new world
let world = World::new();
// Register system
let _sys = world
.system::<(&mut Position, &Velocity)>()
// .each_entity if you want the entity to be added in the parameter list
.each(|(pos, vel)| {
pos.x += vel.x;
pos.y += vel.y;
});
// Create an entity with name Bob, add Position and food preference
let bob = world
.entity_named("Bob")
.set(Position { x: 0.0, y: 0.0 })
.set(Velocity { x: 1.0, y: 2.0 })
.add((Eats, Apples));
// Show us what you got
// println!( "{}'s got [{:?}]", bob.name(), bob.archetype());
println!("{}'s got [{:?}]", bob.name(), bob.archetype());
// Run systems twice. Usually this function is called once per frame
world.progress();
world.progress();
// - get panics if the component is not present, use try_get for a non-panicking version which does not run the callback.
// - or use Option to handle the individual component missing.
bob.get::<&Position>(|pos| {
// See if Bob has moved (he has)
println!("{}'s position: {:?}", bob.name(), pos);
});
// Option example
let has_run = bob
.try_get::<Option<&Position>>(|pos| {
if let Some(pos) = pos {
// See if Bob has moved (he has)
//println!( "{}'s try_get position: {:?}", bob.name(), pos);
println!("{}'s try_get position: {:?}", bob.name(), pos);
}
})
.is_some();
if has_run {
println!("Bob has a position component, so the try_get callback ran.");
}
// Output:
// Bob's got [Position, Velocity, (Identifier,Name), (Eats,Apples)]
// Bob's position: Position { x: 2.0, y: 4.0 }
// Bob's try_get position: Position { x: 2.0, y: 4.0 }
// Bob has a position component, so the try_get callback ran.
}
#[cfg(feature = "flecs_nightly_tests")]
#[test]
fn test() {
let output_capture = OutputCapture::capture().unwrap();
main();
output_capture.test("hello world".to_string());
}