La tabla de puntuació ja mostra amb altre color la puntuació que s'acaba d'afegir

fix: la tabla de punts no guardava a disc el estat de 1CC de cada entrada
This commit is contained in:
2025-02-05 22:52:19 +01:00
parent 6f594b9a1f
commit 5755947ff7
11 changed files with 175 additions and 66 deletions

View File

@@ -1370,11 +1370,11 @@ void Game::pause(bool value)
}
// Añade una puntuación a la tabla de records
void Game::addScoreToScoreBoard(const std::string &name, int score, bool one_credit_continue)
void Game::addScoreToScoreBoard(const std::shared_ptr<Player> &player)
{
const auto entry = HiScoreEntry(trim(name), score);
const auto entry = HiScoreEntry(trim(player->getRecordName()), player->getScore(), player->get1CC());
auto manager = std::make_unique<ManageHiScoreTable>(options.game.hi_score_table);
manager->add(entry);
options.game.last_hi_score_entry.at(player->getId() - 1) = manager->add(entry);
manager->saveToFile(asset_->get("score.bin"));
hi_score_.name = options.game.hi_score_table.front().name;
}
@@ -1627,6 +1627,7 @@ void Game::handlePlayerContinue(const std::shared_ptr<Player> &player)
if (input_->checkInput(InputType::START, INPUT_DO_NOT_ALLOW_REPEAT, options.controllers[controllerIndex].type, options.controllers[controllerIndex].index))
{
player->setPlayingState(PlayerState::PLAYING);
player->addCredit();
}
// Disminuye el contador de continuación si se presiona cualquier botón de disparo.
@@ -1634,7 +1635,10 @@ void Game::handlePlayerContinue(const std::shared_ptr<Player> &player)
input_->checkInput(InputType::FIRE_CENTER, INPUT_DO_NOT_ALLOW_REPEAT, options.controllers[controllerIndex].type, options.controllers[controllerIndex].index) ||
input_->checkInput(InputType::FIRE_RIGHT, INPUT_DO_NOT_ALLOW_REPEAT, options.controllers[controllerIndex].type, options.controllers[controllerIndex].index))
{
player->decContinueCounter();
if (player->getContinueCounter() < 8)
{
player->decContinueCounter();
}
}
}
@@ -1649,7 +1653,7 @@ void Game::handleNameInput(const std::shared_ptr<Player> &player)
if (player->getRecordNamePos() == NAME_LENGHT - 1)
{
player->setInput(InputType::START);
addScoreToScoreBoard(player->getRecordName(), player->getScore(), player->get1CC());
addScoreToScoreBoard(player);
const auto state = player->getPlayingState();
player->setPlayingState(state == PlayerState::ENTERING_NAME ? PlayerState::CONTINUE : PlayerState::LEAVING_SCREEN);
}
@@ -1673,7 +1677,7 @@ void Game::handleNameInput(const std::shared_ptr<Player> &player)
else if (input_->checkInput(InputType::START, INPUT_DO_NOT_ALLOW_REPEAT, options.controllers[controllerIndex].type, options.controllers[controllerIndex].index))
{
player->setInput(InputType::START);
addScoreToScoreBoard(player->getRecordName(), player->getScore(), player->get1CC());
addScoreToScoreBoard(player);
const auto state = player->getPlayingState();
player->setPlayingState(state == PlayerState::ENTERING_NAME ? PlayerState::CONTINUE : PlayerState::LEAVING_SCREEN);
}

View File

@@ -311,7 +311,7 @@ private:
void pause(bool value);
// Añade una puntuación a la tabla de records
void addScoreToScoreBoard(const std::string &name, int score, bool one_credit_continue);
void addScoreToScoreBoard(const std::shared_ptr<Player> &player);
// Saca del estado de GAME OVER al jugador si el otro está activo
void checkAndUpdatePlayerStatus(int active_player_index, int inactive_player_index);

View File

