Files
jaildoctors_dilemma/source/game_over.cpp
2025-02-27 07:37:39 +01:00

190 lines
5.9 KiB
C++

#include "game_over.h"
#include <SDL2/SDL_events.h> // for SDL_PollEvent, SDL_Event
#include <SDL2/SDL_timer.h> // for SDL_GetTicks
#include <algorithm> // for min, max
#include <string> // 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");
player_sprite_ = std::make_shared<AnimatedSprite>(resource_->getTexture("player_game_over.png"), resource_->getAnimation("player_game_over.ani"));
tv_sprite_ = std::make_shared<AnimatedSprite>(resource_->getTexture("tv.png"), resource_->getAnimation("tv.ani"));
music_ = resource_->getMusic("game_over.ogg");
// Inicializa variables
pre_counter_ = 0;
counter_ = 0;
options.section.section = Section::GAME_OVER;
options.section.subsection = Subsection::NONE;
ticks_ = 0;
player_sprite_->setPosX(GAMECANVAS_CENTER_X + 10);
player_sprite_->setPosY(30);
tv_sprite_->setPosX(GAMECANVAS_CENTER_X - tv_sprite_->getWidth() - 10);
tv_sprite_->setPosY(30);
// Inicializa el vector de colores
const std::vector<std::string> 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_ > GAME_SPEED)
{
// 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
player_sprite_->update();
tv_sprite_->update();
screen_->update();
}
}
// Dibuja el final en pantalla
void GameOver::render()
{
constexpr int Y = 32;
screen_->start();
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
player_sprite_->setPosY(Y + 30);
tv_sprite_->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 = COUNTER_SECTION_END_ / 2;
if (counter_ < half)
{
const float step = std::min(counter_, COUNTER_FADE_LENGHT_) / (float)COUNTER_FADE_LENGHT_;
const int index = (colors_.size() - 1) - int((colors_.size() - 1) * step);
color_ = colors_[index];
}
else
{
const float step = std::min(std::max(counter_, COUNTER_INIT_FADE_) - COUNTER_INIT_FADE_, COUNTER_FADE_LENGHT_) / (float)COUNTER_FADE_LENGHT_;
const int index = (colors_.size() - 1) * step;
color_ = colors_[index];
}
}
// Dibuja los sprites
void GameOver::renderSprites()
{
player_sprite_->getTexture()->setColor(color_.r, color_.g, color_.b);
player_sprite_->render();
tv_sprite_->getTexture()->setColor(color_.r, color_.g, color_.b);
tv_sprite_->render();
}
// Actualiza los contadores
void GameOver::updateCounters()
{
// Actualiza el contador
if (pre_counter_ < 50)
{
pre_counter_++;
}
else
{
counter_++;
}
// Hace sonar la música
if (counter_ == 1)
{
JA_PlayMusic(music_, 0);
}
// Comprueba si ha terminado la sección
else if (counter_ == COUNTER_SECTION_END_)
{
options.section.section = Section::LOGO;
options.section.subsection = Subsection::LOGO_TO_TITLE;
}
}