diff --git a/data/actors/enemies/bug.ani b/data/actors/enemies/bug.ani index 11ba1b9..3580193 100644 --- a/data/actors/enemies/bug.ani +++ b/data/actors/enemies/bug.ani @@ -1,4 +1,3 @@ -frames_per_row=6 frame_width=16 frame_height=16 diff --git a/data/actors/enemies/flying_eye.ani b/data/actors/enemies/flying_eye.ani index 11ba1b9..3580193 100644 --- a/data/actors/enemies/flying_eye.ani +++ b/data/actors/enemies/flying_eye.ani @@ -1,4 +1,3 @@ -frames_per_row=6 frame_width=16 frame_height=16 diff --git a/data/actors/enemies/flying_eye_horn.ani b/data/actors/enemies/flying_eye_horn.ani index 98b9ec2..3943028 100644 --- a/data/actors/enemies/flying_eye_horn.ani +++ b/data/actors/enemies/flying_eye_horn.ani @@ -1,4 +1,3 @@ -frames_per_row=6 frame_width=16 frame_height=16 diff --git a/data/actors/enemies/walking_eye.ani b/data/actors/enemies/walking_eye.ani index 11ba1b9..3580193 100644 --- a/data/actors/enemies/walking_eye.ani +++ b/data/actors/enemies/walking_eye.ani @@ -1,4 +1,3 @@ -frames_per_row=6 frame_width=16 frame_height=16 diff --git a/data/actors/items/diamond.ani b/data/actors/items/diamond.ani index d7b2d89..2e937a7 100644 --- a/data/actors/items/diamond.ani +++ b/data/actors/items/diamond.ani @@ -1,4 +1,3 @@ -frames_per_row=8 frame_width=16 frame_height=16 diff --git a/data/actors/moving_platform.ani b/data/actors/moving_platform.ani index e70220c..f10bab6 100644 --- a/data/actors/moving_platform.ani +++ b/data/actors/moving_platform.ani @@ -1,4 +1,3 @@ -frames_per_row=1 frame_width=16 frame_height=8 diff --git a/data/config/config.txt b/data/config/config.txt index 56c3ec4..8074f6a 100644 --- a/data/config/config.txt +++ b/data/config/config.txt @@ -1,5 +1,5 @@ fullScreenMode=0 -windowSize=3 +windowSize=2 filter=FILTER_NEAREST vSync=true integerScale=true diff --git a/data/intro/intro.ani b/data/intro/intro.ani index c644e14..a2fdcf7 100644 --- a/data/intro/intro.ani +++ b/data/intro/intro.ani @@ -1,4 +1,3 @@ -frames_per_row=4 frame_width=320 frame_height=240 diff --git a/data/player/player.ani b/data/player/player.ani index 66f4de9..0654844 100644 --- a/data/player/player.ani +++ b/data/player/player.ani @@ -1,4 +1,3 @@ -frames_per_row=14 frame_width=16 frame_height=24 diff --git a/source/animatedsprite.cpp b/source/animatedsprite.cpp index a2036a0..51cca79 100644 --- a/source/animatedsprite.cpp +++ b/source/animatedsprite.cpp @@ -1,4 +1,4 @@ -#include "const.h" + #include "animatedsprite.h" // Constructor @@ -29,61 +29,72 @@ AnimatedSprite::~AnimatedSprite() int AnimatedSprite::getIndex(std::string name) { int index = -1; - for (int i = 0; i < animation.size(); i++) + + for (auto a : animation) { - if (animation[i].name == name) + index++; + if (a.name == name) { - index = i; + return index; } } - if (index == -1) - { - printf("** Warning: could not find \"%s\" animation\n", name.c_str()); - index = 0; - } - return index; + printf("** Warning: could not find \"%s\" animation\n", name.c_str()); + + return -1; } // Calcula el frame correspondiente a la animación void AnimatedSprite::animate() { - if (enabled) + if (!enabled || animation[currentAnimation].speed == 0) { - // Calcula el frame actual a partir del contador - animation[currentAnimation].currentFrame = animation[currentAnimation].counter / animation[currentAnimation].speed; + return; + } - // Si alcanza el final de la animación, reinicia el contador de la animación - // en función de la variable loop y coloca el nuevo frame - if (animation[currentAnimation].currentFrame >= animation[currentAnimation].frames.size()) - { - if (animation[currentAnimation].loop == -1) - { // Si no hay loop, deja el último frame - animation[currentAnimation].currentFrame = animation[currentAnimation].frames.size(); - animation[currentAnimation].completed = true; - } - else - { // Si hay loop, vuelve al frame indicado - animation[currentAnimation].counter = 0; - animation[currentAnimation].currentFrame = animation[currentAnimation].loop; - } + // Calcula el frame actual a partir del contador + 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 y coloca el nuevo frame + if (animation[currentAnimation].currentFrame >= animation[currentAnimation].frames.size()) + { + if (animation[currentAnimation].loop == -1) + { // Si no hay loop, deja el último frame + animation[currentAnimation].currentFrame = animation[currentAnimation].frames.size(); + animation[currentAnimation].completed = true; } - // En caso contrario else - { - // Escoge el frame correspondiente de la animación - setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]); - - // Incrementa el contador de la animacion - animation[currentAnimation].counter++; + { // Si hay loop, vuelve al frame indicado + animation[currentAnimation].counter = 0; + animation[currentAnimation].currentFrame = animation[currentAnimation].loop; } } + // En caso contrario + else + { + // Escoge el frame correspondiente de la animación + setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]); + + // Incrementa el contador de la animacion + animation[currentAnimation].counter++; + } } // Establece el frame actual de la animación -void AnimatedSprite::setCurrentFrame(std::string name, int num) +void AnimatedSprite::setCurrentFrame(int num) { - animation[getIndex(name)].currentFrame = num; + // Descarta valores fuera de rango + if (num >= animation[currentAnimation].frames.size()) + { + num = 0; + } + + // Cambia el valor de la variable + animation[currentAnimation].counter = animation[currentAnimation].speed * num; + + // Escoge el frame correspondiente de la animación + setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]); } // Establece el valor del contador @@ -143,7 +154,7 @@ bool AnimatedSprite::load(std::string filePath) 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 + // Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación if (line == "[animation]") { t_animation buffer; @@ -195,7 +206,7 @@ bool AnimatedSprite::load(std::string filePath) } } while (line != "[/animation]"); - // Añade el enemigo al vector de enemigos + // Añade la animación al vector de animaciones animation.push_back(buffer); } @@ -212,14 +223,23 @@ bool AnimatedSprite::load(std::string filePath) { frames_per_row = std::stoi(line.substr(pos + 1, line.length())); } + else if (line.substr(0, pos) == "frame_width") { frame_width = std::stoi(line.substr(pos + 1, line.length())); + + // Normaliza valores + if (frames_per_row == 0) + { + frames_per_row = texture->getWidth() / frame_width; + } } + else if (line.substr(0, pos) == "frame_height") { frame_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()); diff --git a/source/animatedsprite.h b/source/animatedsprite.h index b4eb517..1a32623 100644 --- a/source/animatedsprite.h +++ b/source/animatedsprite.h @@ -38,7 +38,7 @@ public: void animate(); // Establece el frame actual de la animación - void setCurrentFrame(std::string name, int num); + void setCurrentFrame(int num); // Establece el valor del contador void setAnimationCounter(std::string name, int num);