From a0fb6934b0f311e9c624dec2c18e15a973f2aa21 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 30 Sep 2025 09:56:32 +0200 Subject: [PATCH] corregit: en el mode demo no calculava correctament el estat del fondo --- source/sections/game.cpp | 5 +++-- source/stage.cpp | 45 ++++++++++++++++++++++++++++++++++++++++ source/stage.h | 1 + 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/source/sections/game.cpp b/source/sections/game.cpp index 5cdc6c4..b1b6581 100644 --- a/source/sections/game.cpp +++ b/source/sections/game.cpp @@ -1551,8 +1551,9 @@ void Game::initDemo(Player::Id player_id) { // Selecciona una pantalla al azar constexpr auto NUM_STAGES = 3; const auto STAGE = rand() % NUM_STAGES; - constexpr std::array STAGES = {0, 2, 4}; - stage_manager_->jumpToStage(STAGES.at(STAGE)); + constexpr std::array STAGES = {0.005F, 0.32F, 0.53F}; + stage_manager_->setTotalPower(stage_manager_->getTotalPowerNeededToCompleteGame() * STAGES.at(STAGE)); + background_->setProgress(stage_manager_->getTotalPower()); // Activa o no al otro jugador if (rand() % 3 != 0) { diff --git a/source/stage.cpp b/source/stage.cpp index cd37fe1..ab605ce 100644 --- a/source/stage.cpp +++ b/source/stage.cpp @@ -151,6 +151,51 @@ auto StageManager::jumpToStage(size_t target_stage_index) -> bool { return true; } +auto StageManager::setTotalPower(int target_total_power) -> bool { + if (target_total_power < 0) { + return false; + } + + int total_power_needed = getTotalPowerNeededToCompleteGame(); + if (target_total_power > total_power_needed) { + return false; + } + + // Calcular en qué fase debería estar y cuánto poder de esa fase + int accumulated_power = 0; + size_t target_stage_index = 0; + int target_current_power = 0; + + for (size_t i = 0; i < stages_.size(); ++i) { + int stage_power = stages_[i].getPowerToComplete(); + + if (accumulated_power + stage_power > target_total_power) { + // El objetivo está dentro de esta fase + target_stage_index = i; + target_current_power = target_total_power - accumulated_power; + break; + } + + accumulated_power += stage_power; + + if (accumulated_power == target_total_power) { + // El objetivo coincide exactamente con el final de esta fase + // Mover a la siguiente fase (si existe) con power 0 + target_stage_index = (i + 1 < stages_.size()) ? i + 1 : i; + target_current_power = (i + 1 < stages_.size()) ? 0 : stage_power; + break; + } + } + + // Actualizar estado + current_stage_index_ = target_stage_index; + current_power_ = target_current_power; + total_power_ = target_total_power; + + updateStageStatuses(); + return true; +} + auto StageManager::subtractPower(int amount) -> bool { if (amount <= 0 || current_power_ < amount) { return false; diff --git a/source/stage.h b/source/stage.h index 95985c9..0bfc01a 100644 --- a/source/stage.h +++ b/source/stage.h @@ -67,6 +67,7 @@ class StageManager : public IStageInfo { // --- Navegación --- auto jumpToStage(size_t target_stage_index) -> bool; // Salta a una fase específica + auto setTotalPower(int target_total_power) -> bool; // Establece el poder total y ajusta fase/progreso // --- Consultas de estado --- [[nodiscard]] auto getCurrentStage() const -> std::optional; // Obtiene la fase actual