From 962e1bfc128b8c4098be001e108218a408423c50 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 30 Dec 2020 10:15:42 -0500 Subject: [PATCH] Making the tail follow the snake --- src/main.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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; + }) } }