diff --git a/src/main.rs b/src/main.rs
index 56e06fb..93dbf09 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -125,10 +125,16 @@ fn spawn_segment(
fn snake_movement(
keyboard_input: Res>,
snake_timer: ResMut,
+ segments: ResMut,
mut heads: Query<(Entity, &mut SnakeHead)>,
mut positions: Query<&mut Position>,
) {
if let Some((head_entity, mut head)) = heads.iter_mut().next() {
+ let segment_positions = segments
+ .0
+ .iter()
+ .map(|e| *positions.get_mut(*e).unwrap())
+ .collect::>();
let mut head_pos = positions.get_mut(head_entity).unwrap();
let dir: Direction = if keyboard_input.pressed(KeyCode::Left) {
Direction::Left
@@ -161,6 +167,12 @@ fn snake_movement(
head_pos.y -= 1;
}
};
+ segment_positions
+ .iter()
+ .zip(segments.0.iter().skip(1))
+ .for_each(|(pos, segment)| {
+ *positions.get_mut(*segment).unwrap() = *pos;
+ })
}
}