From 09cb13195c5b682bcb16f129b595fd2be83c026a Mon Sep 17 00:00:00 2001 From: zv0n Date: Sat, 24 Sep 2022 22:54:03 +0200 Subject: [PATCH] Mario: stop movement when entering main menu, check there was a movement before reversing it --- mario/main.cpp | 18 ++++++++-- mario/scenes/game_main_menu.cpp | 60 +++++++++++++++++++-------------- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/mario/main.cpp b/mario/main.cpp index 20fce13..02f02a5 100644 --- a/mario/main.cpp +++ b/mario/main.cpp @@ -35,6 +35,8 @@ std::shared_ptr coins = nullptr; int coin_count = 0; int global_frames = 0; std::string last_load_level = ""; +bool __right_pressed = false; +bool __left_pressed = false; std::vector> moving_objects = {}; std::vector game_scenes{}; @@ -46,10 +48,12 @@ void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) { switch (key) { case SDLK_a: case SDLK_LEFT: + __left_pressed = true; mario->walkLeft(); break; case SDLK_d: case SDLK_RIGHT: + __right_pressed = true; mario->walkRight(); break; case SDLK_SPACE: @@ -77,16 +81,26 @@ void handleKeyUp(SDL_Keycode key) { switch (key) { case SDLK_ESCAPE: { std::lock_guard lock(render_mutex); + mario->setMovement(0, mario->getMovement().getY()); + game_scenes.back().scene->pauseScene(); + __right_pressed = false; + __left_pressed = false; game_scenes.push_back( createGameMainMenuScene(renderer, false, true, true)); } break; case SDLK_a: case SDLK_LEFT: - mario->walkRight(); + if (__left_pressed) { + mario->walkRight(); + __left_pressed = false; + } break; case SDLK_d: case SDLK_RIGHT: - mario->walkLeft(); + if (__right_pressed) { + mario->walkLeft(); + __right_pressed = false; + } break; case SDLK_SPACE: case SDLK_w: diff --git a/mario/scenes/game_main_menu.cpp b/mario/scenes/game_main_menu.cpp index da9619f..8c8d213 100644 --- a/mario/scenes/game_main_menu.cpp +++ b/mario/scenes/game_main_menu.cpp @@ -55,8 +55,9 @@ void resetGlobals() { __buttons_main_menu.clear(); } -void showLoadMenu(void */*UNUSED*/, Button */*UNUSED*/) { - auto loadMenu = createLoadScene(__buttons_main_menu.back()->getRenderer(), "levels", loadLevel, false, false); +void showLoadMenu(void * /*UNUSED*/, Button * /*UNUSED*/) { + auto loadMenu = createLoadScene(__buttons_main_menu.back()->getRenderer(), + "levels", loadLevel, false, false); std::lock_guard lock(render_mutex); game_scenes.pop_back(); resetGlobals(); @@ -67,9 +68,9 @@ void __updateSelectedButton_MainMenu(uint64_t new_index) { if (new_index != (uint64_t)-1) { __buttons_main_menu[new_index]->setHighlight(); - if (__cur_button_index_main_menu != (uint64_t)-1 && new_index != __cur_button_index_main_menu) { - __buttons_main_menu[__cur_button_index_main_menu] - ->unsetHighlight(); + if (__cur_button_index_main_menu != (uint64_t)-1 && + new_index != __cur_button_index_main_menu) { + __buttons_main_menu[__cur_button_index_main_menu]->unsetHighlight(); } __cur_button_index_main_menu = new_index; } @@ -78,7 +79,7 @@ void __updateSelectedButton_MainMenu(uint64_t new_index) { void handleKeyUpMainMenu(SDL_Keycode key, SDLPP::Scene & /*UNUSED*/) { switch (key) { case SDLK_ESCAPE: - if(__include_resume) { + if (__include_resume) { resumeMainMenu(); } else { quitMainMenu(); @@ -86,7 +87,7 @@ void handleKeyUpMainMenu(SDL_Keycode key, SDLPP::Scene & /*UNUSED*/) { break; case SDLK_DOWN: case SDLK_s: - if(__cur_button_index_main_menu == __buttons_main_menu.size() - 1) { + if (__cur_button_index_main_menu == __buttons_main_menu.size() - 1) { __updateSelectedButton_MainMenu(0); } else { __updateSelectedButton_MainMenu(__cur_button_index_main_menu + 1); @@ -94,14 +95,15 @@ void handleKeyUpMainMenu(SDL_Keycode key, SDLPP::Scene & /*UNUSED*/) { break; case SDLK_UP: case SDLK_w: - if(__cur_button_index_main_menu == 0) { + if (__cur_button_index_main_menu == 0) { __updateSelectedButton_MainMenu(__buttons_main_menu.size() - 1); } else { __updateSelectedButton_MainMenu(__cur_button_index_main_menu - 1); } break; case SDLK_RETURN: - if(__cur_button_index_main_menu >= 0 && __cur_button_index_main_menu < __buttons_main_menu.size()) { + if (__cur_button_index_main_menu >= 0 && + __cur_button_index_main_menu < __buttons_main_menu.size()) { __buttons_main_menu[__cur_button_index_main_menu] ->performFunction(); } @@ -112,10 +114,13 @@ void handleKeyUpMainMenu(SDL_Keycode key, SDLPP::Scene & /*UNUSED*/) { } std::shared_ptr -createSceneMainMenu(std::shared_ptr &renderer, bool include_restart, bool include_resume, bool transparent_bg) { +createSceneMainMenu(std::shared_ptr &renderer, + bool include_restart, bool include_resume, + bool transparent_bg) { auto scene = std::make_shared(renderer); - auto bg = std::make_shared(0, 0, 10, 10, renderer, - transparent_bg ? "#00000088" : MARIO_OVERWORLD_COLORKEY, true); + auto bg = std::make_shared( + 0, 0, 10, 10, renderer, + transparent_bg ? "#00000088" : MARIO_OVERWORLD_COLORKEY, true); bg->setPermanent(); bg->setId(1); scene->addObject(bg); @@ -141,27 +146,29 @@ createSceneMainMenu(std::shared_ptr &renderer, bool include_res default_button_theme.font_outline_color_disabled = "#787878"; default_button_theme.outline = 0.1; // buttons - if(include_resume) { + if (include_resume) { __buttons_main_menu.emplace_back(std::make_shared