diff --git a/mario/main.cpp b/mario/main.cpp index 67d41f6..5494bdc 100644 --- a/mario/main.cpp +++ b/mario/main.cpp @@ -24,6 +24,7 @@ std::shared_ptr< SDLPP::RectangleRender > mario = nullptr; std::shared_ptr< SDLPP::Texture > mario_texture = nullptr; std::shared_ptr< SDLPP::RectangleRender > leftStop = nullptr; std::shared_ptr< SDLPP::Renderer > renderer = nullptr; +bool mario_facing_right = true; void setMarioStanding() { if ( mario->getMovement().getX() == 0 ) { @@ -39,10 +40,18 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) { case SDLK_a: mario->resumeAnimation(); mario->addMovement( -SIDE_MOVEMENT, 0 ); + if ( mario->getMovement().getX() < 0 && mario_facing_right ) { + mario->flipHorizontally(); + mario_facing_right = false; + } break; case SDLK_d: mario->resumeAnimation(); mario->addMovement( SIDE_MOVEMENT, 0 ); + if ( mario->getMovement().getX() > 0 && !mario_facing_right ) { + mario->flipHorizontally(); + mario_facing_right = true; + } break; case SDLK_SPACE: case SDLK_w: @@ -62,10 +71,18 @@ void handleKeyUp( SDL_Keycode key ) { case SDLK_a: mario->resumeAnimation(); mario->addMovement( SIDE_MOVEMENT, 0 ); + if ( mario->getMovement().getX() > 0 && !mario_facing_right ) { + mario->flipHorizontally(); + mario_facing_right = true; + } break; case SDLK_d: mario->resumeAnimation(); mario->addMovement( -SIDE_MOVEMENT, 0 ); + if ( mario->getMovement().getX() < 0 && mario_facing_right ) { + mario->flipHorizontally(); + mario_facing_right = false; + } break; case SDLK_w: case SDLK_s: @@ -140,7 +157,9 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) { } else { mario->resetMovementY(); } - if ( mv.isStopped() || ( !mv.canGoLeft() && prevPos.getX() > mario->getPos().getX() ) || ( !mv.canGoRight() && prevPos.getX() < mario->getPos().getX() ) ) { + if ( mv.isStopped() || + ( !mv.canGoLeft() && prevPos.getX() > mario->getPos().getX() ) || + ( !mv.canGoRight() && prevPos.getX() < mario->getPos().getX() ) ) { mario->setPos( prevPos.getX(), mario->getPos().getY() ); } auto playerX = mario->getRect().x;