Implementadas las animaciones en el jugador

This commit is contained in:
2022-08-13 12:14:31 +02:00
parent e85f138be5
commit a1eaf8af07
7 changed files with 68 additions and 74 deletions

View File

@@ -6,7 +6,7 @@ tile_height=24
name=stand name=stand
speed=8 speed=8
loop=yes 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]
[animation] [animation]

View File

@@ -2,11 +2,14 @@
#include "animatedsprite.h" #include "animatedsprite.h"
// Constructor // Constructor
AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer) AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer, std::string file)
{ {
// Copia los punteros // Copia los punteros
setTexture(texture); setTexture(texture);
setRenderer(renderer); setRenderer(renderer);
// Carga las animaciones
load(file);
} }
// Destructor // Destructor
@@ -34,32 +37,30 @@ int AnimatedSprite::getIndex(std::string name)
} }
// Calcula el frame correspondiente a la animación // Calcula el frame correspondiente a la animación
void AnimatedSprite::animate(std::string name) void AnimatedSprite::animate()
{ {
if (mEnabled) if (mEnabled)
{ {
const int index = getIndex(name);
// Calcula el frame actual a partir del contador // 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 // Si alcanza el final de la animación, reinicia el contador de la animación
// en función de la variable loop // 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) if (animation[currentAnimation].loop)
animation[index].counter = 0; animation[currentAnimation].counter = 0;
else else
animation[index].currentFrame = animation[index].frames.size(); animation[currentAnimation].currentFrame = animation[currentAnimation].frames.size();
} }
// En caso contrario // En caso contrario
else else
{ {
// Escoge el frame correspondiente de la animación // 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 // Incrementa el contador de la animacion
animation[index].counter++; animation[currentAnimation].counter++;
} }
} }
} }
@@ -231,4 +232,24 @@ bool AnimatedSprite::load(std::string filePath)
} }
return success; 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();
} }

View File

@@ -24,16 +24,17 @@ private:
int counter; // Contador para las animaciones int counter; // Contador para las animaciones
}; };
std::vector<t_animation> animation; // Vector con las diferentes animaciones std::vector<t_animation> animation; // Vector con las diferentes animaciones
int currentAnimation; // Animacion activa
public: public:
// Constructor // Constructor
AnimatedSprite(LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr); AnimatedSprite(LTexture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file="");
// Destructor // Destructor
~AnimatedSprite(); ~AnimatedSprite();
// Calcula el frame correspondiente a la animación // Calcula el frame correspondiente a la animación actual
void animate(std::string name); void animate();
// Establece el frame actual de la animación // Establece el frame actual de la animación
void setCurrentFrame(std::string name, int num); void setCurrentFrame(std::string name, int num);
@@ -61,6 +62,12 @@ public:
// Carga la animación desde un fichero // Carga la animación desde un fichero
bool load(std::string filePath); bool load(std::string filePath);
// Establece la animacion actual
void setCurrentAnimation(std::string name);
// Actualiza las variables del objeto
void update();
}; };
#endif #endif

View File

@@ -43,9 +43,6 @@ section_t Game::run()
// Inicializa las variables necesarias para la sección 'Game' // Inicializa las variables necesarias para la sección 'Game'
void Game::init() void Game::init()
{ {
// Carga los recursos
// loadMedia();
ticks = 0; ticks = 0;
ticksSpeed = 15; ticksSpeed = 15;

View File

@@ -184,9 +184,11 @@ void Map::fillMapTexture()
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00); SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(renderer); 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); texture_bg->render(renderer, 0, 0, &clip);
// Dibuja el mapeado de tiles
const int tile_size = 16; const int tile_size = 16;
const int tileset_width_in_tiles = 16; const int tileset_width_in_tiles = 16;
const int map_width_in_tiles = 20; const int map_width_in_tiles = 20;
@@ -208,8 +210,6 @@ void Map::fillMapTexture()
// Dibuja el mapa en pantalla // Dibuja el mapa en pantalla
void Map::render() void Map::render()
{ {
// Dibuja el fondo
// Dibuja la textura con el mapa en pantalla // Dibuja la textura con el mapa en pantalla
SDL_Rect rect = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT}; SDL_Rect rect = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT};
SDL_RenderCopy(renderer, map_texture, &rect, NULL); SDL_RenderCopy(renderer, map_texture, &rect, NULL);

View File

