Finalizado el nuevo motor de animaciones

This commit is contained in:
2022-08-13 11:07:04 +02:00
parent d2a2a1625d
commit e85f138be5
7 changed files with 142 additions and 59 deletions

View File

@@ -4,14 +4,14 @@ tile_height=24
[animation] [animation]
name=stand name=stand
speed=10 speed=8
loop=yes loop=yes
frames=0,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,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
[/animation] [/animation]
[animation] [animation]
name=walk name=walk
speed=10 speed=6
loop=yes loop=yes
frames=8,9,10,10,9,8,11,12,13,13,14,15 frames=8,9,10,10,9,8,11,12,13,13,14,15
[/animation] [/animation]

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<map version="1.9" tiledversion="1.9.0" orientation="orthogonal" renderorder="right-down" width="20" height="13" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="1"> <map version="1.9" tiledversion="1.9.1" orientation="orthogonal" renderorder="right-down" width="20" height="13" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="1">
<tileset firstgid="1" source="../../../volcano_2022_resources/tilesets/surface.tsx"/> <tileset firstgid="1" source="../../../volcano_2022_resources/media_work/map_work/tiles_surface.tsx"/>
<layer id="1" name="tiles" width="20" height="13"> <layer id="1" name="tiles" width="20" height="13">
<data encoding="csv"> <data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
@@ -14,7 +14,7 @@
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,68,68,0,0,0,0,0,0,0,0,74,74,74,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,74,74,74,0,0,0,0,
74,74,74,75,75,74,74,68,68,68,68,68,74,74,78,74,68,74,68,68 74,74,74,75,75,74,74,68,68,68,68,68,74,74,78,74,68,74,68,68
</data> </data>
</layer> </layer>

View File