@@ -44,6 +44,7 @@ HiScoreTable::HiScoreTable()
SDL_SetTextureBlendMode(backbuffer_, SDL_BLENDMODE_BLEND);
initFade();
initBackground();
iniEntryColors();
createSprites();
}
@@ -51,6 +52,7 @@ HiScoreTable::HiScoreTable()
HiScoreTable::~HiScoreTable()
{
SDL_DestroyTexture(backbuffer_);
options.game.clear_last_hi_score_entries();
}
// Actualiza las variables
@@ -364,6 +366,8 @@ void HiScoreTable::updateSprites()
{
entry->update();
}
glowEntryNames();
}
// Inicializa el fade
@@ -419,3 +423,45 @@ void HiScoreTable::initBackground()
break;
}
}
// Obtiene un color del vector de colores de entradas
Color HiScoreTable::getEntryColor(int counter_)
{
int cycle_length = entry_colors_.size() * 2 - 2; // Esto es 6 en este caso
int n = counter_ % cycle_length;
int index;
if (n < entry_colors_.size())
{
index = n; // Avanza: 0,1,2,3
}
else
{
index = 2 * (entry_colors_.size() - 1) - n; // Retrocede: 2,1
}
return entry_colors_[index];
}
// Inicializa los colores de las entradas
void HiScoreTable::iniEntryColors()
{
entry_colors_.clear();
entry_colors_.emplace_back(background_fade_color_.getInverse().lighten(75));
entry_colors_.emplace_back(background_fade_color_.getInverse().lighten(50));
entry_colors_.emplace_back(background_fade_color_.getInverse().lighten(25));
entry_colors_.emplace_back(background_fade_color_.getInverse());
}
// Hace brillar los nombres de la tabla de records
void HiScoreTable::glowEntryNames()
{
const Color entry_color = getEntryColor(counter_ / 5);
for (const auto& entry_index : options.game.last_hi_score_entry)
{
if (entry_index != -1)
{
entry_names_.at(entry_index)->getTexture()->setColor(entry_color);
}
}
}

View File

@@ -7,8 +7,8 @@
#include <string> // Para string
#include <vector> // Para vector
#include "utils.h"
class Background; // lines 10-10
class Fade; // lines 11-11
class Background; // lines 10-10
class Fade; // lines 11-11
class PathSprite;
class Sprite;
enum class FadeMode : Uint8; // lines 13-13
@@ -42,11 +42,12 @@ private:
std::vector<Path> paths_; // Vector con los recorridos precalculados
// Variables
Uint16 counter_ = 0; // Contador
Uint32 ticks_; // Contador de ticks para ajustar la velocidad del programa
SDL_Rect view_area_; // Parte de la textura que se muestra en pantalla
FadeMode fade_mode_; // Modo de fade a utilizar
Color background_fade_color_; // Color de atenuación del fondo
Uint16 counter_ = 0; // Contador
Uint32 ticks_; // Contador de ticks para ajustar la velocidad del programa
SDL_Rect view_area_; // Parte de la textura que se muestra en pantalla
FadeMode fade_mode_; // Modo de fade a utilizar
Color background_fade_color_; // Color de atenuación del fondo
std::vector<Color> entry_colors_; // Colores para destacar las entradas en la tabla
// Actualiza las variables
void update();
@@ -84,6 +85,15 @@ private:
// Inicializa el fondo
void initBackground();
// Obtiene un color del vector de colores de entradas
Color getEntryColor(int counter_);
// Inicializa los colores de las entradas
void iniEntryColors();
// Hace brillar los nombres de la tabla de records
void glowEntryNames();
public:
// Constructor
HiScoreTable();

View File

