@herbert
Again using epub so don’t know the exact page number.
In this snipped of code, what does the movers.iter_mut(ecs)
line do in the following code?
I think that movers is a list of tuples that we stored in the ECS and grabbed from the query.
I think that the iter_mut
gives us a mutable iterator on the list of tuples.
What I don’t understand is why we pass in the ecs object to the iter_mut
. I would think that it would be movers.iter_mut()
use crate::prelude::*;
#[system]
#[write_component(Point)]
#[read_component(MovingRandomly)]
① pub fn random_move(ecs: &mut SubWorld, #[resource] map: &Map) {
② let mut movers = <(&mut Point, &MovingRandomly)>::query();
movers
.iter_mut(ecs)
.for_each(|(pos, _)| {
let mut rng = RandomNumberGenerator::new();
③ let destination = match rng.range(0, 4) {
0 => Point::new(-1, 0),
1 => Point::new(1, 0),
2 => Point::new(0, -1),
_ => Point::new(0, 1),
} + *pos;
④ if map.can_enter_tile(destination) {
⑤ *pos = destination;
}
}
);
}
①