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
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]

View File

@@ -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();
}

View File

@@ -24,16 +24,17 @@ private:
int counter; // Contador para las animaciones
};
std::vector<t_animation> 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

View File

@@ -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;

View File

@@ -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);

View File

@@ -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");
}
}

View File

@@ -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<bool> 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();