@@ -13,7 +13,7 @@ void ManageHiScoreTable::clear()
// Añade 10 entradas predefinidas
table_.push_back(HiScoreEntry("BRY", 1000000));
table_.push_back(HiScoreEntry("USUFON", 500000, true));
table_.push_back(HiScoreEntry("USUFON", 500000));
table_.push_back(HiScoreEntry("GLUCAS", 100000));
table_.push_back(HiScoreEntry("PDLGAT", 50000));
table_.push_back(HiScoreEntry("PARRAB", 10000));
@@ -27,7 +27,7 @@ void ManageHiScoreTable::clear()
}
// Añade un elemento a la tabla
void ManageHiScoreTable::add(HiScoreEntry entry)
int ManageHiScoreTable::add(const HiScoreEntry &entry)
{
// Añade la entrada a la tabla
table_.push_back(entry);
@@ -35,8 +35,32 @@ void ManageHiScoreTable::add(HiScoreEntry entry)
// Ordena la tabla
sort();
// Encontrar la posición del nuevo elemento
auto it = std::find_if(table_.begin(), table_.end(), [&](const HiScoreEntry &e)
{ return e.name == entry.name &&
e.score == entry.score &&
e.one_credit_complete == entry.one_credit_complete; });
int position = -1;
if (it != table_.end())
{
position = std::distance(table_.begin(), it);
}
// Deja solo las 10 primeras entradas
table_.resize(10);
if (table_.size() > 10)
{
table_.resize(10);
// Si el nuevo elemento quedó fuera del top 10
if (position >= 10)
{
position = -1; // No entró en el top 10
}
}
// Devuelve la posición
return position;
}
// Ordena la tabla
@@ -50,52 +74,55 @@ void ManageHiScoreTable::sort()
std::sort(table_.begin(), table_.end(), scoreDescendingComparator);
}
// Carga la tabla con los datos de un fichero
// Carga la tabla desde un fichero
bool ManageHiScoreTable::loadFromFile(const std::string &file_path)
{
clear();
auto success = true;
auto file = SDL_RWFromFile(file_path.c_str(), "r+b");
auto file = SDL_RWFromFile(file_path.c_str(), "rb");
if (file)
{
std::cout << "Reading file: " << getFileName(file_path) << std::endl;
table_.clear(); // Limpia la tabla actual
for (auto &entry : table_)
// Lee el número de entradas en la tabla
int tableSize = 0;
SDL_RWread(file, &tableSize, sizeof(int), 1);
// Lee los datos de cada entrada
for (int i = 0; i < tableSize; ++i)
{
HiScoreEntry entry;
// Lee la puntuación
SDL_RWread(file, &entry.score, sizeof(int), 1);
// Lee el tamaño del nombre y luego el nombre
int nameSize = 0;
if (SDL_RWread(file, &entry.score, sizeof(int), 1) == 0)
{
success = false;
break;
}
if (SDL_RWread(file, &nameSize, sizeof(int), 1) == 0)
{
success = false;
break;
}
SDL_RWread(file, &nameSize, sizeof(int), 1);
std::vector<char> nameBuffer(nameSize + 1);
if (SDL_RWread(file, nameBuffer.data(), sizeof(char) * nameSize, 1) == 0)
{
success = false;
break;
}
nameBuffer[nameSize] = '\0';
SDL_RWread(file, nameBuffer.data(), nameSize, 1);
nameBuffer[nameSize] = '\0'; // Asegurar el fin de la cadena
entry.name = std::string(nameBuffer.data());
// Lee el valor de one_credit_complete
int occValue = 0;
SDL_RWread(file, &occValue, sizeof(int), 1);
entry.one_credit_complete = (occValue != 0);
// Añade la entrada a la tabla
table_.push_back(entry);
}
std::cout << "Reading file: " << getFileName(file_path) << std::endl;
SDL_RWclose(file);
}
if (!success)
else
{
clear();
std::cout << "Error: Unable to load " << getFileName(file_path) << " file! " << SDL_GetError() << std::endl;
success = false;
}
return success;
}
@@ -107,21 +134,35 @@ bool ManageHiScoreTable::saveToFile(const std::string &file_path)
if (file)
{
// Guarda los datos
for (int i = 0; i < (int)table_.size(); ++i)
// Guarda el número de entradas en la tabla
int tableSize = static_cast<int>(table_.size());
SDL_RWwrite(file, &tableSize, sizeof(int), 1);
// Guarda los datos de cada entrada
for (int i = 0; i < tableSize; ++i)
{
SDL_RWwrite(file, &table_.at(i).score, sizeof(int), 1);
const int nameSize = (int)table_.at(i).name.size();
const HiScoreEntry& entry = table_.at(i);
// Guarda la puntuación
SDL_RWwrite(file, &entry.score, sizeof(int), 1);
// Guarda el tamaño del nombre y luego el nombre
int nameSize = static_cast<int>(entry.name.size());
SDL_RWwrite(file, &nameSize, sizeof(int), 1);
SDL_RWwrite(file, table_.at(i).name.c_str(), nameSize, 1);
SDL_RWwrite(file, entry.name.c_str(), nameSize, 1);
// Guarda el valor de one_credit_complete como un entero (0 o 1)
int occValue = entry.one_credit_complete ? 1 : 0;
SDL_RWwrite(file, &occValue, sizeof(int), 1);
}
std::cout << "Writing file: " << getFileName(file_path).c_str() << std::endl;
std::cout << "Writing file: " << getFileName(file_path) << std::endl;
SDL_RWclose(file);
}
else
{
std::cout << "Error: Unable to save " << getFileName(file_path).c_str() << " file! " << SDL_GetError() << std::endl;
std::cout << "Error: Unable to save " << getFileName(file_path) << " file! " << SDL_GetError() << std::endl;
success = false;
}
return success;
}

