tiled_bg.cpp, afegit changeSpeedTo()

This commit is contained in:
2025-09-24 18:44:37 +02:00
parent ec65ff9acb
commit cadf7de3d8
4 changed files with 50 additions and 2 deletions

View File

@@ -42,7 +42,7 @@ Director::Director(int argc, std::span<char *> argv) {
Section::name = Section::Name::GAME;
Section::options = Section::Options::GAME_PLAY_1P;
#elif _DEBUG
Section::name = Section::Name::GAME;
Section::name = Section::Name::TITLE;
Section::options = Section::Options::GAME_PLAY_1P;
#else // NORMAL GAME
Section::name = Section::Name::LOGO;

View File

@@ -46,7 +46,7 @@ Title::Title()
num_controllers_(Input::get()->getNumGamepads()) {
// Configura objetos
tiled_bg_->setColor(param.title.bg_color);
tiled_bg_->setSpeed(60.0F); // Set appropriate speed for seconds-based deltaTime
tiled_bg_->setSpeed(0.0F);
game_logo_->enable();
mini_logo_sprite_->setX(param.game.game_area.center_x - (mini_logo_sprite_->getWidth() / 2));
fade_->setColor(param.fade.color);
@@ -493,6 +493,7 @@ void Title::setState(State state) {
break;
case State::LOGO_FINISHED:
Audio::get()->playMusic("title.ogg");
tiled_bg_->changeSpeedTo(60.0F, 1.0F);
break;
case State::START_HAS_BEEN_PRESSED:
Audio::get()->fadeOutMusic(MUSIC_FADE_OUT_LONG_MS);

View File

@@ -83,6 +83,7 @@ void TiledBG::render() {
// Actualiza la lógica de la clase (time-based)
void TiledBG::update(float delta_time) {
updateSpeedChange(delta_time);
updateDesp(delta_time);
updateStop(delta_time);
@@ -130,3 +131,39 @@ void TiledBG::updateStop(float delta_time) {
}
}
}
// Cambia la velocidad gradualmente en X segundos
void TiledBG::changeSpeedTo(float target_speed, float duration_s) {
if (duration_s <= 0.0f) {
// Si la duración es 0 o negativa, cambia inmediatamente
speed_ = target_speed;
changing_speed_ = false;
return;
}
// Configurar el cambio gradual
changing_speed_ = true;
initial_speed_ = speed_;
target_speed_ = target_speed;
change_duration_s_ = duration_s;
change_timer_s_ = 0.0f;
}
// Actualiza el cambio gradual de velocidad (time-based)
void TiledBG::updateSpeedChange(float delta_time) {
if (!changing_speed_) {
return;
}
change_timer_s_ += delta_time;
if (change_timer_s_ >= change_duration_s_) {
// Cambio completado
speed_ = target_speed_;
changing_speed_ = false;
} else {
// Interpolación lineal entre velocidad inicial y objetivo
float progress = change_timer_s_ / change_duration_s_;
speed_ = initial_speed_ + (target_speed_ - initial_speed_) * progress;
}
}

View File

@@ -29,11 +29,13 @@ class TiledBG {
// --- Configuración ---
void setSpeed(float speed) { speed_ = speed; } // Establece la velocidad
void changeSpeedTo(float target_speed, float duration_s); // Cambia la velocidad gradualmente en X segundos
void stopGracefully() { stopping_ = true; } // Detiene el desplazamiento de forma ordenada
void setColor(Color color) { SDL_SetTextureColorMod(canvas_, color.r, color.g, color.b); } // Cambia el color de la textura
// --- Getters ---
[[nodiscard]] auto isStopped() const -> bool { return speed_ == 0.0F; } // Indica si está parado
[[nodiscard]] auto isChangingSpeed() const -> bool { return changing_speed_; } // Indica si está cambiando velocidad gradualmente
private:
// --- Constantes ---
@@ -56,8 +58,16 @@ class TiledBG {
float speed_ = 1.0F; // Incremento que se añade al desplazamiento a cada bucle
bool stopping_ = false; // Indica si se está deteniendo
// --- Variables para cambio gradual de velocidad ---
bool changing_speed_ = false; // Indica si está cambiando velocidad gradualmente
float initial_speed_ = 0.0F; // Velocidad inicial del cambio
float target_speed_ = 0.0F; // Velocidad objetivo del cambio
float change_duration_s_ = 0.0F; // Duración total del cambio en segundos
float change_timer_s_ = 0.0F; // Tiempo transcurrido del cambio
// --- Métodos internos ---
void fillTexture(); // Rellena la textura con el contenido
void updateDesp(float delta_time) { desp_ += speed_ * delta_time; } // Actualiza el desplazamiento (time-based)
void updateStop(float delta_time); // Detiene el desplazamiento de forma ordenada (time-based)
void updateSpeedChange(float delta_time); // Actualiza el cambio gradual de velocidad (time-based)
};