diff --git a/source/game.cpp b/source/game.cpp index 91b61d4..e6244cf 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -11,7 +11,7 @@ Game::Game(SDL_Renderer *renderer, Asset *asset, Screen *screen, Input *input) eventHandler = new SDL_Event(); map = new Map(asset->get("01.map"), renderer, asset); - player = new Player(renderer, asset); + player = new Player(renderer, asset, input); } // Destructor @@ -72,6 +72,8 @@ void Game::update() break; } } + + player->update(); } } diff --git a/source/player.cpp b/source/player.cpp index 9b1baee..8abdc62 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -6,10 +6,11 @@ #define BASE_SPEED 0.5; // Constructor -Player::Player(SDL_Renderer *renderer, Asset *asset) +Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input) { this->asset = asset; this->renderer = renderer; + this->input = input; sound_jump = JA_LoadSound(asset->get("sound_player_jump.wav").c_str()); sound_death = JA_LoadSound(asset->get("sound_player_death.wav").c_str()); @@ -60,10 +61,32 @@ Player::~Player() // Actualiza todas las variables void Player::update() { + checkInput(); + sprite->update(); } // Dibuja el objeto void Player::render() { sprite->render(); +} + +// Comprueba las entradas y modifica variables +void Player::checkInput() +{ + // Solo comprueba las entradas de dirección cuando está de pie + if (input->checkInput(INPUT_LEFT, REPEAT_TRUE)) + { + sprite->setVelX(-0.6f); + sprite->setFlip(SDL_FLIP_HORIZONTAL); + } + else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE)) + { + sprite->setVelX(0.6f); + sprite->setFlip(SDL_FLIP_NONE); + } + else + { + sprite->setVelX(0); + } } \ No newline at end of file diff --git a/source/player.h b/source/player.h index ebe8fd4..9a3ccaf 100644 --- a/source/player.h +++ b/source/player.h @@ -2,6 +2,7 @@ #include #include "jail_audio.h" #include "utils.h" +#include "input.h" #include "animatedsprite.h" #include "asset.h" @@ -14,6 +15,7 @@ class Player private: Asset *asset; // Objeto con la ruta a todos los ficheros de recursos SDL_Renderer *renderer; // El renderizador de la ventana + Input *input; // Objeto Input para gestionar las entradas AnimatedSprite *sprite; // Objeto con los graficos, animaciones y posición del jugador LTexture *texture; // Textura con los graficos del jugador @@ -40,9 +42,12 @@ private: Uint8 lifes; // Cantidad de vidas Uint8 respawn_direction; // Dirección para revivir + // Comprueba las entradas y modifica variables + void checkInput(); + public: // Constructor - Player(SDL_Renderer *renderer, Asset *asset); + Player(SDL_Renderer *renderer, Asset *asset, Input *input); // Destructor ~Player(); diff --git a/source/prog.cpp b/source/prog.cpp index fe25417..6c28c39 100644 --- a/source/prog.cpp +++ b/source/prog.cpp @@ -30,6 +30,28 @@ Prog::Prog(std::string executablePath) } input = new Input(asset->get("gamecontrollerdb.txt")); screen = new Screen(window, renderer, options); + + // Controles + input->bindKey(INPUT_UP, SDL_SCANCODE_UP); + input->bindKey(INPUT_DOWN, SDL_SCANCODE_DOWN); + input->bindKey(INPUT_LEFT, SDL_SCANCODE_LEFT); + input->bindKey(INPUT_RIGHT, SDL_SCANCODE_RIGHT); + input->bindKey(INPUT_ACCEPT, SDL_SCANCODE_RETURN); + input->bindKey(INPUT_CANCEL, SDL_SCANCODE_ESCAPE); + input->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_SPACE); + input->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_D); + input->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE); + input->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_ESCAPE); + + input->bindGameControllerButton(INPUT_UP, SDL_CONTROLLER_BUTTON_DPAD_UP); + input->bindGameControllerButton(INPUT_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN); + input->bindGameControllerButton(INPUT_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT); + input->bindGameControllerButton(INPUT_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT); + input->bindGameControllerButton(INPUT_ACCEPT, SDL_CONTROLLER_BUTTON_B); + input->bindGameControllerButton(INPUT_CANCEL, SDL_CONTROLLER_BUTTON_A); + input->bindGameControllerButton(INPUT_BUTTON_1, SDL_CONTROLLER_BUTTON_B); + input->bindGameControllerButton(INPUT_BUTTON_PAUSE, SDL_CONTROLLER_BUTTON_GUIDE); + input->bindGameControllerButton(INPUT_BUTTON_ESCAPE, SDL_CONTROLLER_BUTTON_GUIDE); } Prog::~Prog()