forked from jaildesigner-jailgames/jaildoctors_dilemma
184 lines
5.7 KiB
C++
184 lines
5.7 KiB
C++
#include "scoreboard.h"
|
|
#include <SDL2/SDL_rect.h> // Para SDL_Rect
|
|
#include <SDL2/SDL_timer.h> // Para SDL_GetTicks
|
|
#include "animatedsprite.h" // Para AnimatedSprite
|
|
#include "const.h" // Para BLOCK, PLAY_AREA_HEIGHT, PLAY_AREA_WIDTH
|
|
#include "resource.h" // Para Resource
|
|
#include "text.h" // Para Text
|
|
#include "texture.h" // Para Texture
|
|
#include "options.h"
|
|
#include "screen.h"
|
|
#include "asset.h"
|
|
class Asset;
|
|
|
|
// Constructor
|
|
Scoreboard::Scoreboard(board_t *board)
|
|
: renderer(Screen::get()->getRenderer()),
|
|
resource(Resource::get()),
|
|
asset(Asset::get()),
|
|
board(board)
|
|
{
|
|
// Reserva memoria para los objetos
|
|
itemTexture = resource->getTexture("items.png");
|
|
const std::string playerANI = options.cheats.alternate_skin == Cheat::CheatState::ENABLED ? "player2.ani" : "player.ani";
|
|
sprite = new AnimatedSprite(renderer, resource->getAnimation(playerANI));
|
|
sprite->setCurrentAnimation("walk_menu");
|
|
text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer);
|
|
|
|
// Inicializa las variables
|
|
counter = 0;
|
|
colorChangeSpeed = 4;
|
|
paused = false;
|
|
timePaused = 0;
|
|
totalTimePaused = 0;
|
|
itemsColor = stringToColor(options.video.palette, "white");
|
|
|
|
// Inicializa el vector de colores
|
|
const std::vector<std::string> vColors = {"blue", "magenta", "green", "cyan", "yellow", "white", "bright_blue", "bright_magenta", "bright_green", "bright_cyan", "bright_yellow", "bright_white"};
|
|
for (auto v : vColors)
|
|
{
|
|
color.push_back(stringToColor(options.video.palette, v));
|
|
}
|
|
}
|
|
|
|
// Destructor
|
|
Scoreboard::~Scoreboard()
|
|
{
|
|
delete sprite;
|
|
delete text;
|
|
}
|
|
|
|
// Pinta el objeto en pantalla
|
|
void Scoreboard::render()
|
|
{
|
|
// Anclas
|
|
const int line1 = 19 * BLOCK;
|
|
const int line2 = line1 + (2 * BLOCK);
|
|
|
|
// Dibuja el fondo del marcador
|
|
const SDL_Rect rect = {0, 18 * BLOCK, PLAY_AREA_WIDTH, GAMECANVAS_HEIGHT - PLAY_AREA_HEIGHT};
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
|
SDL_RenderFillRect(renderer, &rect);
|
|
|
|
// Dibuja las vidas
|
|
const int desp = (counter / 40) % 8;
|
|
const int frame = desp % 4;
|
|
sprite->setCurrentFrame(frame);
|
|
sprite->setPosY(line2);
|
|
for (int i = 0; i < board->lives; ++i)
|
|
{
|
|
sprite->setPosX(8 + (16 * i) + desp);
|
|
const int index = i % color.size();
|
|
sprite->getTexture()->setColor(color[index].r, color[index].g, color[index].b);
|
|
sprite->render();
|
|
}
|
|
|
|
// Muestra si suena la música
|
|
if (board->music)
|
|
{
|
|
const Color c = board->color;
|
|
SDL_Rect clip = {0, 8, 8, 8};
|
|
itemTexture->setColor(c.r, c.g, c.b);
|
|
itemTexture->render(renderer, 20 * BLOCK, line2, &clip);
|
|
}
|
|
|
|
// Escribe los textos
|
|
const std::string timeTxt = std::to_string((clock.minutes % 100) / 10) + std::to_string(clock.minutes % 10) + clock.separator + std::to_string((clock.seconds % 60) / 10) + std::to_string(clock.seconds % 10);
|
|
const std::string itemsTxt = std::to_string(board->items / 100) + std::to_string((board->items % 100) / 10) + std::to_string(board->items % 10);
|
|
this->text->writeColored(BLOCK, line1, "Items collected ", board->color);
|
|
this->text->writeColored(17 * BLOCK, line1, itemsTxt, itemsColor);
|
|
this->text->writeColored(20 * BLOCK, line1, " Time ", board->color);
|
|
this->text->writeColored(26 * BLOCK, line1, timeTxt, stringToColor(options.video.palette, "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(options.video.palette, "white"));
|
|
this->text->writeColored(28 * BLOCK, line2, roomsTxt, stringToColor(options.video.palette, "white"));
|
|
}
|
|
|
|
// Actualiza las variables del objeto
|
|
void Scoreboard::update()
|
|
{
|
|
counter++;
|
|
sprite->update();
|
|
|
|
// Actualiza el color de la cantidad de items recogidos
|
|
updateItemsColor();
|
|
|
|
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 - totalTimePaused;
|
|
|
|
clock_t time;
|
|
time.hours = timeElapsed / 3600000;
|
|
time.minutes = timeElapsed / 60000;
|
|
time.seconds = timeElapsed / 1000;
|
|
time.separator = (timeElapsed % 1000 <= 500) ? ":" : " ";
|
|
|
|
return time;
|
|
}
|
|
|
|
// Recarga la textura
|
|
void Scoreboard::reLoadTexture()
|
|
{
|
|
sprite->getTexture()->reLoad();
|
|
// playerTexture->reLoad();
|
|
itemTexture->reLoad();
|
|
text->reLoadTexture();
|
|
}
|
|
|
|
// Recarga la paleta
|
|
void Scoreboard::reLoadPalette()
|
|
{
|
|
// Reinicia el vector de colores
|
|
const std::vector<std::string> vColors = {"blue", "magenta", "green", "cyan", "yellow", "white", "bright_blue", "bright_magenta", "bright_green", "bright_cyan", "bright_yellow", "bright_white"};
|
|
color.clear();
|
|
for (auto v : vColors)
|
|
{
|
|
color.push_back(stringToColor(options.video.palette, v));
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
// Actualiza el color de la cantidad de items recogidos
|
|
void Scoreboard::updateItemsColor()
|
|
{
|
|
if (!board->jail_is_open)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (counter % 20 < 10)
|
|
{
|
|
itemsColor = stringToColor(options.video.palette, "white");
|
|
}
|
|
else
|
|
{
|
|
itemsColor = stringToColor(options.video.palette, "magenta");
|
|
}
|
|
}
|
|
|
|
// Devuelve la cantidad de minutos de juego transcurridos
|
|
int Scoreboard::getMinutes()
|
|
{
|
|
return getTime().minutes;
|
|
} |