claude: arreglos d'estil
This commit is contained in:
+42
-38
@@ -1,20 +1,24 @@
|
||||
#include "stage.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
// Implementación de StageData
|
||||
StageData::StageData(int power_to_complete, int min_menace, int max_menace, const std::string& name)
|
||||
: power_to_complete_(power_to_complete), min_menace_(min_menace),
|
||||
max_menace_(max_menace), name_(name), status_(StageStatus::LOCKED) {}
|
||||
: status_(StageStatus::LOCKED),
|
||||
name_(name),
|
||||
power_to_complete_(power_to_complete),
|
||||
min_menace_(min_menace),
|
||||
max_menace_(max_menace) {}
|
||||
|
||||
// Implementación de StageManager
|
||||
StageManager::StageManager()
|
||||
: current_power_(0), total_power_(0), current_stage_index_(0),
|
||||
: power_change_callback_(nullptr),
|
||||
power_collection_state_(PowerCollectionState::ENABLED),
|
||||
power_change_callback_(nullptr) {
|
||||
initialize();
|
||||
}
|
||||
current_stage_index_(0),
|
||||
current_power_(0),
|
||||
total_power_(0) { initialize(); }
|
||||
|
||||
void StageManager::initialize() {
|
||||
stages_.clear();
|
||||
@@ -24,12 +28,12 @@ void StageManager::initialize() {
|
||||
|
||||
void StageManager::initialize(const std::string& stages_file) {
|
||||
stages_.clear();
|
||||
|
||||
|
||||
// Intentar cargar desde archivo, si falla usar valores predeterminados
|
||||
if (!loadStagesFromFile(stages_file)) {
|
||||
createDefaultStages();
|
||||
}
|
||||
|
||||
|
||||
reset();
|
||||
}
|
||||
|
||||
@@ -58,22 +62,22 @@ void StageManager::createDefaultStages() {
|
||||
bool StageManager::loadStagesFromFile(const std::string& filename) {
|
||||
std::ifstream file(filename);
|
||||
if (!file.is_open()) {
|
||||
return false; // No se pudo abrir el archivo
|
||||
return false; // No se pudo abrir el archivo
|
||||
}
|
||||
|
||||
|
||||
std::string line;
|
||||
|
||||
|
||||
while (std::getline(file, line)) {
|
||||
// Ignorar líneas vacías y comentarios (líneas que empiezan con #)
|
||||
if (line.empty() || line[0] == '#') {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// Parsear línea: power_to_complete,min_menace,max_menace,name
|
||||
std::stringstream ss(line);
|
||||
std::string token;
|
||||
std::vector<std::string> tokens;
|
||||
|
||||
|
||||
// Dividir por comas
|
||||
while (std::getline(ss, token, ',')) {
|
||||
// Eliminar espacios en blanco al inicio y final
|
||||
@@ -81,36 +85,36 @@ bool StageManager::loadStagesFromFile(const std::string& filename) {
|
||||
token.erase(token.find_last_not_of(" \t") + 1);
|
||||
tokens.push_back(token);
|
||||
}
|
||||
|
||||
|
||||
// Verificar que tenemos exactamente 4 campos
|
||||
if (tokens.size() != 4) {
|
||||
// Error de formato, continuar con la siguiente línea
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
// Convertir a enteros los primeros tres campos
|
||||
int power_to_complete = std::stoi(tokens[0]);
|
||||
int min_menace = std::stoi(tokens[1]);
|
||||
int max_menace = std::stoi(tokens[2]);
|
||||
std::string name = tokens[3];
|
||||
|
||||
|
||||
// Validar valores
|
||||
if (power_to_complete <= 0 || min_menace < 0 || max_menace < min_menace) {
|
||||
continue; // Valores inválidos, saltar línea
|
||||
continue; // Valores inválidos, saltar línea
|
||||
}
|
||||
|
||||
|
||||
// Crear y añadir la fase
|
||||
stages_.emplace_back(power_to_complete, min_menace, max_menace, name);
|
||||
|
||||
|
||||
} catch (const std::exception&) {
|
||||
// Error de conversión, continuar con la siguiente línea
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
file.close();
|
||||
|
||||
|
||||
// Verificar que se cargó al menos una fase
|
||||
return !stages_.empty();
|
||||
}
|
||||
@@ -119,9 +123,9 @@ bool StageManager::advanceToNextStage() {
|
||||
if (!isCurrentStageCompleted() || current_stage_index_ >= stages_.size() - 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
current_stage_index_++;
|
||||
current_power_ = 0; // Reiniciar poder para la nueva fase
|
||||
current_power_ = 0; // Reiniciar poder para la nueva fase
|
||||
updateStageStatuses();
|
||||
return true;
|
||||
}
|
||||
@@ -130,18 +134,18 @@ bool StageManager::jumpToStage(size_t target_stage_index) {
|
||||
if (!validateStageIndex(target_stage_index)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Calcular el poder acumulado hasta la fase objetivo
|
||||
int accumulated_power = 0;
|
||||
for (size_t i = 0; i < target_stage_index; ++i) {
|
||||
accumulated_power += stages_[i].getPowerToComplete();
|
||||
}
|
||||
|
||||
|
||||
// Actualizar estado
|
||||
current_stage_index_ = target_stage_index;
|
||||
current_power_ = 0; // Comenzar la fase objetivo sin poder
|
||||
current_power_ = 0; // Comenzar la fase objetivo sin poder
|
||||
total_power_ = accumulated_power; // Poder total como si se hubieran completado las anteriores
|
||||
|
||||
|
||||
updateStageStatuses();
|
||||
return true;
|
||||
}
|
||||
@@ -150,7 +154,7 @@ bool StageManager::subtractPower(int amount) {
|
||||
if (amount <= 0 || current_power_ < amount) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
current_power_ -= amount;
|
||||
updateStageStatuses();
|
||||
return true;
|
||||
@@ -180,7 +184,7 @@ bool StageManager::isCurrentStageCompleted() const {
|
||||
if (!current_stage.has_value()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
return current_power_ >= current_stage->getPowerToComplete();
|
||||
}
|
||||
|
||||
@@ -190,10 +194,10 @@ bool StageManager::isGameCompleted() const {
|
||||
|
||||
double StageManager::getProgressPercentage() const {
|
||||
if (stages_.empty()) return 0.0;
|
||||
|
||||
|
||||
int total_power_needed = getTotalPowerNeededToCompleteGame();
|
||||
if (total_power_needed == 0) return 100.0;
|
||||
|
||||
|
||||
return (static_cast<double>(total_power_) / total_power_needed) * 100.0;
|
||||
}
|
||||
|
||||
@@ -206,12 +210,12 @@ double StageManager::getCurrentStageProgressFraction() const {
|
||||
if (!current_stage.has_value()) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
|
||||
int power_needed = current_stage->getPowerToComplete();
|
||||
if (power_needed == 0) {
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
|
||||
// Devuelve una fracción entre 0.0 y 1.0
|
||||
double fraction = static_cast<double>(current_power_) / power_needed;
|
||||
return std::min(fraction, 1.0);
|
||||
@@ -222,7 +226,7 @@ int StageManager::getPowerNeededForCurrentStage() const {
|
||||
if (!current_stage.has_value()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
return std::max(0, current_stage->getPowerToComplete() - current_power_);
|
||||
}
|
||||
|
||||
@@ -243,7 +247,7 @@ void StageManager::addPower(int amount) {
|
||||
if (amount <= 0 || !canCollectPower()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
current_power_ += amount;
|
||||
total_power_ += amount;
|
||||
|
||||
@@ -251,7 +255,7 @@ void StageManager::addPower(int amount) {
|
||||
if (power_change_callback_) {
|
||||
power_change_callback_(amount);
|
||||
}
|
||||
|
||||
|
||||
// Verificar si se completó la fase actual
|
||||
if (isCurrentStageCompleted()) {
|
||||
auto current_stage = getCurrentStage();
|
||||
@@ -259,7 +263,7 @@ void StageManager::addPower(int amount) {
|
||||
stages_[current_stage_index_].setStatus(StageStatus::COMPLETED);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
updateStageStatuses();
|
||||
}
|
||||
|
||||
@@ -268,7 +272,7 @@ int StageManager::getCurrentMenaceLevel() const {
|
||||
if (!current_stage.has_value()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
return current_stage->getMinMenace();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user