@@ -12,13 +12,18 @@ AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer)
// Destructor // Destructor
AnimatedSprite::~AnimatedSprite() AnimatedSprite::~AnimatedSprite()
{ {
for (auto a : animation)
{
a.frames.clear();
}
animation.clear();
} }
// Obtiene el indice de la animación a partir del nombre // Obtiene el indice de la animación a partir del nombre
int AnimatedSprite::getIndex(std::string name) int AnimatedSprite::getIndex(std::string name)
{ {
int result = -1; int result = -1;
for (int i = 0; i < animation.size(), i++) for (int i = 0; i < animation.size(); i++)
{ {
if (animation[i].name == name) if (animation[i].name == name)
{ {
@@ -104,55 +109,124 @@ SDL_Rect AnimatedSprite::getAnimationClip(std::string name, Uint8 index)
// Carga la animación desde un fichero // Carga la animación desde un fichero
bool AnimatedSprite::load(std::string filePath) bool AnimatedSprite::load(std::string filePath)
{ {
} int png_width_in_tiles = 0;
int tile_width = 0;
// Asigna variables a partir de dos cadenas int tile_height = 0;
bool AnimatedSprite::setVars(int index, std::string var, std::string value) // Indicador de éxito en la carga
{
// Indicador de éxito en la asignación
bool success = true; bool success = true;
if (var == "name") const std::string filename = filePath.substr(filePath.find_last_of("\\/") + 1);
std::ifstream file(filePath);
std::string line;
// El fichero se puede abrir
if (file.good())
{ {
animation[index].name = value; // Procesa el fichero linea a linea
printf("Reading file %s\n", filename.c_str());
while (std::getline(file, line))
{
// Si la linea contiene el texto [enemy] se realiza el proceso de carga de un enemigo
if (line == "[animation]")
{
t_animation buffer;
buffer.counter = 0;
buffer.currentFrame = 0;
buffer.completed = false;
do
{
std::getline(file, line);
// Encuentra la posición del caracter '='
int pos = line.find("=");
// Procesa las dos subcadenas
if (pos != line.npos)
{
if (line.substr(0, pos) == "name")
{
buffer.name = line.substr(pos + 1, line.length());
} }
else if (var == "speed") else if (line.substr(0, pos) == "speed")
{ {
animation[index].speed = std::stoi(value); buffer.speed = std::stoi(line.substr(pos + 1, line.length()));
} }
else if (var == "loop") else if (line.substr(0, pos) == "loop")
{ {
if (value == "yes" || value == "true") if (line.substr(pos + 1, line.length()) == "yes" || line.substr(pos + 1, line.length()) == "true")
{ {
animation[index].loop = true; buffer.loop = true;
} }
else else
{ {
animation[index].loop = false; buffer.loop = false;
} }
} }
else if (var == "frames") else if (line.substr(0, pos) == "frames")
{ {
const int w = 16;
const int h = 24;
const int png_width_tiles = 8;
// Se introducen los valores separados por comas en un vector // Se introducen los valores separados por comas en un vector
std::stringstream ss(value); std::stringstream ss(line.substr(pos + 1, line.length()));
std::string tmp; std::string tmp;
SDL_Rect rect = {0, 0, w, h}; SDL_Rect rect = {0, 0, tile_width, tile_height};
while (getline(ss, tmp, ',')) while (getline(ss, tmp, ','))
{ {
int num_tile = std::stoi(tmp); int num_tile = std::stoi(tmp);
rect.x = (num_tile % png_width_tiles) * w; rect.x = (num_tile % png_width_in_tiles) * tile_width;
rect.y = (num_tile / png_width_tiles) * h; rect.y = (num_tile / png_width_in_tiles) * tile_height;
animation[index].frames.push_back(rect); buffer.frames.push_back(rect);
} }
} }
else if (var == "")
{
}
else else
{ {
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
success = false;
}
}
} while (line != "[/animation]");
// Añade el enemigo al vector de enemigos
animation.push_back(buffer);
}
// En caso contrario se parsea el fichero para buscar las variables y los valores
else
{
// Encuentra la posición del caracter '='
int pos = line.find("=");
// Procesa las dos subcadenas
if (pos != line.npos)
{
if (line.substr(0, pos) == "png_width_in_tiles")
{
png_width_in_tiles = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "tile_width")
{
tile_width = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "tile_height")
{
tile_height = std::stoi(line.substr(pos + 1, line.length()));
}
else
{
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
success = false;
}
}
}
}
// Cierra el fichero
printf("Closing file %s\n", filename.c_str());
file.close();
}
// El fichero no se puede abrir
else
{
printf("Warning: Unable to open %s file\n", filename.c_str());
success = false; success = false;
} }

View File

@@ -4,6 +4,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <fstream>
#ifndef ANIMATEDSPRITE_H #ifndef ANIMATEDSPRITE_H
#define ANIMATEDSPRITE_H #define ANIMATEDSPRITE_H
@@ -60,9 +61,6 @@ 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);
// Asigna variables a partir de dos cadenas
bool setVars(int index, std::string var, std::string value);
}; };
#endif #endif

View File

@@ -20,9 +20,11 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input)
loadTextureFromFile(texture, asset->get("player.png"), renderer); loadTextureFromFile(texture, asset->get("player.png"), renderer);
sprite = new AnimatedSprite(texture, renderer); sprite = new AnimatedSprite(texture, renderer);
sprite->setPosX(0); sprite->setPosX(16);
sprite->setPosY(0); sprite->setPosY(160 + 16 - 8);
sprite->setSpriteClip(0, 0, 16, 24); sprite->setSpriteClip(0, 0, 16, 24);
sprite->load(asset->get("player.ani"));
sprite->animate("stand");
direction = RIGHT; direction = RIGHT;
respawn_x = sprite->getPosX(); respawn_x = sprite->getPosX();
@@ -63,6 +65,14 @@ 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
@@ -78,12 +88,12 @@ void Player::checkInput()
if (input->checkInput(INPUT_LEFT, REPEAT_TRUE)) if (input->checkInput(INPUT_LEFT, REPEAT_TRUE))
{ {
sprite->setVelX(-0.6f); sprite->setVelX(-0.6f);
sprite->setFlip(SDL_FLIP_HORIZONTAL); sprite->setFlip(SDL_FLIP_NONE);
} }
else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE)) else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE))
{ {
sprite->setVelX(0.6f); sprite->setVelX(0.6f);
sprite->setFlip(SDL_FLIP_NONE); sprite->setFlip(SDL_FLIP_HORIZONTAL);
} }
else else
{ {

View File

@@ -25,15 +25,15 @@ private:
bool jump_pressed_now; // Si se acaba de pulsar el salto bool jump_pressed_now; // Si se acaba de pulsar el salto
bool key[6]; // Indica las llaves que posee el jugador bool key[6]; // Indica las llaves que posee el jugador
bool standing; // Si esta de pie (o quieto?) bool standing; // Si esta de pie (o quieto?)
bool was_on_background; // Si viene de una zona atravesable //bool was_on_background; // Si viene de una zona atravesable
bool invulnerable; // Si es invulnerable bool invulnerable; // Si es invulnerable
int coins; // Cantidad de monedas int coins; // Cantidad de monedas
int cooldown; // Tiempo de inhabilitación int cooldown; // Tiempo de inhabilitación
int jumpforce; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar int jumpforce; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar
int respawn_x; // Coordenadas para revivir int respawn_x; // Coordenadas para revivir
int respawn_y; // Coordenades para revivir int respawn_y; // Coordenades para revivir
int speed_x; // Cantidad de pixeles a desplazarse float speed_x; // Cantidad de pixeles a desplazarse
int speed_y; // Cantidad de pixels a desplazarse float speed_y; // Cantidad de pixels a desplazarse
JA_Sound sound_coin; // Sonido al coger monedas JA_Sound sound_coin; // Sonido al coger monedas
JA_Sound sound_death; // Sonido al morir JA_Sound sound_death; // Sonido al morir
JA_Sound sound_jump; // Sonido al saltar JA_Sound sound_jump; // Sonido al saltar

View File

@@ -6,7 +6,7 @@ Prog::Prog(std::string executablePath)
// Establece las opciones por defecto // Establece las opciones por defecto
options = new options_t; options = new options_t;
options->fullScreenMode = 0; options->fullScreenMode = 0;
options->windowSize = 3; options->windowSize = 2;
options->filter = FILTER_NEAREST; options->filter = FILTER_NEAREST;
options->vSync = true; options->vSync = true;
options->screenWidth = GAME_WIDTH * options->windowSize; options->screenWidth = GAME_WIDTH * options->windowSize;
@@ -141,6 +141,7 @@ bool Prog::setFileList()
asset->add("/data/map/01.tmx", data); asset->add("/data/map/01.tmx", data);
asset->add("/data/config.bin", data, false); asset->add("/data/config.bin", data, false);
asset->add("/data/gamecontrollerdb.txt", data); asset->add("/data/gamecontrollerdb.txt", data);
asset->add("/data/animations/player.ani", data);
// Texturas // Texturas
asset->add("/media/gfx/actors.png", bitmap); asset->add("/media/gfx/actors.png", bitmap);