diff --git a/data/room/01.room b/data/room/01.room index 08cc9e5..326940a 100644 --- a/data/room/01.room +++ b/data/room/01.room @@ -1,5 +1,5 @@ name=void main -bgColor=light_black +bgColor=black tileset=standard.png roomUp=0 roomDown=0 diff --git a/media/music/jd.ogg b/media/music/jd.ogg new file mode 100644 index 0000000..0e07b9c Binary files /dev/null and b/media/music/jd.ogg differ diff --git a/media/player/player01.ani b/media/player/player01.ani index 4559475..897504f 100644 --- a/media/player/player01.ani +++ b/media/player/player01.ani @@ -18,7 +18,7 @@ frames=0,1,2,3 [animation] name=walk_menu -speed=50 +speed=0 loop=0 frames=0,1,2,3 [/animation] \ No newline at end of file diff --git a/source/animatedsprite.cpp b/source/animatedsprite.cpp index e7dc797..562614d 100644 --- a/source/animatedsprite.cpp +++ b/source/animatedsprite.cpp @@ -48,42 +48,47 @@ int AnimatedSprite::getIndex(std::string name) // 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; + animation[currentAnimation].currentFrame = num; + + // Escoge el frame correspondiente de la animación + setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]); } // Establece el valor del contador 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); diff --git a/source/director.cpp b/source/director.cpp index ae0edff..6a2e4ba 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -183,6 +183,8 @@ bool Director::setFileList() asset->add("/media/player/player01.ani", data); asset->add("/media/items/items.png", bitmap); + + asset->add("/media/music/jd.ogg", music); return asset->check(); } diff --git a/source/game.cpp b/source/game.cpp index 6e0ded2..f3440a0 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -22,6 +22,7 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input) eventHandler = new SDL_Event(); text = new Text(asset->get("smb2.png"), asset->get("smb2.txt"), renderer); debugText = new Text(asset->get("debug.png"), asset->get("debug.txt"), renderer); + music = JA_LoadMusic(asset->get("jd.ogg").c_str()); // Inicializa variables ticks = 0; @@ -64,6 +65,8 @@ Game::~Game() // Bucle para el juego section_t Game::run() { + JA_PlayMusic(music); + while (section.name == SECTION_PROG_GAME) { // Sección juego jugando @@ -74,6 +77,8 @@ section_t Game::run() } } + JA_StopMusic(); + return section; } @@ -103,6 +108,10 @@ void Game::update() debug = !debug; break; + case SDL_SCANCODE_M: + (JA_GetMusicState() == JA_MUSIC_PLAYING) ? JA_PauseMusic() : JA_ResumeMusic(); + break; + case SDL_SCANCODE_F: screen->switchVideoMode(); break; diff --git a/source/game.h b/source/game.h index 0e81db8..495ead0 100644 --- a/source/game.h +++ b/source/game.h @@ -33,6 +33,7 @@ private: Text *text; // Objeto para los textos del juego Text *debugText; // Objeto para los textos de debug del juego ScoreBoard *scoreboard; // Objeto encargado de gestionar el marcador + JA_Music music; // Musica que suena durante el juego int ticks; // Contador de ticks para ajustar la velocidad del programa int ticksSpeed; // Velocidad a la que se repiten los bucles del programa section_t section; // Seccion actual dentro del juego diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index 9b3a873..bb19e55 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -14,6 +14,7 @@ ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Asset *asset) loadTextureFromFile(texture, asset->get("player01.png"), renderer); sprite = new AnimatedSprite(texture, renderer, asset->get("player01.ani")); sprite->setCurrentAnimation("walk_menu"); + text = new Text(asset->get("smb2.png"), asset->get("smb2.txt"), renderer); // Inicializa las variables counter = 0; @@ -37,6 +38,30 @@ ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Asset *asset) c = stringToColor("yellow"); color.push_back(c); + + c = stringToColor("white"); + color.push_back(c); + + c = stringToColor("light_blue"); + color.push_back(c); + + c = stringToColor("light_red"); + color.push_back(c); + + c = stringToColor("light_purple"); + color.push_back(c); + + c = stringToColor("light_green"); + color.push_back(c); + + c = stringToColor("light_cyan"); + color.push_back(c); + + c = stringToColor("light_yellow"); + color.push_back(c); + + c = stringToColor("light_white"); + color.push_back(c); } // Destructor @@ -48,6 +73,9 @@ ScoreBoard::~ScoreBoard() delete sprite; sprite = nullptr; + + delete text; + text = nullptr; } // Pinta el objeto en pantalla @@ -58,12 +86,15 @@ void ScoreBoard::render() // Dibuja el fondo del marcador const SDL_Rect rect = {0, 17 * BLOCK, PLAY_AREA_WIDTH, GAMECANVAS_HEIGHT - PLAY_AREA_HEIGHT}; - SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderFillRect(renderer, &rect); + // Dibuja las vidas sprite->setPosY(18 * BLOCK); int index; - int desp = (counter / 40) % 8; + const int desp = (counter / 40) % 8; + const int frame = desp % 4; + sprite->setCurrentFrame(frame); for (int i = 0; i < num_lives; i++) { @@ -72,6 +103,11 @@ void ScoreBoard::render() sprite->getTexture()->setColor(color[index].r, color[index].g, color[index].b); sprite->render(); } + + // Escribe los textos + const std::string text = "Items collected 008 Time 88875"; + const color_t color = stringToColor("white"); + this->text->writeColored(BLOCK, 21 * BLOCK, text, color); } // Actualiza las variables del objeto diff --git a/source/scoreboard.h b/source/scoreboard.h index c30a306..c4fed89 100644 --- a/source/scoreboard.h +++ b/source/scoreboard.h @@ -2,6 +2,7 @@ #include #include "utils.h" +#include "text.h" #include "asset.h" #include "animatedsprite.h" #include "const.h" @@ -18,6 +19,7 @@ private: AnimatedSprite *sprite; // Sprite para mostrar las vidas en el marcador SDL_Renderer *renderer; // El renderizador de la ventana Asset *asset; // Objeto con la ruta a todos los ficheros de recursos + Text *text; // Objeto para escribir texto std::vector color; // Vector con los colores del objeto int counter; // Contador interno int colorChangeSpeed; // Cuanto mas alto, mas tarda en cambiar de color