#include "game_over.h" #include // for SDL_PollEvent, SDL_Event #include // for SDL_GetTicks #include // for min, max #include // for basic_string, operator+, to_string, cha... #include "animated_sprite.h" // for AnimatedSprite #include "asset.h" // for Asset #include "defines.h" // for GAMECANVAS_CENTER_X #include "global_events.h" // for check #include "global_inputs.h" // for check #include "input.h" // for Input #include "jail_audio.h" // for JA_PlayMusic #include "options.h" // for Options, options, OptionsStats, Section... #include "resource.h" // for Resource #include "screen.h" // for Screen #include "text.h" // for TEXT_CENTER, TEXT_COLOR, Text #include "texture.h" // for Texture // Constructor GameOver::GameOver() : screen(Screen::get()), renderer(Screen::get()->getRenderer()), resource(Resource::get()), asset(Asset::get()), input(Input::get()) { // Reserva memoria para los punteros a objetos text = resource->getText("smb2"); playerSprite = std::make_shared(resource->getTexture("player_game_over.png"), resource->getAnimation("player_game_over.ani")); tvSprite = std::make_shared(resource->getTexture("tv.png"), resource->getAnimation("tv.ani")); music = resource->getMusic("game_over.ogg"); // Inicializa variables preCounter = 0; counter = 0; options.section.section = Section::GAME_OVER; options.section.subsection = Subsection::NONE; ticks = 0; ticksSpeed = 15; endSection = 400; iniFade = 310; fadeLenght = 20; playerSprite->setPosX(GAMECANVAS_CENTER_X + 10); playerSprite->setPosY(30); tvSprite->setPosX(GAMECANVAS_CENTER_X - tvSprite->getWidth() - 10); tvSprite->setPosY(30); // Inicializa el vector de colores const std::vector colorList = {"white", "yellow", "cyan", "green", "magenta", "red", "blue", "black"}; for (auto cl : colorList) { colors.push_back(stringToColor(options.video.palette, cl)); } color = colors.back(); } // Actualiza el objeto void GameOver::update() { // Comprueba que la diferencia de ticks sea mayor a la velocidad del juego if (SDL_GetTicks() - ticks > ticksSpeed) { // Actualiza el contador de ticks ticks = SDL_GetTicks(); // Comprueba las entradas checkInput(); // Actualiza el color usado para renderizar los textos e imagenes updateColor(); // Actualiza los contadores updateCounters(); // Actualiza los dos sprites playerSprite->update(); tvSprite->update(); screen->update(); } } // Dibuja el final en pantalla void GameOver::render() { const int y = 32; // Prepara para empezar a dibujar en la textura de juego screen->start(); // Limpia la pantalla screen->clean(); // Escribe el texto de GAME OVER text->writeDX(TEXT_CENTER | TEXT_COLOR, GAMECANVAS_CENTER_X, y, "G A M E O V E R", 1, color); // Dibuja los sprites playerSprite->setPosY(y + 30); tvSprite->setPosY(y + 30); renderSprites(); // Escribe el texto con las habitaciones y los items const std::string itemsTxt = std::to_string(options.stats.items / 100) + std::to_string((options.stats.items % 100) / 10) + std::to_string(options.stats.items % 10); const std::string roomsTxt = std::to_string(options.stats.rooms / 100) + std::to_string((options.stats.rooms % 100) / 10) + std::to_string(options.stats.rooms % 10); text->writeDX(TEXT_CENTER | TEXT_COLOR, GAMECANVAS_CENTER_X, y + 80, "ITEMS: " + itemsTxt, 1, color); text->writeDX(TEXT_CENTER | TEXT_COLOR, GAMECANVAS_CENTER_X, y + 90, "ROOMS: " + roomsTxt, 1, color); // Escribe el texto con "Tu peor pesadilla" text->writeDX(TEXT_CENTER | TEXT_COLOR, GAMECANVAS_CENTER_X, y + 110, "YOUR WORST NIGHTMARE IS", 1, color); text->writeDX(TEXT_CENTER | TEXT_COLOR, GAMECANVAS_CENTER_X, y + 120, options.stats.worst_nightmare, 1, color); // Vuelca el contenido del renderizador en pantalla screen->render(); } // Comprueba el manejador de eventos void GameOver::checkEvents() { SDL_Event event; while (SDL_PollEvent(&event)) { globalEvents::check(event); } } // Comprueba las entradas void GameOver::checkInput() { globalInputs::check(); } // Bucle principal void GameOver::run() { while (options.section.section == Section::GAME_OVER) { update(); checkEvents(); render(); } } // Actualiza el color usado para renderizar los textos e imagenes void GameOver::updateColor() { const int half = endSection / 2; if (counter < half) { const float step = std::min(counter, fadeLenght) / (float)fadeLenght; const int index = (colors.size() - 1) - int((colors.size() - 1) * step); color = colors[index]; } else { const float step = std::min(std::max(counter, iniFade) - iniFade, fadeLenght) / (float)fadeLenght; const int index = (colors.size() - 1) * step; color = colors[index]; } } // Dibuja los sprites void GameOver::renderSprites() { playerSprite->getTexture()->setColor(color.r, color.g, color.b); playerSprite->render(); tvSprite->getTexture()->setColor(color.r, color.g, color.b); tvSprite->render(); } // Actualiza los contadores void GameOver::updateCounters() { // Actualiza el contador if (preCounter < 50) { preCounter++; } else { counter++; } // Hace sonar la música if (counter == 1) { JA_PlayMusic(music, 0); } // Comprueba si ha terminado la sección else if (counter == endSection) { options.section.section = Section::LOGO; options.section.subsection = Subsection::LOGO_TO_TITLE; } }