schedule/
schedule.rs

1#![allow(missing_docs)]
2use xuko_ecs::{Query, QueryFilter, World, component_id, schedule::*};
3
4#[derive(Clone, Debug)]
5struct Position(i32, i32);
6#[derive(Clone, Debug)]
7struct Velocity(i32, i32);
8
9struct Bullet;
10
11fn main() {
12    let mut s = Scheduler::new(World::new());
13
14    s.add_system(schedules::INIT, add_bullets as FnMutSystem);
15    s.add_system(schedules::UPDATE, move_entities as FnMutSystem);
16
17    s.run_schedule(schedules::INIT);
18
19    loop {
20        s.run_schedule(schedules::UPDATE);
21    }
22}
23
24fn add_bullets(world: &mut World) {
25    let bullets = [
26        (Position(4, 7), Velocity(3, 0)),
27        (Position(0, 0), Velocity(0, 0)),
28        (Position(5, -8), Velocity(0, -5)),
29        (Position(12, 0), Velocity(-1, 0)),
30    ];
31
32    for (position, velocity) in bullets {
33        let entity = world.spawn();
34        let entity = world.get_entity_mut(entity).unwrap();
35
36        entity.add(Bullet);
37        entity.add(position);
38        entity.add(velocity);
39    }
40}
41
42fn move_entities(world: &mut World) {
43    let entities = Query::query_world(
44        world,
45        QueryFilter {
46            requires: vec![component_id!(Position), component_id!(Velocity)],
47            ..Default::default()
48        },
49    );
50
51    for e in &entities {
52        let entity = world.get_entity_mut(e).unwrap();
53
54        let velocity = entity.get::<Velocity>().cloned().unwrap();
55        let position = entity.get_mut::<Position>().unwrap();
56
57        position.0 += velocity.0;
58        position.1 += velocity.1;
59
60        println!("Something that has a position: {position:?}");
61    }
62}