diff --git a/data/animations/player.ani b/data/animations/player.ani index ac153c7..5f01fc6 100644 --- a/data/animations/player.ani +++ b/data/animations/player.ani @@ -4,14 +4,14 @@ tile_height=24 [animation] name=stand -speed=10 +speed=8 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] name=walk -speed=10 +speed=6 loop=yes frames=8,9,10,10,9,8,11,12,13,13,14,15 [/animation] diff --git a/data/map/01.tmx b/data/map/01.tmx index 698e5b5..8a7a433 100644 --- a/data/map/01.tmx +++ b/data/map/01.tmx @@ -1,6 +1,6 @@ - - + + 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,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 diff --git a/source/animatedsprite.cpp b/source/animatedsprite.cpp index 9c0f427..8e2090c 100644 --- a/source/animatedsprite.cpp +++ b/source/animatedsprite.cpp @@ -12,13 +12,18 @@ AnimatedSprite::AnimatedSprite(LTexture *texture, SDL_Renderer *renderer) // Destructor AnimatedSprite::~AnimatedSprite() { + for (auto a : animation) + { + a.frames.clear(); + } + animation.clear(); } // Obtiene el indice de la animación a partir del nombre int AnimatedSprite::getIndex(std::string name) { int result = -1; - for (int i = 0; i < animation.size(), i++) + for (int i = 0; i < animation.size(); i++) { 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 bool AnimatedSprite::load(std::string filePath) { -} - -// Asigna variables a partir de dos cadenas -bool AnimatedSprite::setVars(int index, std::string var, std::string value) -{ - // Indicador de éxito en la asignación + int png_width_in_tiles = 0; + int tile_width = 0; + int tile_height = 0; + // Indicador de éxito en la carga 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; - } - else if (var == "speed") - { - animation[index].speed = std::stoi(value); - } - else if (var == "loop") - { - if (value == "yes" || value == "true") + // Procesa el fichero linea a linea + printf("Reading file %s\n", filename.c_str()); + while (std::getline(file, line)) { - animation[index].loop = true; - } - else - { - animation[index].loop = false; + // 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 (line.substr(0, pos) == "speed") + { + buffer.speed = std::stoi(line.substr(pos + 1, line.length())); + } + else if (line.substr(0, pos) == "loop") + { + if (line.substr(pos + 1, line.length()) == "yes" || line.substr(pos + 1, line.length()) == "true") + { + buffer.loop = true; + } + else + { + buffer.loop = false; + } + } + else if (line.substr(0, pos) == "frames") + { + // Se introducen los valores separados por comas en un vector + std::stringstream ss(line.substr(pos + 1, line.length())); + std::string tmp; + SDL_Rect rect = {0, 0, tile_width, tile_height}; + while (getline(ss, tmp, ',')) + { + int num_tile = std::stoi(tmp); + rect.x = (num_tile % png_width_in_tiles) * tile_width; + rect.y = (num_tile / png_width_in_tiles) * tile_height; + buffer.frames.push_back(rect); + } + } + 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(); } - else if (var == "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 - std::stringstream ss(value); - std::string tmp; - SDL_Rect rect = {0, 0, w, h}; - while (getline(ss, tmp, ',')) - { - int num_tile = std::stoi(tmp); - rect.x = (num_tile % png_width_tiles) * w; - rect.y = (num_tile / png_width_tiles) * h; - animation[index].frames.push_back(rect); - } - } - else if (var == "") - { - } + // El fichero no se puede abrir else { + printf("Warning: Unable to open %s file\n", filename.c_str()); success = false; } diff --git a/source/animatedsprite.h b/source/animatedsprite.h index 72850ca..28fba15 100644 --- a/source/animatedsprite.h +++ b/source/animatedsprite.h @@ -4,6 +4,7 @@ #include #include #include +#include #ifndef ANIMATEDSPRITE_H #define ANIMATEDSPRITE_H @@ -60,9 +61,6 @@ public: // Carga la animación desde un fichero bool load(std::string filePath); - - // Asigna variables a partir de dos cadenas - bool setVars(int index, std::string var, std::string value); }; #endif \ No newline at end of file diff --git a/source/player.cpp b/source/player.cpp index 8abdc62..8b43d9c 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -20,9 +20,11 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input) loadTextureFromFile(texture, asset->get("player.png"), renderer); sprite = new AnimatedSprite(texture, renderer); - sprite->setPosX(0); - sprite->setPosY(0); + sprite->setPosX(16); + sprite->setPosY(160 + 16 - 8); sprite->setSpriteClip(0, 0, 16, 24); + sprite->load(asset->get("player.ani")); + sprite->animate("stand"); direction = RIGHT; respawn_x = sprite->getPosX(); @@ -63,6 +65,14 @@ void Player::update() { checkInput(); sprite->update(); + if (sprite->getVelX() == 0) + { + sprite->animate("stand"); + } + else + { + sprite->animate("walk"); + } } // Dibuja el objeto @@ -78,12 +88,12 @@ void Player::checkInput() if (input->checkInput(INPUT_LEFT, REPEAT_TRUE)) { sprite->setVelX(-0.6f); - sprite->setFlip(SDL_FLIP_HORIZONTAL); + sprite->setFlip(SDL_FLIP_NONE); } else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE)) { sprite->setVelX(0.6f); - sprite->setFlip(SDL_FLIP_NONE); + sprite->setFlip(SDL_FLIP_HORIZONTAL); } else { diff --git a/source/player.h b/source/player.h index 9a3ccaf..17806a8 100644 --- a/source/player.h +++ b/source/player.h @@ -25,15 +25,15 @@ private: 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 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 - int speed_x; // Cantidad de pixeles a desplazarse - int speed_y; // Cantidad de pixels a desplazarse + 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 diff --git a/source/prog.cpp b/source/prog.cpp index 6c28c39..4e7eac8 100644 --- a/source/prog.cpp +++ b/source/prog.cpp @@ -6,7 +6,7 @@ Prog::Prog(std::string executablePath) // Establece las opciones por defecto options = new options_t; options->fullScreenMode = 0; - options->windowSize = 3; + options->windowSize = 2; options->filter = FILTER_NEAREST; options->vSync = true; options->screenWidth = GAME_WIDTH * options->windowSize; @@ -141,6 +141,7 @@ bool Prog::setFileList() asset->add("/data/map/01.tmx", data); asset->add("/data/config.bin", data, false); asset->add("/data/gamecontrollerdb.txt", data); + asset->add("/data/animations/player.ani", data); // Texturas asset->add("/media/gfx/actors.png", bitmap);