Files
jaildoctors_dilemma/source/scoreboard.cpp

154 lines
4.4 KiB
C++

#include "scoreboard.h"
#include <fstream>
#include <sstream>
// Constructor
ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Asset *asset, board_t *board)
{
// Obten punteros a objetos
this->asset = asset;
this->renderer = renderer;
this->board = board;
// Reserva memoria para los objetos
playerTexture = new LTexture(renderer, asset->get("player.png"));
itemTexture = new LTexture(renderer, asset->get("items.png"));
sprite = new AnimatedSprite(playerTexture, renderer, asset->get("player.ani"));
sprite->setCurrentAnimation("walk_menu");
text = new Text(asset->get("smb2.png"), asset->get("smb2.txt"), renderer);
// Inicializa las variables
counter = 0;
colorChangeSpeed = 4;
// Inicializa los colores
color_t c = stringToColor("blue");
color.push_back(c);
//c = stringToColor("red");
//color.push_back(c);
c = stringToColor("magenta");
color.push_back(c);
c = stringToColor("green");
color.push_back(c);
c = stringToColor("cyan");
color.push_back(c);
c = stringToColor("yellow");
color.push_back(c);
c = stringToColor("white");
color.push_back(c);
c = stringToColor("bright_blue");
color.push_back(c);
//c = stringToColor("bright_red");
//color.push_back(c);
c = stringToColor("bright_magenta");
color.push_back(c);
c = stringToColor("bright_green");
color.push_back(c);
c = stringToColor("bright_cyan");
color.push_back(c);
c = stringToColor("bright_yellow");
color.push_back(c);
c = stringToColor("bright_white");
color.push_back(c);
}
// Destructor
ScoreBoard::~ScoreBoard()
{
delete playerTexture;
delete itemTexture;
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
sprite->setPosY(line2);
int index;
const int desp = (counter / 40) % 8;
const int frame = desp % 4;
sprite->setCurrentFrame(frame);
for (int i = 0; i < board->lives; i++)
{
sprite->setPosX(8 + (16 * i) + desp);
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_t c = stringToColor("bright_blue");
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 % 60) / 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 ", stringToColor("yellow"));
this->text->writeColored(17 * BLOCK, line1, itemsTxt, stringToColor("bright_blue"));
this->text->writeColored(20 * BLOCK, line1, " Time ", stringToColor("yellow"));
this->text->writeColored(26 * BLOCK, line1, timeTxt, stringToColor("bright_blue"));
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("yellow"));
this->text->writeColored(28 * BLOCK, line2, roomsTxt, stringToColor("bright_blue"));
}
// Actualiza las variables del objeto
void ScoreBoard::update()
{
counter++;
sprite->update();
clock = getTime();
}
// Obtiene el tiempo transcurrido de partida
ScoreBoard::clock_t ScoreBoard::getTime()
{
const Uint32 timeElapsed = SDL_GetTicks() - board->iniClock;
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()
{
playerTexture->reLoad();
itemTexture->reLoad();
text->reLoadTexture();
}