diff --git a/source/game.cpp b/source/game.cpp index 4aad1d1..7a1f093 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -16,7 +16,7 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, D this->debug = debug; // **** - //this->debug->setEnabled(true); + // this->debug->setEnabled(true); currentRoom = "11.room"; spawnPoint = {2 * 8, 5 * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL}; @@ -48,6 +48,9 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, D board.rooms = 1; board.color = room->getBorderColor(); roomTracker->addRoom(currentRoom); + paused = false; + blackScreen = false; + blackScreenCounter = 0; player->setInvincible(debug->getEnabled()); board.music = !debug->getEnabled(); @@ -112,8 +115,20 @@ void Game::checkEventHandler() break; case SDL_SCANCODE_P: - player->pause(); - room->pause(); + if (paused) + { + player->resume(); + room->resume(); + scoreboard->resume(); + paused = false; + } + else + { + player->pause(); + room->pause(); + scoreboard->pause(); + paused = true; + } break; case SDL_SCANCODE_B: @@ -200,6 +215,7 @@ void Game::update() checkEndGame(); scoreboard->update(); updateDebugInfo(); + updateBlackScreen(); } } @@ -216,6 +232,7 @@ void Game::render() player->render(); renderRoomName(); scoreboard->render(); + renderBlackScreen(); // Debug info renderDebugInfo(); @@ -375,11 +392,14 @@ void Game::killPlayer() // Sonido JA_PlaySound(deathSound); - SDL_Delay(500); + blackScreen = true; // Crea la nueva habitación y el nuevo jugador room = new Room(asset->get(currentRoom), renderer, screen, asset, itemTracker, &board.items, debug); player = new Player(spawnPoint, asset->get("player.png"), asset->get("player.ani"), renderer, asset, input, room, debug); + + room->pause(); + player->pause(); } // Recarga todas las texturas @@ -389,4 +409,32 @@ void Game::reLoadTextures() room->reLoadTexture(); scoreboard->reLoadTexture(); text->reLoadTexture(); +} + +// Actualiza las variables relativas a la pantalla en negro +void Game::updateBlackScreen() +{ + if (blackScreen) + { + blackScreenCounter++; + if (blackScreenCounter > 50) + { + blackScreen = false; + blackScreenCounter = 0; + + player->resume(); + room->resume(); + + } + } +} + +// Dibuja la pantalla negra +void Game::renderBlackScreen() +{ + if (blackScreen) + { + screen->clean(); + screen->setBorderColor(stringToColor("black")); + } } \ No newline at end of file diff --git a/source/game.h b/source/game.h index 8a64a93..516d0f3 100644 --- a/source/game.h +++ b/source/game.h @@ -45,6 +45,9 @@ private: player_t spawnPoint; // Lugar de la habitación donde aparece el jugador JA_Sound deathSound; // Sonido a reproducir cuando muere el jugador board_t board; // Estructura con los datos del marcador + bool paused; // Indica si el juego se encuentra en pausa + bool blackScreen; // Indica si la pantalla está en negro. Se utiliza para la muerte del jugador + int blackScreenCounter; // Contador para temporizar la pantalla en negro Test *test; // Actualiza el juego, las variables, comprueba la entrada, etc. @@ -89,6 +92,12 @@ private: // Recarga todas las texturas void reLoadTextures(); + // Actualiza las variables relativas a la pantalla en negro + void updateBlackScreen(); + + // Dibuja la pantalla negra + void renderBlackScreen(); + public: // Constructor Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, Debug *debug); diff --git a/source/player.h b/source/player.h index 5ccd660..584594e 100644 --- a/source/player.h +++ b/source/player.h @@ -59,7 +59,7 @@ public: bool onBorder; // Indica si el jugador esta en uno de los cuatro bordes de la pantalla int border; // Indica en cual de los cuatro bordes se encuentra bool invincible; // Si es invencible, no puede morir - bool paused; // Indica si el jugador esta en modo pausa + bool paused; // Indica si el jugador esta en modo pausa SDL_Rect lastPosition; // Contiene la ultima posición del jugador, por si hay que deshacer algun movimiento int jumpIni; // Valor del eje Y en el que se inicia el salto float maxVY; // Velocidad máxima que puede alcanzar al desplazarse en vertical diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index 6a87d24..fdc9cf1 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -20,13 +20,16 @@ ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Asset *asset, board_t *board) // Inicializa las variables counter = 0; colorChangeSpeed = 4; + paused = false; + timePaused = 0; + totalTimePaused = 0; // Inicializa los colores color_t c = stringToColor("blue"); color.push_back(c); - //c = stringToColor("red"); - //color.push_back(c); + // c = stringToColor("red"); + // color.push_back(c); c = stringToColor("magenta"); color.push_back(c); @@ -46,8 +49,8 @@ ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Asset *asset, board_t *board) c = stringToColor("bright_blue"); color.push_back(c); - //c = stringToColor("bright_red"); - //color.push_back(c); + // c = stringToColor("bright_red"); + // color.push_back(c); c = stringToColor("bright_magenta"); color.push_back(c); @@ -117,7 +120,7 @@ void ScoreBoard::render() this->text->writeColored(17 * BLOCK, line1, itemsTxt, stringToColor("white")); this->text->writeColored(20 * BLOCK, line1, " Time ", board->color); this->text->writeColored(26 * BLOCK, line1, timeTxt, stringToColor("white")); - + const std::string roomsTxt = std::to_string(board->rooms / 100) + std::to_string((board->rooms % 100) / 10) + std::to_string(board->rooms % 10); this->text->writeColored(22 * BLOCK, line2, "Rooms", stringToColor("white")); this->text->writeColored(28 * BLOCK, line2, roomsTxt, stringToColor("white")); @@ -128,13 +131,17 @@ void ScoreBoard::update() { counter++; sprite->update(); - clock = getTime(); + + if (!paused) + {// Si está en pausa no se actualiza el reloj + clock = getTime(); + } } // Obtiene el tiempo transcurrido de partida ScoreBoard::clock_t ScoreBoard::getTime() { - const Uint32 timeElapsed = SDL_GetTicks() - board->iniClock; + const Uint32 timeElapsed = SDL_GetTicks() - board->iniClock - totalTimePaused; clock_t time; time.hours = timeElapsed / 3600000; @@ -151,4 +158,18 @@ void ScoreBoard::reLoadTexture() playerTexture->reLoad(); itemTexture->reLoad(); text->reLoadTexture(); +} + +// Pone el marcador en modo pausa +void ScoreBoard::pause() +{ + paused = true; + timePaused = SDL_GetTicks(); +} + +// Quita el modo pausa del marcador +void ScoreBoard::resume() +{ + paused = false; + totalTimePaused += SDL_GetTicks() - timePaused; } \ No newline at end of file diff --git a/source/scoreboard.h b/source/scoreboard.h index bf44333..09a47d2 100644 --- a/source/scoreboard.h +++ b/source/scoreboard.h @@ -13,10 +13,10 @@ struct board_t { - int items; // Lleva la cuenta de los objetos recogidos - int lives; // Lleva la cuenta de ls vidas restantes del jugador - int rooms; // Lleva la cuenta de las habitaciones visitadas - bool music; // Indica si ha de sonar la musica durante el juego + int items; // Lleva la cuenta de los objetos recogidos + int lives; // Lleva la cuenta de ls vidas restantes del jugador + int rooms; // Lleva la cuenta de las habitaciones visitadas + bool music; // Indica si ha de sonar la musica durante el juego color_t color; // Color para escribir el texto del marcador Uint32 iniClock; }; @@ -41,6 +41,9 @@ private: std::vector color; // Vector con los colores del objeto int counter; // Contador interno int colorChangeSpeed; // Cuanto mas alto, mas tarda en cambiar de color + bool paused; // Indica si el marcador esta en modo pausa + Uint32 timePaused; // Milisegundos que ha estado el marcador en pausa + Uint32 totalTimePaused; // Tiempo acumulado en pausa clock_t clock; // Contiene las horas, minutos y segundos transcurridos desde el inicio de la partida LTexture *itemTexture; // Textura con los graficos para las vidas board_t *board; // Contiene las variables a mostrar en el marcador @@ -63,6 +66,12 @@ public: // Recarga la textura void reLoadTexture(); + + // Pone el marcador en modo pausa + void pause(); + + // Quita el modo pausa del marcador + void resume(); }; #endif