Files
jaildoctors_dilemma/source/game_over.cpp

241 lines
6.4 KiB
C++

#include "game_over.h"
// Constructor
GameOver::GameOver(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *asset, Input *input, options_t *options, section_t *section)
{
// Copia los punteros
this->renderer = renderer;
this->screen = screen;
this->resource = resource;
this->asset = asset;
this->input = input;
this->options = options;
this->section = section;
// 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;
section->name = SECTION_PROG_GAME_OVER;
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 vector<string> 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 el manejador de eventos
checkEventHandler();
// 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();
// Actualiza las notificaciones
screen->updateNotifier();
}
}
// 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 string itemsTxt = to_string(options->stats.items / 100) + to_string((options->stats.items % 100) / 10) + to_string(options->stats.items % 10);
const string roomsTxt = to_string(options->stats.rooms / 100) + to_string((options->stats.rooms % 100) / 10) + 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->blit();
}
// Comprueba el manejador de eventos
void GameOver::checkEventHandler()
{
// 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)
{
section->name = SECTION_PROG_QUIT;
section->subsection = 0;
break;
}
}
}
// Comprueba las entradas
void GameOver::checkInput()
{
if (input->checkInput(input_exit, REPEAT_FALSE))
{
section->name = SECTION_PROG_QUIT;
}
else if (input->checkInput(input_toggle_border, REPEAT_FALSE))
{
screen->switchBorder();
}
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
{
screen->switchVideoMode();
}
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_swap_palette, REPEAT_FALSE))
{
switchPalette();
}
}
// Bucle principal
void GameOver::run()
{
while (section->name == SECTION_PROG_GAME_OVER)
{
update();
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 = min(counter, fadeLenght) / (float)fadeLenght;
const int index = (colors.size() - 1) - int((colors.size() - 1) * step);
color = colors[index];
}
else
{
const float step = min(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)
{
section->name = SECTION_PROG_LOGO;
section->subsection = SUBSECTION_LOGO_TO_TITLE;
}
}
// Cambia la paleta
void GameOver::switchPalette()
{
options->palette = (options->palette == p_zxspectrum) ? p_zxarne : p_zxspectrum;
}