From 1632441c31608296be947f9844e1e7cdb6cb2f83 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 26 Jul 2024 11:48:44 +0200 Subject: [PATCH] afegit el modo demo al marcador --- source/game.cpp | 9 ++++- source/scoreboard.cpp | 78 +++++++++++++++++++++++++++++++++++-------- source/scoreboard.h | 61 +++++++++++++++++++++------------ 3 files changed, 112 insertions(+), 36 deletions(-) diff --git a/source/game.cpp b/source/game.cpp index df4c40f..83f0dfd 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -288,7 +288,7 @@ void Game::init(int playerID) powerBallCounter = 0; coffeeMachineEnabled = false; - // Inicializa las variables para el modo demo + // Inicializa las variables para el modo DEMO if (demo.enabled) { // Selecciona una pantalla al azar @@ -325,6 +325,10 @@ void Game::init(int playerID) // Deshabilita los sonidos JA_EnableSound(false); + + // Configura los marcadores + scoreboard->setMode(SCOREBOARD_LEFT_SIDE, SCOREBOARD_MODE_DEMO); + scoreboard->setMode(SCOREBOARD_RIGHT_SIDE, SCOREBOARD_MODE_DEMO); } initPaths(); @@ -3417,6 +3421,9 @@ void Game::updateScoreboard() scoreboard->setPower((float)stage[currentStage].currentPower / (float)stage[currentStage].powerToComplete); scoreboard->setHiScore(hiScore); scoreboard->setHiScoreName(hiScoreName); + + // Lógica del marcador + scoreboard->update(); } // Pausa el juego diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index 33f2074..e0da421 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -21,6 +21,10 @@ Scoreboard::Scoreboard(SDL_Renderer *renderer, Screen *screen, Asset *asset, Lan hiScoreName = ""; color = {0, 0, 0}; rect = {0, 0, 320, 40}; + mode[0] = SCOREBOARD_MODE_PLAYING; + mode[1] = SCOREBOARD_MODE_PLAYING; + ticks = SDL_GetTicks(); + counter = 0; // Recalcula las anclas de los elementos recalculateAnchors(); @@ -88,10 +92,26 @@ std::string Scoreboard::updateScoreText(Uint32 num) return (std::to_string(num)); } +// Actualiza el contador +void Scoreboard::updateCounter() +{ + if (SDL_GetTicks() - ticks > SCOREBOARD_TICK_SPEED) + { + ticks = SDL_GetTicks(); + counter++; + } +} + +// Actualiza la lógica del marcador +void Scoreboard::update() +{ + fillBackgroundTexture(); + updateCounter(); +} + // Pinta el marcador void Scoreboard::render() { - fillBackgroundTexture(); SDL_RenderCopy(renderer, background, nullptr, &rect); } @@ -182,21 +202,47 @@ void Scoreboard::fillBackgroundTexture() SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 255); SDL_RenderFillRect(renderer, nullptr); - // PLAYER1 - SCORE - textScoreBoard->writeCentered(offsetScoreP1Label.x, offsetScoreP1Label.y, lang->getText(53)); - textScoreBoard->writeCentered(offsetScoreP1.x, offsetScoreP1.y, updateScoreText(score1)); + // PARTE IZQUIERDA + if (mode[SCOREBOARD_LEFT_SIDE] == SCOREBOARD_MODE_PLAYING) + { // PLAYER1 - SCORE + textScoreBoard->writeCentered(offsetScoreP1Label.x, offsetScoreP1Label.y, lang->getText(53)); + textScoreBoard->writeCentered(offsetScoreP1.x, offsetScoreP1.y, updateScoreText(score1)); - // PLAYER1 - MULT - textScoreBoard->writeCentered(offsetMultP1Label.x, offsetMultP1Label.y, lang->getText(55)); - textScoreBoard->writeCentered(offsetMultP1.x, offsetMultP1.y, std::to_string(mult1).substr(0, 3)); + // PLAYER1 - MULT + textScoreBoard->writeCentered(offsetMultP1Label.x, offsetMultP1Label.y, lang->getText(55)); + textScoreBoard->writeCentered(offsetMultP1.x, offsetMultP1.y, std::to_string(mult1).substr(0, 3)); + } - // PLAYER2 - SCORE - textScoreBoard->writeCentered(offsetScoreP2Label.x, offsetScoreP2Label.y, lang->getText(54)); - textScoreBoard->writeCentered(offsetScoreP2.x, offsetScoreP2.y, updateScoreText(score2)); + else if (mode[SCOREBOARD_LEFT_SIDE] == SCOREBOARD_MODE_DEMO) + { + if (counter % 2 == 0) + { + textScoreBoard->writeCentered(offsetScoreP1.x, offsetScoreP1.y, "Mode"); + textScoreBoard->writeCentered(offsetMultP1Label.x, offsetMultP1Label.y, "Demo"); + } + } - // PLAYER2 - MULT - textScoreBoard->writeCentered(offsetMultP2Label.x, offsetMultP2Label.y, lang->getText(55)); - textScoreBoard->writeCentered(offsetMultP2.x, offsetMultP2.y, std::to_string(mult2).substr(0, 3)); + // PARTE DERECHA + if (mode[SCOREBOARD_RIGHT_SIDE] == SCOREBOARD_MODE_PLAYING) + { // PLAYER2 - SCORE + textScoreBoard->writeCentered(offsetScoreP2Label.x, offsetScoreP2Label.y, lang->getText(54)); + textScoreBoard->writeCentered(offsetScoreP2.x, offsetScoreP2.y, updateScoreText(score2)); + + // PLAYER2 - MULT + textScoreBoard->writeCentered(offsetMultP2Label.x, offsetMultP2Label.y, lang->getText(55)); + textScoreBoard->writeCentered(offsetMultP2.x, offsetMultP2.y, std::to_string(mult2).substr(0, 3)); + } + + else if (mode[SCOREBOARD_RIGHT_SIDE] == SCOREBOARD_MODE_DEMO) + { + if (counter % 2 == 0) + { + textScoreBoard->writeCentered(offsetScoreP2.x, offsetScoreP2.y, "Mode"); + textScoreBoard->writeCentered(offsetMultP2Label.x, offsetMultP2Label.y, "Demo"); + } + } + + // PARTE CENTRAL // STAGE textScoreBoard->writeCentered(offsetStage.x, offsetStage.y, lang->getText(57) + std::to_string(stage)); @@ -250,4 +296,10 @@ void Scoreboard::recalculateAnchors() offsetMultP1 = {col1, row4}; offsetHiScore = {col2, row4}; offsetMultP2 = {col3, row4}; +} + +// Establece el modo del marcador +void Scoreboard::setMode(int index, scoreboard_modes_e mode) +{ + this->mode[index] = mode; } \ No newline at end of file diff --git a/source/scoreboard.h b/source/scoreboard.h index 3ca8695..d794ee7 100644 --- a/source/scoreboard.h +++ b/source/scoreboard.h @@ -11,11 +11,16 @@ enum scoreboard_modes_e { - scoreboard_mode_playing, - scoreboard_mode_game_over, - scoreboard_mode_demo, + SCOREBOARD_MODE_PLAYING, + SCOREBOARD_MODE_GAME_OVER, + SCOREBOARD_MODE_DEMO, }; +#define SCOREBOARD_LEFT_SIDE 0 +#define SCOREBOARD_RIGHT_SIDE 1 + +#define SCOREBOARD_TICK_SPEED 1000 + // Clase Scoreboard class Scoreboard { @@ -31,17 +36,20 @@ private: SDL_Texture *background; // Textura para dibujar el marcador // Variables - struct options_t *options; // Variable con todas las variables de las opciones del programa - int stage; // Número de fase actual - int score1; // Puntuación del jugador 1 - int score2; // Puntuación del jugador 2 - float mult1; // Multiplicador del jugador 1 - float mult2; // MUltiplicador del jugador 2 - int hiScore; // Mäxima puntuación - float power; // Poder actual de la fase - std::string hiScoreName; // Nombre del jugador con la máxima puntuación - color_t color; // Color del marcador - SDL_Rect rect; // Posición y dimensiones del marcador + struct options_t *options; // Variable con todas las variables de las opciones del programa + int stage; // Número de fase actual + int score1; // Puntuación del jugador 1 + int score2; // Puntuación del jugador 2 + float mult1; // Multiplicador del jugador 1 + float mult2; // MUltiplicador del jugador 2 + int hiScore; // Mäxima puntuación + float power; // Poder actual de la fase + std::string hiScoreName; // Nombre del jugador con la máxima puntuación + color_t color; // Color del marcador + SDL_Rect rect; // Posición y dimensiones del marcador + scoreboard_modes_e mode[2]; // Modos en los que se puede encontrar el marcador + Uint32 ticks; // Variable donde almacenar el valor de SDL_GetTiks() + int counter; // Contador SDL_Point offsetScoreP1Label; SDL_Point offsetScoreP1; @@ -68,6 +76,9 @@ private: // Rellena la textura de fondo void fillBackgroundTexture(); + // Actualiza el contador + void updateCounter(); + public: // Constructor Scoreboard(SDL_Renderer *renderer, Screen *screen, Asset *asset, Lang *lang, options_t *options); @@ -75,6 +86,9 @@ public: // Destructor ~Scoreboard(); + // Actualiza la lógica del marcador + void update(); + // Pinta el marcador void render(); @@ -83,28 +97,31 @@ public: // Establece el valor de la variable void setScore2(int score); - + // Establece el valor de la variable void setMult1(float mult); - + // Establece el valor de la variable void setMult2(float mult); - + // Establece el valor de la variable void setStage(int stage); - + // Establece el valor de la variable void setHiScore(int hiScore); - + // Establece el valor de la variable void setPower(float power); - + // Establece el valor de la variable void setHiScoreName(std::string name); - + // Establece el valor de la variable void setColor(color_t color); - + // Establece el valor de la variable void setPos(SDL_Rect rect); + + // Establece el modo del marcador + void setMode(int index, scoreboard_modes_e mode); };