Files
jaildoctors_dilemma/source/scoreboard.cpp

141 lines
3.6 KiB
C++

#include "scoreboard.h"
#include <fstream>
#include <sstream>
// Constructor
ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Asset *asset, int *lives, int *items, Uint32 *clock)
{
// Obten punteros a objetos
this->asset = asset;
this->renderer = renderer;
this->lives = lives;
this->items = items;
this->clock = clock;
// Reserva memoria para los objetos
texture = new LTexture();
loadTextureFromFile(texture, asset->get("player01.png"), renderer);
sprite = new AnimatedSprite(texture, renderer, asset->get("player01.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("purple");
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("light_blue");
color.push_back(c);
c = stringToColor("light_red");
color.push_back(c);
c = stringToColor("light_purple");
color.push_back(c);
c = stringToColor("light_green");
color.push_back(c);
c = stringToColor("light_cyan");
color.push_back(c);
c = stringToColor("light_yellow");
color.push_back(c);
c = stringToColor("light_white");
color.push_back(c);
}
// Destructor
ScoreBoard::~ScoreBoard()
{
texture->unload();
delete texture;
texture = nullptr;
delete sprite;
sprite = nullptr;
delete text;
text = nullptr;
}
// Pinta el objeto en pantalla
void ScoreBoard::render()
{
const int num_lives = 9;
// Dibuja el fondo del marcador
const SDL_Rect rect = {0, 17 * 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(18 * BLOCK);
int index;
const int desp = (counter / 40) % 8;
const int frame = desp % 4;
sprite->setCurrentFrame(frame);
for (int i = 0; i < *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();
}
// Escribe los textos
const clock_t clock = getTime();
std::string itemsTxt = std::to_string(*items / 100) + std::to_string((*items % 100) / 10) + std::to_string(*items % 10);
std::string separator = " ";
if (clock.separator)
{
separator = ":";
}
std::string timeTxt = std::to_string((clock.minutes % 60) / 10) + std::to_string(clock.minutes % 10) + separator + std::to_string((clock.seconds % 60) / 10) + std::to_string(clock.seconds % 10);
std::string text = "Items collected " + itemsTxt + " Time " + timeTxt;
const color_t color = stringToColor("white");
this->text->writeColored(BLOCK, 21 * BLOCK, text, color);
}
// Actualiza las variables del objeto
void ScoreBoard::update()
{
counter++;
sprite->update();
}
// Obtiene el tiempo transcurrido de partida
ScoreBoard::clock_t ScoreBoard::getTime()
{
const Uint32 timeElapsed = SDL_GetTicks() - *clock;
clock_t time;
time.hours = timeElapsed / 3600000;
time.minutes = timeElapsed / 60000;
time.seconds = timeElapsed / 1000;
time.separator = (timeElapsed % 1000 <= 500);
return time;
}