diff --git a/source/game.cpp b/source/game.cpp index 28cf408..b9002c9 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -201,10 +201,10 @@ void Game::checkPlayerOnFloor() // Comprueba ambos pies if ((mRoom->isFloor(mPlayer->getLeftFoot())) || (mRoom->isFloor(mPlayer->getRightFoot()))) { - mPlayer->setFalling(false); + mPlayer->setStatus(STATUS_STANDING); } else { - mPlayer->setFalling(true); + mPlayer->setStatus(STATUS_FALLING); } } \ No newline at end of file diff --git a/source/player.cpp b/source/player.cpp index 71a9fa2..45e6d68 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -2,6 +2,8 @@ #include #include +// CAUTION!!!!! si no se gasta al final, quitar la referencia a la habitación + // Constructor Player::Player(std::string _tileset, SDL_Renderer *_renderer, Asset *_asset, Input *_input, Room *_room) { @@ -22,6 +24,7 @@ Player::Player(std::string _tileset, SDL_Renderer *_renderer, Asset *_asset, Inp color = stringToColor("white"); onBorder = false; border = BORDER_TOP; + status = STATUS_STANDING; sprite->setPosX(2 * 8); sprite->setPosX(10 * 8); @@ -67,8 +70,8 @@ void Player::update() { checkInput(); sprite->update(); - // sprite->animate(0); checkBorders(); + applyGravity(); } // Comprueba las entradas y modifica variables @@ -80,16 +83,16 @@ void Player::checkInput() // if (mInput->checkInput(INPUT_DOWN, REPEAT_FALSE)) // changeRoom(mRoom->getRoomDown()); - if (input->checkInput(INPUT_LEFT, REPEAT_TRUE)) + // Se mueve en horizontal solo cuando no esté cayendo + if ((input->checkInput(INPUT_LEFT, REPEAT_TRUE)) && (status != STATUS_FALLING)) { - sprite->setVelX(-VX); + sprite->setVelX(-0.6f); sprite->animate(0); sprite->setFlip(SDL_FLIP_HORIZONTAL); } - - else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE)) + else if ((input->checkInput(INPUT_RIGHT, REPEAT_TRUE)) && (status != STATUS_FALLING)) { - sprite->setVelX(VX); + sprite->setVelX(0.6f); sprite->animate(0); sprite->setFlip(SDL_FLIP_NONE); } @@ -97,6 +100,11 @@ void Player::checkInput() { sprite->setVelX(0); } + + if (input->checkInput(INPUT_UP, REPEAT_FALSE)) + { + setStatus(STATUS_JUMPING); + } } // Indica si el jugador esta en uno de los cuatro bordes de la pantalla @@ -183,15 +191,48 @@ SDL_Point Player::getRightFoot() return point; } -// Establece la velocidad en el eje Y al jugador -void Player::setFalling(bool value) +// Cambia el estado del jugador +void Player::setStatus(int value) { - if (value) + // Si quiere cambiar a saltando, ha de ser desde quieto + if ((value == STATUS_JUMPING) && (status == STATUS_STANDING)) { + status = STATUS_JUMPING; + sprite->setVelY(-2.0f); + } + + // Si se quiere cambiar a cayendo, ha de ser desde quieto + if ((value == STATUS_FALLING) && (status == STATUS_STANDING)) + { + status = STATUS_FALLING; sprite->setVelY(0.5f); } - else + + // Si se quiere cambiar a quieto, no hay resticciones + if (value == STATUS_STANDING) { + status = STATUS_STANDING; sprite->setVelY(0.0f); } +} + +// Aplica gravedad al jugador +void Player::applyGravity() +{ + if (status == STATUS_JUMPING) + { + sprite->setVelY(sprite->getVelY() + 0.1f); + if (sprite->getVelY() > 0.5f) + { + sprite->setVelY(0.5f); + // status = STATUS_FALLING; + } + } +} + +// Obtiene el rectangulo que delimita al jugador +SDL_Rect Player::getRect() +{ + SDL_Rect rect = {sprite->getPosX(), sprite->getPosY(), sprite->getWidth(), sprite->getHeight()}; + return rect; } \ No newline at end of file diff --git a/source/player.h b/source/player.h index ac26bac..2996e5b 100644 --- a/source/player.h +++ b/source/player.h @@ -11,7 +11,10 @@ #ifndef PLAYER_H #define PLAYER_H -#define VX 0.6 +//#define VX 0.6 +#define STATUS_STANDING 0 +#define STATUS_JUMPING 1 +#define STATUS_FALLING 2 // Clase Player class Player @@ -25,6 +28,8 @@ private: SDL_Renderer *renderer; // El renderizador de la ventana Asset *asset; // Objeto con la ruta a todos los ficheros de recursos color_t color; // Color del jugador + int status; // Estado en el que se encuentra el jugador. Util apara saber si está saltando o cayendo + SDL_Rect lastPosition; // Contiene la ultima posición del jugador, por si hay que deshacer algun movimiento bool onBorder; // Indica si el jugador esta en uno de los cuatro bordes de la pantalla int border; // Indica en cual de los cuatro bordes se encuentra @@ -35,6 +40,12 @@ private: // Comprueba si se halla en alguno de los cuatro bordes void checkBorders(); + // Asigna velocidad negativa en el eje Y al jugador + void jump(); + + // Aplica gravedad al jugador + void applyGravity(); + public: // Constructor Player(std::string _tileset, SDL_Renderer *_renderer, Asset *_asset, Input *_input, Room *_room); @@ -66,8 +77,11 @@ public: // Obtiene el valor del pixel inferior derecho del jugador SDL_Point getRightFoot(); - // Establece la velocidad en el eje Y al jugador - void setFalling(bool value); + // Cambia el estado del jugador + void setStatus(int value); + + // Obtiene el rectangulo que delimita al jugador + SDL_Rect getRect(); }; #endif