Browse Source

:sparkles: improved snake controls a bit

one can now build stairs by holding two direction keys
Felix Bytow 1 năm trước cách đây
mục cha
commit
2eea729906
1 tập tin đã thay đổi với 12 bổ sung11 xóa
  1. 12 11
      game/PlayingState.cxx

+ 12 - 11
game/PlayingState.cxx

@@ -139,23 +139,24 @@ void PlayingState::handle_direction_change()
 {
   // this is not done in the event handler as we don't want to wait for KEYUP to re-fire in certain situations
   auto const keyboard = SDL_GetKeyboardState(nullptr);
-  if (keyboard[SDL_SCANCODE_UP] || keyboard[SDL_SCANCODE_W]) {
-    if (direction_==Direction::Left || direction_==Direction::Right) {
+
+  if (new_direction_.has_value())
+    return;
+
+  if (direction_==Direction::Left || direction_==Direction::Right) {
+    if (keyboard[SDL_SCANCODE_UP] || keyboard[SDL_SCANCODE_W]) {
       new_direction_ = Direction::Up;
     }
-  }
-  else if (keyboard[SDL_SCANCODE_DOWN] || keyboard[SDL_SCANCODE_S]) {
-    if (direction_==Direction::Left || direction_==Direction::Right) {
+
+    else if (keyboard[SDL_SCANCODE_DOWN] || keyboard[SDL_SCANCODE_S]) {
       new_direction_ = Direction::Down;
     }
-  }
-  else if (keyboard[SDL_SCANCODE_LEFT] || keyboard[SDL_SCANCODE_A]) {
-    if (direction_==Direction::Up || direction_==Direction::Down) {
+  } else {
+    if (keyboard[SDL_SCANCODE_LEFT] || keyboard[SDL_SCANCODE_A]) {
       new_direction_ = Direction::Left;
     }
-  }
-  else if (keyboard[SDL_SCANCODE_RIGHT] || keyboard[SDL_SCANCODE_D]) {
-    if (direction_==Direction::Up || direction_==Direction::Down) {
+
+    else if (keyboard[SDL_SCANCODE_RIGHT] || keyboard[SDL_SCANCODE_D]) {
       new_direction_ = Direction::Right;
     }
   }