#include "game_over.h" #include // Para SDL_GetTicks #include // Para min, max #include // Para basic_string, operator+, to_string, char... #include "animatedsprite.h" // Para AnimatedSprite #include "asset.h" // Para Asset #include "const.h" // Para GAMECANVAS_CENTER_X, SECTION_GAME_OVER #include "input.h" // Para Input, REPEAT_FALSE, inputs_e #include "jail_audio.h" // Para JA_DeleteMusic, JA_LoadMusic, JA_PlayMusic #include "resource.h" // Para Resource #include "screen.h" // Para Screen #include "text.h" // Para Text, TXT_CENTER, TXT_COLOR #include "texture.h" // Para Texture #include "options.h" // 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 eventHandler = new SDL_Event(); text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer); playerSprite = new AnimatedSprite(renderer, resource->getAnimation("player_game_over.ani")); tvSprite = new AnimatedSprite(renderer, resource->getAnimation("tv.ani")); music = JA_LoadMusic(asset->get("game_over.ogg").c_str()); // Inicializa variables preCounter = 0; counter = 0; options.section.name = SECTION_GAME_OVER; options.section.subsection = 0; 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->getAnimationClip(0, 0).w - 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.palette, cl)); } color = colors.back(); } // Destructor GameOver::~GameOver() { // Libera la memoria de los objetos delete eventHandler; delete text; delete playerSprite; delete tvSprite; JA_DeleteMusic(music); } // 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(TXT_CENTER | TXT_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(TXT_CENTER | TXT_COLOR, GAMECANVAS_CENTER_X, y + 80, "ITEMS: " + itemsTxt, 1, color); text->writeDX(TXT_CENTER | TXT_COLOR, GAMECANVAS_CENTER_X, y + 90, "ROOMS: " + roomsTxt, 1, color); // Escribe el texto con "Tu peor pesadilla" text->writeDX(TXT_CENTER | TXT_COLOR, GAMECANVAS_CENTER_X, y + 110, "YOUR WORST NIGHTMARE IS", 1, color); text->writeDX(TXT_CENTER | TXT_COLOR, GAMECANVAS_CENTER_X, y + 120, options.stats.worstNightmare, 1, color); // Vuelca el contenido del renderizador en pantalla screen->render(); } // Comprueba el manejador de eventos void GameOver::checkEvents() { // Comprueba los eventos que hay en la cola while (SDL_PollEvent(eventHandler) != 0) { // Evento de salida de la aplicación if (eventHandler->type == SDL_QUIT) { options.section.name = SECTION_QUIT; options.section.subsection = 0; break; } } } // Comprueba las entradas void GameOver::checkInput() { if (input->checkInput(input_exit, REPEAT_FALSE)) { options.section.name = SECTION_QUIT; } else if (input->checkInput(input_toggle_border, REPEAT_FALSE)) { screen->toggleBorder(); } else if (input->checkInput(input_toggle_videomode, REPEAT_FALSE)) { screen->toggleVideoMode(); } else if (input->checkInput(input_window_dec_size, REPEAT_FALSE)) { screen->decWindowSize(); } else if (input->checkInput(input_window_inc_size, REPEAT_FALSE)) { screen->incWindowSize(); } else if (input->checkInput(input_toggle_palette, REPEAT_FALSE)) { switchPalette(); } } // Bucle principal void GameOver::run() { while (options.section.name == 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.name = SECTION_LOGO; options.section.subsection = SUBSECTION_LOGO_TO_TITLE; } } // Cambia la paleta void GameOver::switchPalette() { options.palette = (options.palette == p_zxspectrum) ? p_zxarne : p_zxspectrum; }