Singleton de ItemTracker

Arreglos menors
This commit is contained in:
2025-02-26 13:07:41 +01:00
parent 2457517f2b
commit 85ab5ea03f
13 changed files with 466 additions and 442 deletions

View File

@@ -1,43 +1,43 @@
#include "scoreboard.h"
#include <SDL2/SDL_rect.h> // for SDL_Rect
#include <SDL2/SDL_timer.h> // for SDL_GetTicks
#include "animated_sprite.h" // for AnimatedSprite
#include "asset.h" // for Asset
#include "defines.h" // for BLOCK, GAMECANVAS_HEIGHT, PLAY_AREA_HEIGHT
#include "options.h" // for Options, options, OptionsVideo, Cheat
#include "resource.h" // for Resource
#include "screen.h" // for Screen
#include "text.h" // for Text
#include "texture.h" // for Texture
#include <SDL2/SDL_rect.h> // for SDL_Rect
#include <SDL2/SDL_timer.h> // for SDL_GetTicks
#include "animated_sprite.h" // for AnimatedSprite
#include "asset.h" // for Asset
#include "defines.h" // for BLOCK, GAMECANVAS_HEIGHT, PLAY_AREA_HEIGHT
#include "options.h" // for Options, options, OptionsVideo, Cheat
#include "resource.h" // for Resource
#include "screen.h" // for Screen
#include "text.h" // for Text
#include "texture.h" // for Texture
// Constructor
Scoreboard::Scoreboard(board_t *board)
: renderer(Screen::get()->getRenderer()),
resource(Resource::get()),
asset(Asset::get()),
board(board)
Scoreboard::Scoreboard(ScoreboardData *board)
: renderer_(Screen::get()->getRenderer()),
resource_(Resource::get()),
asset_(Asset::get()),
board_(board)
{
// Reserva memoria para los objetos
itemTexture = resource->getTexture("items.png");
item_texture_ = resource_->getTexture("items.png");
const std::string playerPNG = options.cheats.alternate_skin == Cheat::CheatState::ENABLED ? "player2.png" : "player.png";
const std::string playerANI = options.cheats.alternate_skin == Cheat::CheatState::ENABLED ? "player2.ani" : "player.ani";
sprite = std::make_shared<AnimatedSprite>(resource->getTexture(playerPNG), resource->getAnimation(playerANI));
sprite->setCurrentAnimation("walk_menu");
text = resource->getText("smb2");
player_sprite_ = std::make_shared<AnimatedSprite>(resource_->getTexture(playerPNG), resource_->getAnimation(playerANI));
player_sprite_->setCurrentAnimation("walk_menu");
text_ = resource_->getText("smb2");
// Inicializa las variables
counter = 0;
colorChangeSpeed = 4;
paused = false;
timePaused = 0;
totalTimePaused = 0;
itemsColor = stringToColor(options.video.palette, "white");
counter_ = 0;
change_color_speed_ = 4;
is_paused_ = false;
paused_time_ = 0;
paused_time_elapsed_ = 0;
items_color_ = 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));
color_.push_back(stringToColor(options.video.palette, v));
}
}
@@ -50,66 +50,66 @@ void Scoreboard::render()
// 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);
SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255);
SDL_RenderFillRect(renderer_, &rect);
// Dibuja las vidas
const int desp = (counter / 40) % 8;
const int desp = (counter_ / 40) % 8;
const int frame = desp % 4;
sprite->setCurrentAnimationFrame(frame);
sprite->setPosY(line2);
for (int i = 0; i < board->lives; ++i)
player_sprite_->setCurrentAnimationFrame(frame);
player_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();
player_sprite_->setPosX(8 + (16 * i) + desp);
const int index = i % color_.size();
player_sprite_->getTexture()->setColor(color_[index].r, color_[index].g, color_[index].b);
player_sprite_->render();
}
// Muestra si suena la música
if (board->music)
if (board_->music)
{
const Color c = board->color;
const Color c = board_->color;
SDL_Rect clip = {0, 8, 8, 8};
itemTexture->setColor(c.r, c.g, c.b);
itemTexture->render(20 * BLOCK, line2, &clip);
item_texture_->setColor(c.r, c.g, c.b);
item_texture_->render(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 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, items_color_);
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"));
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();
counter_++;
player_sprite_->update();
// Actualiza el color de la cantidad de items recogidos
updateItemsColor();
if (!paused)
if (!is_paused_)
{
// Si está en pausa no se actualiza el reloj
clock = getTime();
clock_ = getTime();
}
}
// Obtiene el tiempo transcurrido de partida
Scoreboard::clock_t Scoreboard::getTime()
Scoreboard::ClockData Scoreboard::getTime()
{
const Uint32 timeElapsed = SDL_GetTicks() - board->iniClock - totalTimePaused;
const Uint32 timeElapsed = SDL_GetTicks() - board_->ini_clock - paused_time_elapsed_;
clock_t time;
ClockData time;
time.hours = timeElapsed / 3600000;
time.minutes = timeElapsed / 60000;
time.seconds = timeElapsed / 1000;
@@ -121,10 +121,10 @@ Scoreboard::clock_t Scoreboard::getTime()
// Recarga la textura
void Scoreboard::reLoadTexture()
{
sprite->getTexture()->reLoad();
player_sprite_->getTexture()->reLoad();
// playerTexture->reLoad();
itemTexture->reLoad();
text->reLoadTexture();
item_texture_->reLoad();
text_->reLoadTexture();
}
// Recarga la paleta
@@ -132,42 +132,42 @@ 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();
color_.clear();
for (auto v : vColors)
{
color.push_back(stringToColor(options.video.palette, v));
color_.push_back(stringToColor(options.video.palette, v));
}
}
// Pone el marcador en modo pausa
void Scoreboard::pause()
{
paused = true;
timePaused = SDL_GetTicks();
is_paused_ = true;
paused_time_ = SDL_GetTicks();
}
// Quita el modo pausa del marcador
void Scoreboard::resume()
{
paused = false;
totalTimePaused += SDL_GetTicks() - timePaused;
is_paused_ = false;
paused_time_elapsed_ += SDL_GetTicks() - paused_time_;
}
// Actualiza el color de la cantidad de items recogidos
void Scoreboard::updateItemsColor()
{
if (!board->jail_is_open)
if (!board_->jail_is_open)
{
return;
}
if (counter % 20 < 10)
if (counter_ % 20 < 10)
{
itemsColor = stringToColor(options.video.palette, "white");
items_color_ = stringToColor(options.video.palette, "white");
}
else
{
itemsColor = stringToColor(options.video.palette, "magenta");
items_color_ = stringToColor(options.video.palette, "magenta");
}
}