Making the tail follow the snake

This commit is contained in:
Timothy Warren 2020-12-30 10:15:42 -05:00
parent 529fe3f2b1
commit 962e1bfc12

View File

@ -125,10 +125,16 @@ fn spawn_segment(
fn snake_movement( fn snake_movement(
keyboard_input: Res<Input<KeyCode>>, keyboard_input: Res<Input<KeyCode>>,
snake_timer: ResMut<SnakeMoveTimer>, snake_timer: ResMut<SnakeMoveTimer>,
segments: ResMut<SnakeSegments>,
mut heads: Query<(Entity, &mut SnakeHead)>, mut heads: Query<(Entity, &mut SnakeHead)>,
mut positions: Query<&mut Position>, mut positions: Query<&mut Position>,
) { ) {
if let Some((head_entity, mut head)) = heads.iter_mut().next() { 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::<Vec<Position>>();
let mut head_pos = positions.get_mut(head_entity).unwrap(); let mut head_pos = positions.get_mut(head_entity).unwrap();
let dir: Direction = if keyboard_input.pressed(KeyCode::Left) { let dir: Direction = if keyboard_input.pressed(KeyCode::Left) {
Direction::Left Direction::Left
@ -161,6 +167,12 @@ fn snake_movement(
head_pos.y -= 1; head_pos.y -= 1;
} }
}; };
segment_positions
.iter()
.zip(segments.0.iter().skip(1))
.for_each(|(pos, segment)| {
*positions.get_mut(*segment).unwrap() = *pos;
})
} }
} }