|
@@ -31,6 +31,7 @@ pub fn spawn(
|
|
|
})
|
|
|
.insert(c::SnakeHead {
|
|
|
direction: c::Direction::Up,
|
|
|
+ intention: c::Direction::Up,
|
|
|
})
|
|
|
.insert(c::SnakeSegment)
|
|
|
.insert(c::Position { x: 3, y: 3 })
|
|
@@ -55,10 +56,10 @@ pub fn input(keyboard_input: Res<Input<KeyCode>>, mut heads: Query<&mut c::Snake
|
|
|
} else if keyboard_input.pressed(KeyCode::Right) {
|
|
|
c::Direction::Right
|
|
|
} else {
|
|
|
- head.direction
|
|
|
+ head.intention
|
|
|
};
|
|
|
if dir != head.direction.opposite() {
|
|
|
- head.direction = dir;
|
|
|
+ head.intention = dir;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -67,16 +68,17 @@ pub fn movement(
|
|
|
segments: ResMut<c::SnakeSegments>,
|
|
|
mut last_tail_position: ResMut<c::LastTailPosition>,
|
|
|
mut game_over_writer: EventWriter<c::GameOverEvent>,
|
|
|
- mut heads: Query<(Entity, &c::SnakeHead)>,
|
|
|
+ mut heads: Query<(Entity, &mut c::SnakeHead)>,
|
|
|
mut positions: Query<&mut c::Position>,
|
|
|
) {
|
|
|
- if let Some((head_entity, head)) = heads.iter_mut().next() {
|
|
|
+ if let Some((head_entity, mut head)) = heads.iter_mut().next() {
|
|
|
let segment_positions = segments
|
|
|
.segments
|
|
|
.iter()
|
|
|
.map(|e| *positions.get_mut(*e).unwrap())
|
|
|
.collect::<Vec<c::Position>>();
|
|
|
let mut head_pos = positions.get_mut(head_entity).unwrap();
|
|
|
+ head.direction = head.intention;
|
|
|
match &head.direction {
|
|
|
c::Direction::Left => head_pos.x -= 1,
|
|
|
c::Direction::Right => head_pos.x += 1,
|