tiled_bg.cpp, afegit changeSpeedTo()
This commit is contained in:
@@ -42,7 +42,7 @@ Director::Director(int argc, std::span<char *> argv) {
|
|||||||
Section::name = Section::Name::GAME;
|
Section::name = Section::Name::GAME;
|
||||||
Section::options = Section::Options::GAME_PLAY_1P;
|
Section::options = Section::Options::GAME_PLAY_1P;
|
||||||
#elif _DEBUG
|
#elif _DEBUG
|
||||||
Section::name = Section::Name::GAME;
|
Section::name = Section::Name::TITLE;
|
||||||
Section::options = Section::Options::GAME_PLAY_1P;
|
Section::options = Section::Options::GAME_PLAY_1P;
|
||||||
#else // NORMAL GAME
|
#else // NORMAL GAME
|
||||||
Section::name = Section::Name::LOGO;
|
Section::name = Section::Name::LOGO;
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ Title::Title()
|
|||||||
num_controllers_(Input::get()->getNumGamepads()) {
|
num_controllers_(Input::get()->getNumGamepads()) {
|
||||||
// Configura objetos
|
// Configura objetos
|
||||||
tiled_bg_->setColor(param.title.bg_color);
|
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();
|
game_logo_->enable();
|
||||||
mini_logo_sprite_->setX(param.game.game_area.center_x - (mini_logo_sprite_->getWidth() / 2));
|
mini_logo_sprite_->setX(param.game.game_area.center_x - (mini_logo_sprite_->getWidth() / 2));
|
||||||
fade_->setColor(param.fade.color);
|
fade_->setColor(param.fade.color);
|
||||||
@@ -493,6 +493,7 @@ void Title::setState(State state) {
|
|||||||
break;
|
break;
|
||||||
case State::LOGO_FINISHED:
|
case State::LOGO_FINISHED:
|
||||||
Audio::get()->playMusic("title.ogg");
|
Audio::get()->playMusic("title.ogg");
|
||||||
|
tiled_bg_->changeSpeedTo(60.0F, 1.0F);
|
||||||
break;
|
break;
|
||||||
case State::START_HAS_BEEN_PRESSED:
|
case State::START_HAS_BEEN_PRESSED:
|
||||||
Audio::get()->fadeOutMusic(MUSIC_FADE_OUT_LONG_MS);
|
Audio::get()->fadeOutMusic(MUSIC_FADE_OUT_LONG_MS);
|
||||||
|
|||||||
@@ -83,6 +83,7 @@ void TiledBG::render() {
|
|||||||
|
|
||||||
// Actualiza la lógica de la clase (time-based)
|
// Actualiza la lógica de la clase (time-based)
|
||||||
void TiledBG::update(float delta_time) {
|
void TiledBG::update(float delta_time) {
|
||||||
|
updateSpeedChange(delta_time);
|
||||||
updateDesp(delta_time);
|
updateDesp(delta_time);
|
||||||
updateStop(delta_time);
|
updateStop(delta_time);
|
||||||
|
|
||||||
@@ -129,4 +130,40 @@ void TiledBG::updateStop(float delta_time) {
|
|||||||
stopping_ = false; // Desactivamos el estado de "stopping"
|
stopping_ = false; // Desactivamos el estado de "stopping"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -29,11 +29,13 @@ class TiledBG {
|
|||||||
|
|
||||||
// --- Configuración ---
|
// --- Configuración ---
|
||||||
void setSpeed(float speed) { speed_ = speed; } // Establece la velocidad
|
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 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
|
void setColor(Color color) { SDL_SetTextureColorMod(canvas_, color.r, color.g, color.b); } // Cambia el color de la textura
|
||||||
|
|
||||||
// --- Getters ---
|
// --- Getters ---
|
||||||
[[nodiscard]] auto isStopped() const -> bool { return speed_ == 0.0F; } // Indica si está parado
|
[[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:
|
private:
|
||||||
// --- Constantes ---
|
// --- Constantes ---
|
||||||
@@ -56,8 +58,16 @@ class TiledBG {
|
|||||||
float speed_ = 1.0F; // Incremento que se añade al desplazamiento a cada bucle
|
float speed_ = 1.0F; // Incremento que se añade al desplazamiento a cada bucle
|
||||||
bool stopping_ = false; // Indica si se está deteniendo
|
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 ---
|
// --- Métodos internos ---
|
||||||
void fillTexture(); // Rellena la textura con el contenido
|
void fillTexture(); // Rellena la textura con el contenido
|
||||||
void updateDesp(float delta_time) { desp_ += speed_ * delta_time; } // Actualiza el desplazamiento (time-based)
|
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 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)
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user