@@ -1,10 +1,5 @@
#include "player.h" #include "player.h"
#define LEFT 0
#define RIGHT 1
#define BASE_SPEED 0.5;
// Constructor // Constructor
Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input) 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(); texture = new LTexture();
loadTextureFromFile(texture, asset->get("player.png"), renderer); 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->setPosX(16);
sprite->setPosY(160 + 16 - 8); sprite->setPosY(168);
sprite->setSpriteClip(0, 0, 16, 24); sprite->setCurrentAnimation("stand");
sprite->load(asset->get("player.ani"));
sprite->animate("stand");
direction = RIGHT;
respawn_x = sprite->getPosX();
respawn_y = sprite->getPosY();
respawn_direction = direction;
speed_x = BASE_SPEED;
speed_y = 0;
can_jump = true; can_jump = true;
jump_pressed_now = false;
jump_pressed_before = false;
standing = true; standing = true;
invulnerable = true; invulnerable = true;
jumpforce = 10; jumpforce = 10;
active_animation = 0;
enabled = true; enabled = true;
cooldown = 0; cooldown = 0;
lifes = 10; lifes = 10;
coins = 0; coins = 0;
for (Uint8 i = 0; i < 6; i++) key.insert(key.end(), {0, 0, 0, 0, 0, 0});
key[i] = false;
} }
// Destructor // Destructor
@@ -65,14 +48,6 @@ void Player::update()
{ {
checkInput(); checkInput();
sprite->update(); sprite->update();
if (sprite->getVelX() == 0)
{
sprite->animate("stand");
}
else
{
sprite->animate("walk");
}
} }
// Dibuja el objeto // Dibuja el objeto
@@ -84,19 +59,23 @@ void Player::render()
// Comprueba las entradas y modifica variables // Comprueba las entradas y modifica variables
void Player::checkInput() void Player::checkInput()
{ {
const float speed = 1.0f;
// Solo comprueba las entradas de dirección cuando está de pie // Solo comprueba las entradas de dirección cuando está de pie
if (input->checkInput(INPUT_LEFT, REPEAT_TRUE)) if (input->checkInput(INPUT_LEFT, REPEAT_TRUE))
{ {
sprite->setVelX(-0.6f); sprite->setVelX(-speed);
sprite->setFlip(SDL_FLIP_NONE); sprite->setFlip(SDL_FLIP_NONE);
sprite->setCurrentAnimation("walk");
} }
else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE)) else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE))
{ {
sprite->setVelX(0.6f); sprite->setVelX(speed);
sprite->setFlip(SDL_FLIP_HORIZONTAL); sprite->setFlip(SDL_FLIP_HORIZONTAL);
sprite->setCurrentAnimation("walk");
} }
else else
{ {
sprite->setVelX(0); sprite->setVelX(0);
sprite->setCurrentAnimation("stand");
} }
} }

View File

@@ -19,28 +19,18 @@ private:
AnimatedSprite *sprite; // Objeto con los graficos, animaciones y posición del jugador AnimatedSprite *sprite; // Objeto con los graficos, animaciones y posición del jugador
LTexture *texture; // Textura con los graficos del jugador LTexture *texture; // Textura con los graficos del jugador
bool can_jump; // Si puede saltar bool can_jump; // Si puede saltar
bool enabled; // Si está habilitado bool enabled; // Si está habilitado
bool jump_pressed_before; // Si se ha pulsado el botón de salto previamente bool standing; // Si esta de pie (o quieto?)
bool jump_pressed_now; // Si se acaba de pulsar el salto bool invulnerable; // Si es invulnerable
bool key[6]; // Indica las llaves que posee el jugador int coins; // Cantidad de monedas
bool standing; // Si esta de pie (o quieto?) int cooldown; // Tiempo de inhabilitación
//bool was_on_background; // Si viene de una zona atravesable int jumpforce; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar
bool invulnerable; // Si es invulnerable int lifes; // Cantidad de vidas
int coins; // Cantidad de monedas std::vector<bool> key; // Indica las llaves que posee el jugador
int cooldown; // Tiempo de inhabilitación JA_Sound sound_coin; // Sonido al coger monedas
int jumpforce; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar JA_Sound sound_death; // Sonido al morir
int respawn_x; // Coordenadas para revivir JA_Sound sound_jump; // Sonido al saltar
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
// Comprueba las entradas y modifica variables // Comprueba las entradas y modifica variables
void checkInput(); void checkInput();