Añadida música

This commit is contained in:
2022-08-31 19:51:00 +02:00
parent f1047a8b07
commit c18a13ec83
10 changed files with 86 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
name=void main name=void main
bgColor=light_black bgColor=black
tileset=standard.png tileset=standard.png
roomUp=0 roomUp=0
roomDown=0 roomDown=0

BIN
media/music/jd.ogg Normal file

Binary file not shown.

View File

@@ -18,7 +18,7 @@ frames=0,1,2,3
[animation] [animation]
name=walk_menu name=walk_menu
speed=50 speed=0
loop=0 loop=0
frames=0,1,2,3 frames=0,1,2,3
[/animation] [/animation]

View File

@@ -48,42 +48,47 @@ int AnimatedSprite::getIndex(std::string name)
// Calcula el frame correspondiente a la animación // Calcula el frame correspondiente a la animación
void AnimatedSprite::animate() void AnimatedSprite::animate()
{ {
if (enabled) if (!enabled || animation[currentAnimation].speed == 0)
{ {
// Calcula el frame actual a partir del contador return;
animation[currentAnimation].currentFrame = animation[currentAnimation].counter / animation[currentAnimation].speed; }
// Si alcanza el final de la animación, reinicia el contador de la animación // Calcula el frame actual a partir del contador
// en función de la variable loop y coloca el nuevo frame animation[currentAnimation].currentFrame = animation[currentAnimation].counter / animation[currentAnimation].speed;
if (animation[currentAnimation].currentFrame >= animation[currentAnimation].frames.size())
{ // Si alcanza el final de la animación, reinicia el contador de la animación
if (animation[currentAnimation].loop == -1) // en función de la variable loop y coloca el nuevo frame
{ // Si no hay loop, deja el último frame if (animation[currentAnimation].currentFrame >= animation[currentAnimation].frames.size())
animation[currentAnimation].currentFrame = animation[currentAnimation].frames.size(); {
animation[currentAnimation].completed = true; if (animation[currentAnimation].loop == -1)
} { // Si no hay loop, deja el último frame
else animation[currentAnimation].currentFrame = animation[currentAnimation].frames.size();
{ // Si hay loop, vuelve al frame indicado animation[currentAnimation].completed = true;
animation[currentAnimation].counter = 0;
animation[currentAnimation].currentFrame = animation[currentAnimation].loop;
}
} }
// En caso contrario
else else
{ { // Si hay loop, vuelve al frame indicado
// Escoge el frame correspondiente de la animación animation[currentAnimation].counter = 0;
setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]); animation[currentAnimation].currentFrame = animation[currentAnimation].loop;
// Incrementa el contador de la animacion
animation[currentAnimation].counter++;
} }
} }
// 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 // 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 // Establece el valor del contador

View File

@@ -38,7 +38,7 @@ public:
void animate(); void animate();
// Establece el frame actual de la animación // Establece el frame actual de la animación
void setCurrentFrame(std::string name, int num); void setCurrentFrame(int num);
// Establece el valor del contador // Establece el valor del contador
void setAnimationCounter(std::string name, int num); void setAnimationCounter(std::string name, int num);

View File

@@ -183,6 +183,8 @@ bool Director::setFileList()
asset->add("/media/player/player01.ani", data); asset->add("/media/player/player01.ani", data);
asset->add("/media/items/items.png", bitmap); asset->add("/media/items/items.png", bitmap);
asset->add("/media/music/jd.ogg", music);
return asset->check(); return asset->check();
} }

View File

