diff --git a/source/game.cpp b/source/game.cpp index 272d673..7a0ef42 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -138,4 +138,7 @@ void Game::renderDebugInfo() text = "getTile " + std::to_string(player->map->getTile(player->collider[0])); debugText->write(0, line += 6, text, -1); + + text = "state " + std::to_string(player->state); + debugText->write(0, line += 6, text, -1); } \ No newline at end of file diff --git a/source/player.cpp b/source/player.cpp index 9e4556a..aebab6d 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -28,16 +28,14 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map) sprite->setCurrentAnimation("stand"); sprite->setFlip(SDL_FLIP_HORIZONTAL); - // jumpStrenght = -5.6f; jumpStrenght = 2.0f; gravity = 0.3f; accelX = 0.12f; maxVX = 1.5f; maxVY = 4.0f; - jumping = false; + state = standing; jumpPressed = false; - standing = true; invulnerable = false; enabled = true; cooldown = 0; @@ -104,26 +102,34 @@ void Player::checkInput() if (input->checkInput(INPUT_UP, REPEAT_TRUE)) { - if (!jumping) + if (state == standing) { if (!jumpPressed) { jumpStrenght = 2.0f; vy -= jumpStrenght; - jumping = true; + state = jumping; jumpPressed = true; } } - else + else if (state == jumping) { - jumpStrenght = std::max(jumpStrenght -= 0.4f, 0.0f); - vy -= jumpStrenght; + if (jumpPressed) + { + jumpStrenght = std::max(jumpStrenght -= 0.4f, 0.0f); + vy -= jumpStrenght; + } } + jumpPressed = true; } else { jumpPressed = false; + if (state == jumping) + { + state = falling; + } } } @@ -133,7 +139,10 @@ void Player::addGravity() if (!isOnFloor()) { vy = std::min(vy += gravity, maxVY); - jumping = true; + if (state == standing) + { + state = falling; + } } } @@ -215,18 +224,18 @@ void Player::move() if (vy > 0.0f) { y -= ((int)y + h) % tile; + state = standing; } else { y += tile - ((int)y % tile); + state = falling; } - - jumping = false; vy = 0.0f; } else if ((!wasOnFloor) && (isOnFloor()) && (vy > 0.0f)) { - jumping = false; + state = standing; vy = 0.0f; y -= ((int)y + h) % tile; } @@ -238,7 +247,7 @@ void Player::move() // Anima al jugador void Player::animate() { - if (jumping) + if (state != standing) { sprite->setCurrentAnimation("jump"); } diff --git a/source/player.h b/source/player.h index b766e95..9198506 100644 --- a/source/player.h +++ b/source/player.h @@ -10,6 +10,13 @@ #ifndef PLAYER_H #define PLAYER_H +enum t_player_state +{ + standing, + jumping, + falling +}; + // The player class Player { @@ -21,20 +28,19 @@ public: LTexture *texture; // Textura con los graficos del jugador Map *map; // Objeto con el mapa - float x; // Posición del jugador en el eje X - float y; // Posición del jugador en el eje Y - float vx; // Velocidad/desplazamiento del jugador en el eje X - float vy; // Velocidad/desplazamiento del jugador en el eje Y - bool jumping; // Indica si se encuentra saltando - bool jumpPressed; // Indica si esta pulsada la tecla de salto - bool enabled; // Si está habilitado - bool standing; // Si esta de pie (o quieto?) - bool invulnerable; // Indica si se encuentra en estado invulnerable - int coins; // Cantidad de monedas - int cooldown; // Tiempo de inhabilitación - int lives; // Cantidad de vidas - int w; // Ancho del jugador - int h; // ALto del jugador + float x; // Posición del jugador en el eje X + float y; // Posición del jugador en el eje Y + float vx; // Velocidad/desplazamiento del jugador en el eje X + float vy; // Velocidad/desplazamiento del jugador en el eje Y + bool jumpPressed; // Indica si esta pulsada la tecla de salto + bool enabled; // Si está habilitado + bool invulnerable; // Indica si se encuentra en estado invulnerable + int coins; // Cantidad de monedas + int cooldown; // Tiempo de inhabilitación + int lives; // Cantidad de vidas + int w; // Ancho del jugador + int h; // ALto del jugador + t_player_state state; // Estado actual del jugador // Variables que afectan a la inercia del movimiento float jumpStrenght; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar