Browse Source

fix the quick-turning bug

Getty Ritter 2 years ago
parent
commit
fdae473d9f
2 changed files with 9 additions and 5 deletions
  1. 3 1
      src/components.rs
  2. 6 4
      src/snake.rs

+ 3 - 1
src/components.rs

@@ -25,7 +25,7 @@ impl GridSize {
     }
 }
 
-#[derive(PartialEq, Copy, Clone)]
+#[derive(PartialEq, Copy, Clone, Debug)]
 pub enum Direction {
     Left,
     Up,
@@ -55,7 +55,9 @@ pub struct Materials {
 pub struct Food;
 
 // snake-related components
+#[derive(Debug)]
 pub struct SnakeHead {
+    pub intention: Direction,
     pub direction: Direction,
 }
 

+ 6 - 4
src/snake.rs

@@ -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,