From a1eaf8af0752cbc5d7752081a97f895585fd60ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Sat, 13 Aug 2022 12:14:31 +0200 Subject: [PATCH] Implementadas las animaciones en el jugador --- data/animations/player.ani | 2 +- source/animatedsprite.cpp | 43 ++++++++++++++++++++++++++++---------- source/animatedsprite.h | 13 +++++++++--- source/game.cpp | 3 --- source/map.cpp | 6 +++--- source/player.cpp | 41 +++++++++--------------------------- source/player.h | 34 +++++++++++------------------- 7 files changed, 68 insertions(+), 74 deletions(-) diff --git a/data/animations/player.ani b/data/animations/player.ani index 5f01fc6..6bd3498 100644 --- a/data/animations/player.ani +++ b/data/animations/player.ani @@ -6,7 +6,7 @@ tile_height=24 name=stand speed=8 loop=yes -frames=0,1,2,2,1,00,1,2,2,1,00,1,2,2,1,0,0,1,2,2,1,0,0,1,2,3,4,5,6,7,0,0 +frames=0,1,2,2,1,0,0,1,2,2,1,0,0,1,2,2,1,0,0,1,2,2,1,0,0,1,2,3,4,5,6,7,0,0,0,0 [/animation] [animation] diff --git a/source/animatedsprite.cpp b/source/animatedsprite.cpp index 8e2090c..dcef260 100644 --- a/source/animatedsprite.cpp +++ b/source/animatedsprite.cpp @@ -2,11 +2,14 @@ #include "animatedsprite.h" // Constructor -AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer) +AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer, std::string file) { // Copia los punteros setTexture(texture); setRenderer(renderer); + + // Carga las animaciones + load(file); } // Destructor @@ -34,32 +37,30 @@ int AnimatedSprite::getIndex(std::string name) } // Calcula el frame correspondiente a la animación -void AnimatedSprite::animate(std::string name) +void AnimatedSprite::animate() { if (mEnabled) { - const int index = getIndex(name); - // Calcula el frame actual a partir del contador - animation[index].currentFrame = animation[index].counter / animation[index].speed; + animation[currentAnimation].currentFrame = animation[currentAnimation].counter / animation[currentAnimation].speed; // Si alcanza el final de la animación, reinicia el contador de la animación // en función de la variable loop - if (animation[index].currentFrame >= animation[index].frames.size()) + if (animation[currentAnimation].currentFrame >= animation[currentAnimation].frames.size()) { - if (animation[index].loop) - animation[index].counter = 0; + if (animation[currentAnimation].loop) + animation[currentAnimation].counter = 0; else - animation[index].currentFrame = animation[index].frames.size(); + animation[currentAnimation].currentFrame = animation[currentAnimation].frames.size(); } // En caso contrario else { // Escoge el frame correspondiente de la animación - setSpriteClip(animation[index].frames[animation[index].currentFrame]); + setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]); // Incrementa el contador de la animacion - animation[index].counter++; + animation[currentAnimation].counter++; } } } @@ -231,4 +232,24 @@ bool AnimatedSprite::load(std::string filePath) } return success; +} + +// Establece la animacion actual +void AnimatedSprite::setCurrentAnimation(std::string name) +{ + const int newAnimation = getIndex(name); + if (currentAnimation != newAnimation) + { + currentAnimation = newAnimation; + animation[currentAnimation].currentFrame = 0; + animation[currentAnimation].counter = 0; + animation[currentAnimation].completed = false; + } +} + +// Actualiza las variables del objeto +void AnimatedSprite::update() +{ + animate(); + MovingSprite::update(); } \ No newline at end of file diff --git a/source/animatedsprite.h b/source/animatedsprite.h index 28fba15..ad8881e 100644 --- a/source/animatedsprite.h +++ b/source/animatedsprite.h @@ -24,16 +24,17 @@ private: int counter; // Contador para las animaciones }; std::vector animation; // Vector con las diferentes animaciones + int currentAnimation; // Animacion activa public: // Constructor - AnimatedSprite(LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr); + AnimatedSprite(LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file=""); // Destructor ~AnimatedSprite(); - // Calcula el frame correspondiente a la animación - void animate(std::string name); + // Calcula el frame correspondiente a la animación actual + void animate(); // Establece el frame actual de la animación void setCurrentFrame(std::string name, int num); @@ -61,6 +62,12 @@ public: // Carga la animación desde un fichero bool load(std::string filePath); + + // Establece la animacion actual + void setCurrentAnimation(std::string name); + + // Actualiza las variables del objeto + void update(); }; #endif \ No newline at end of file diff --git a/source/game.cpp b/source/game.cpp index e6244cf..a039576 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -43,9 +43,6 @@ section_t Game::run() // Inicializa las variables necesarias para la sección 'Game' void Game::init() { - // Carga los recursos - // loadMedia(); - ticks = 0; ticksSpeed = 15; diff --git a/source/map.cpp b/source/map.cpp index 5446f30..021bf68 100644 --- a/source/map.cpp +++ b/source/map.cpp @@ -184,9 +184,11 @@ void Map::fillMapTexture() SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00); SDL_RenderClear(renderer); - SDL_Rect clip = {0, 0, 320, 240}; + // Dibuja la textura de fondo + SDL_Rect clip = {0, 0, 320, 240-32}; texture_bg->render(renderer, 0, 0, &clip); + // Dibuja el mapeado de tiles const int tile_size = 16; const int tileset_width_in_tiles = 16; const int map_width_in_tiles = 20; @@ -208,8 +210,6 @@ void Map::fillMapTexture() // Dibuja el mapa en pantalla void Map::render() { - // Dibuja el fondo - // Dibuja la textura con el mapa en pantalla SDL_Rect rect = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT}; SDL_RenderCopy(renderer, map_texture, &rect, NULL); diff --git a/source/player.cpp b/source/player.cpp index 8b43d9c..1404b66 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -1,10 +1,5 @@ #include "player.h" -#define LEFT 0 -#define RIGHT 1 - -#define BASE_SPEED 0.5; - // Constructor Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input) { @@ -19,32 +14,20 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input) texture = new LTexture(); loadTextureFromFile(texture, asset->get("player.png"), renderer); - sprite = new AnimatedSprite(texture, renderer); + sprite = new AnimatedSprite(texture, renderer, asset->get("player.ani")); sprite->setPosX(16); - sprite->setPosY(160 + 16 - 8); - sprite->setSpriteClip(0, 0, 16, 24); - sprite->load(asset->get("player.ani")); - sprite->animate("stand"); + sprite->setPosY(168); + sprite->setCurrentAnimation("stand"); - direction = RIGHT; - respawn_x = sprite->getPosX(); - respawn_y = sprite->getPosY(); - respawn_direction = direction; - speed_x = BASE_SPEED; - speed_y = 0; can_jump = true; - jump_pressed_now = false; - jump_pressed_before = false; standing = true; invulnerable = true; jumpforce = 10; - active_animation = 0; enabled = true; cooldown = 0; lifes = 10; coins = 0; - for (Uint8 i = 0; i < 6; i++) - key[i] = false; + key.insert(key.end(), {0, 0, 0, 0, 0, 0}); } // Destructor @@ -65,14 +48,6 @@ void Player::update() { checkInput(); sprite->update(); - if (sprite->getVelX() == 0) - { - sprite->animate("stand"); - } - else - { - sprite->animate("walk"); - } } // Dibuja el objeto @@ -84,19 +59,23 @@ void Player::render() // Comprueba las entradas y modifica variables void Player::checkInput() { + const float speed = 1.0f; // Solo comprueba las entradas de dirección cuando está de pie if (input->checkInput(INPUT_LEFT, REPEAT_TRUE)) { - sprite->setVelX(-0.6f); + sprite->setVelX(-speed); sprite->setFlip(SDL_FLIP_NONE); + sprite->setCurrentAnimation("walk"); } else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE)) { - sprite->setVelX(0.6f); + sprite->setVelX(speed); sprite->setFlip(SDL_FLIP_HORIZONTAL); + sprite->setCurrentAnimation("walk"); } else { sprite->setVelX(0); + sprite->setCurrentAnimation("stand"); } } \ No newline at end of file diff --git a/source/player.h b/source/player.h index 17806a8..aa94e77 100644 --- a/source/player.h +++ b/source/player.h @@ -19,28 +19,18 @@ private: AnimatedSprite *sprite; // Objeto con los graficos, animaciones y posición del jugador LTexture *texture; // Textura con los graficos del jugador - bool can_jump; // Si puede saltar - bool enabled; // Si está habilitado - bool jump_pressed_before; // Si se ha pulsado el botón de salto previamente - bool jump_pressed_now; // Si se acaba de pulsar el salto - bool key[6]; // Indica las llaves que posee el jugador - bool standing; // Si esta de pie (o quieto?) - //bool was_on_background; // Si viene de una zona atravesable - bool invulnerable; // Si es invulnerable - int coins; // Cantidad de monedas - int cooldown; // Tiempo de inhabilitación - int jumpforce; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar - int respawn_x; // Coordenadas para revivir - int respawn_y; // Coordenades para revivir - float speed_x; // Cantidad de pixeles a desplazarse - float speed_y; // Cantidad de pixels a desplazarse - JA_Sound sound_coin; // Sonido al coger monedas - JA_Sound sound_death; // Sonido al morir - JA_Sound sound_jump; // Sonido al saltar - Uint8 active_animation; // Animación activa - Uint8 direction; // Sentido del desplazamiento - Uint8 lifes; // Cantidad de vidas - Uint8 respawn_direction; // Dirección para revivir + bool can_jump; // Si puede saltar + bool enabled; // Si está habilitado + bool standing; // Si esta de pie (o quieto?) + bool invulnerable; // Si es invulnerable + int coins; // Cantidad de monedas + int cooldown; // Tiempo de inhabilitación + int jumpforce; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar + int lifes; // Cantidad de vidas + std::vector key; // Indica las llaves que posee el jugador + JA_Sound sound_coin; // Sonido al coger monedas + JA_Sound sound_death; // Sonido al morir + JA_Sound sound_jump; // Sonido al saltar // Comprueba las entradas y modifica variables void checkInput();