View File

@@ -45,7 +45,7 @@ public:
void clear();
// Añade un elemento a la tabla
void add(HiScoreEntry entry);
int add(const HiScoreEntry& entry);
// Carga la tabla con los datos de un fichero
bool loadFromFile(const std::string &file_path);

View File

@@ -44,6 +44,7 @@ void initOptions()
options.game.difficulty = GameDifficulty::NORMAL;
options.game.language = lang::Code::ba_BA;
options.game.autofire = true;
options.game.clear_last_hi_score_entries();
// Opciones de control
options.controllers.clear();

View File

@@ -64,10 +64,18 @@ struct OptionsAudio
// Estructura para las opciones del juego
struct OptionsGame
{
GameDifficulty difficulty; // Dificultad del juego
lang::Code language; // Idioma usado en el juego
bool autofire; // Indica si el jugador ha de pulsar repetidamente para disparar o basta con mantener pulsado
std::vector<HiScoreEntry> hi_score_table; // Tabla con las mejores puntuaciones
GameDifficulty difficulty; // Dificultad del juego
lang::Code language; // Idioma usado en el juego
bool autofire; // Indicador de autofire
std::vector<HiScoreEntry> hi_score_table; // Tabla de mejores puntuaciones
std::vector<int> last_hi_score_entry = { -1, -1 }; // Inicialización directa con dos elementos en -1
// Método para reiniciar las últimas entradas de puntuación
void clear_last_hi_score_entries()
{
last_hi_score_entry[0] = -1;
last_hi_score_entry[1] = -1;
}
};
// Estructura para los controles del juego

View File

@@ -59,7 +59,6 @@ void Player::init()
score_multiplier_ = 1.0f;
cool_down_ = 10;
enter_name_->init();
++credits_used_;
// Establece la posición del sprite
player_sprite_->clear();
@@ -531,7 +530,7 @@ void Player::setPlayingState(PlayerState state)
// Activa la animación de morir
player_sprite_->setAccelY(0.2f);
player_sprite_->setVelY(-6.6f);
rand() % 2 == 0 ? player_sprite_->setVelX(3.3f) : player_sprite_->setVelX(-3.3f);
(rand() % 2 == 0) ? player_sprite_->setVelX(3.3f) : player_sprite_->setVelX(-3.3f);
break;
}
case PlayerState::DIED:

View File

@@ -94,7 +94,7 @@ private:
Uint32 enter_name_ticks_ = 0; // Variable para poder cambiar el contador de poner nombre en función del tiempo
int step_counter_ = 0; // Cuenta los pasos para los estados en los que camina automáticamente
bool game_completed_ = false; // Indica si ha completado el juego
int credits_used_ = 0; // Indica el numero de veces que ha continuado
int credits_used_ = 1; // Indica el numero de veces que ha continuado
// Actualiza el circulo de colisión a la posición del jugador
void shiftColliders();
@@ -245,4 +245,5 @@ public:
void setScoreBoardPanel(int panel) { scoreboard_panel_ = panel; }
void setScoreMultiplier(float value) { score_multiplier_ = value; }
void setWalkingState(PlayerState state) { walking_state_ = state; }
void addCredit() { ++credits_used_; }
};

View File

@@ -394,10 +394,9 @@ void Screen::renderInfo()
// Contador de service_pressed_counter
if (const int counter = globalInputs::service_pressed_counter; counter > 0)
{
dbg_print(0, 8, std::to_string(counter).c_str(), 255, 0, 255);
const std::string atten = attenuate_effect_ ? "ATTEN YES" : "ATTEN NO";
dbg_print(0, 16, atten.c_str(), 255, 0, 0);
}
}
}