Mario: flip texture according to movement direction

This commit is contained in:
zvon 2021-04-29 13:08:08 +02:00
parent ff741dd882
commit b52dee267b

View File

@ -24,6 +24,7 @@ std::shared_ptr< SDLPP::RectangleRender > mario = nullptr;
std::shared_ptr< SDLPP::Texture > mario_texture = nullptr; std::shared_ptr< SDLPP::Texture > mario_texture = nullptr;
std::shared_ptr< SDLPP::RectangleRender > leftStop = nullptr; std::shared_ptr< SDLPP::RectangleRender > leftStop = nullptr;
std::shared_ptr< SDLPP::Renderer > renderer = nullptr; std::shared_ptr< SDLPP::Renderer > renderer = nullptr;
bool mario_facing_right = true;
void setMarioStanding() { void setMarioStanding() {
if ( mario->getMovement().getX() == 0 ) { if ( mario->getMovement().getX() == 0 ) {
@ -39,10 +40,18 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
case SDLK_a: case SDLK_a:
mario->resumeAnimation(); mario->resumeAnimation();
mario->addMovement( -SIDE_MOVEMENT, 0 ); mario->addMovement( -SIDE_MOVEMENT, 0 );
if ( mario->getMovement().getX() < 0 && mario_facing_right ) {
mario->flipHorizontally();
mario_facing_right = false;
}
break; break;
case SDLK_d: case SDLK_d:
mario->resumeAnimation(); mario->resumeAnimation();
mario->addMovement( SIDE_MOVEMENT, 0 ); mario->addMovement( SIDE_MOVEMENT, 0 );
if ( mario->getMovement().getX() > 0 && !mario_facing_right ) {
mario->flipHorizontally();
mario_facing_right = true;
}
break; break;
case SDLK_SPACE: case SDLK_SPACE:
case SDLK_w: case SDLK_w:
@ -62,10 +71,18 @@ void handleKeyUp( SDL_Keycode key ) {
case SDLK_a: case SDLK_a:
mario->resumeAnimation(); mario->resumeAnimation();
mario->addMovement( SIDE_MOVEMENT, 0 ); mario->addMovement( SIDE_MOVEMENT, 0 );
if ( mario->getMovement().getX() > 0 && !mario_facing_right ) {
mario->flipHorizontally();
mario_facing_right = true;
}
break; break;
case SDLK_d: case SDLK_d:
mario->resumeAnimation(); mario->resumeAnimation();
mario->addMovement( -SIDE_MOVEMENT, 0 ); mario->addMovement( -SIDE_MOVEMENT, 0 );
if ( mario->getMovement().getX() < 0 && mario_facing_right ) {
mario->flipHorizontally();
mario_facing_right = false;
}
break; break;
case SDLK_w: case SDLK_w:
case SDLK_s: case SDLK_s:
@ -140,7 +157,9 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
} else { } else {
mario->resetMovementY(); 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() ); mario->setPos( prevPos.getX(), mario->getPos().getY() );
} }
auto playerX = mario->getRect().x; auto playerX = mario->getRect().x;