Making the tail follow the snake
This commit is contained in:
parent
529fe3f2b1
commit
962e1bfc12
12
src/main.rs
12
src/main.rs
@ -125,10 +125,16 @@ fn spawn_segment(
|
||||
fn snake_movement(
|
||||
keyboard_input: Res<Input<KeyCode>>,
|
||||
snake_timer: ResMut<SnakeMoveTimer>,
|
||||
segments: ResMut<SnakeSegments>,
|
||||
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::<Vec<Position>>();
|
||||
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;
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user