@@ -22,6 +22,7 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input)
eventHandler = new SDL_Event(); eventHandler = new SDL_Event();
text = new Text(asset->get("smb2.png"), asset->get("smb2.txt"), renderer); text = new Text(asset->get("smb2.png"), asset->get("smb2.txt"), renderer);
debugText = new Text(asset->get("debug.png"), asset->get("debug.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 // Inicializa variables
ticks = 0; ticks = 0;
@@ -64,6 +65,8 @@ Game::~Game()
// Bucle para el juego // Bucle para el juego
section_t Game::run() section_t Game::run()
{ {
JA_PlayMusic(music);
while (section.name == SECTION_PROG_GAME) while (section.name == SECTION_PROG_GAME)
{ {
// Sección juego jugando // Sección juego jugando
@@ -74,6 +77,8 @@ section_t Game::run()
} }
} }
JA_StopMusic();
return section; return section;
} }
@@ -103,6 +108,10 @@ void Game::update()
debug = !debug; debug = !debug;
break; break;
case SDL_SCANCODE_M:
(JA_GetMusicState() == JA_MUSIC_PLAYING) ? JA_PauseMusic() : JA_ResumeMusic();
break;
case SDL_SCANCODE_F: case SDL_SCANCODE_F:
screen->switchVideoMode(); screen->switchVideoMode();
break; break;

View File

@@ -33,6 +33,7 @@ private:
Text *text; // Objeto para los textos del juego Text *text; // Objeto para los textos del juego
Text *debugText; // Objeto para los textos de debug del juego Text *debugText; // Objeto para los textos de debug del juego
ScoreBoard *scoreboard; // Objeto encargado de gestionar el marcador 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 ticks; // Contador de ticks para ajustar la velocidad del programa
int ticksSpeed; // Velocidad a la que se repiten los bucles del programa int ticksSpeed; // Velocidad a la que se repiten los bucles del programa
section_t section; // Seccion actual dentro del juego section_t section; // Seccion actual dentro del juego

View File

@@ -14,6 +14,7 @@ ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Asset *asset)
loadTextureFromFile(texture, asset->get("player01.png"), renderer); loadTextureFromFile(texture, asset->get("player01.png"), renderer);
sprite = new AnimatedSprite(texture, renderer, asset->get("player01.ani")); sprite = new AnimatedSprite(texture, renderer, asset->get("player01.ani"));
sprite->setCurrentAnimation("walk_menu"); sprite->setCurrentAnimation("walk_menu");
text = new Text(asset->get("smb2.png"), asset->get("smb2.txt"), renderer);
// Inicializa las variables // Inicializa las variables
counter = 0; counter = 0;
@@ -37,6 +38,30 @@ ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Asset *asset)
c = stringToColor("yellow"); c = stringToColor("yellow");
color.push_back(c); 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 // Destructor
@@ -48,6 +73,9 @@ ScoreBoard::~ScoreBoard()
delete sprite; delete sprite;
sprite = nullptr; sprite = nullptr;
delete text;
text = nullptr;
} }
// Pinta el objeto en pantalla // Pinta el objeto en pantalla
@@ -58,12 +86,15 @@ void ScoreBoard::render()
// Dibuja el fondo del marcador // Dibuja el fondo del marcador
const SDL_Rect rect = {0, 17 * BLOCK, PLAY_AREA_WIDTH, GAMECANVAS_HEIGHT - PLAY_AREA_HEIGHT}; 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); SDL_RenderFillRect(renderer, &rect);
// Dibuja las vidas
sprite->setPosY(18 * BLOCK); sprite->setPosY(18 * BLOCK);
int index; 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++) 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->getTexture()->setColor(color[index].r, color[index].g, color[index].b);
sprite->render(); 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 // Actualiza las variables del objeto

View File

@@ -2,6 +2,7 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "utils.h" #include "utils.h"
#include "text.h"
#include "asset.h" #include "asset.h"
#include "animatedsprite.h" #include "animatedsprite.h"
#include "const.h" #include "const.h"
@@ -18,6 +19,7 @@ private:
AnimatedSprite *sprite; // Sprite para mostrar las vidas en el marcador AnimatedSprite *sprite; // Sprite para mostrar las vidas en el marcador
SDL_Renderer *renderer; // El renderizador de la ventana SDL_Renderer *renderer; // El renderizador de la ventana
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
Text *text; // Objeto para escribir texto
std::vector<color_t> color; // Vector con los colores del objeto std::vector<color_t> color; // Vector con los colores del objeto
int counter; // Contador interno int counter; // Contador interno
int colorChangeSpeed; // Cuanto mas alto, mas tarda en cambiar de color int colorChangeSpeed; // Cuanto mas alto, mas tarda en cambiar de color