migrat instructions a delta_time pur

This commit is contained in:
2025-10-23 18:59:37 +02:00
parent 245524c021
commit 1acbe1d097
3 changed files with 55 additions and 36 deletions

View File

@@ -49,8 +49,8 @@ Instructions::Instructions()
fade_->setMode(Fade::Mode::IN);
fade_->activate();
// Inicializa las líneas con un retraso progresivo de 50 ms
lines_ = initializeLines(256);
// Inicializa las líneas con un retraso progresivo
lines_ = initializeLines(256, LINE_START_DELAY_S);
// Rellena la textura de texto
fillTexture();
@@ -278,39 +278,53 @@ void Instructions::run() {
}
// Método para inicializar las líneas
auto Instructions::initializeLines(int height) -> std::vector<Line> {
auto Instructions::initializeLines(int height, float line_delay) -> std::vector<Line> {
std::vector<Line> lines;
for (int y = 0; y < height; y++) {
int direction = (y % 2 == 0) ? -1 : 1; // Pares a la izquierda, impares a la derecha
lines.emplace_back(y, 0.0F, direction);
float delay = y * line_delay; // Retraso progresivo basado en la línea
lines.emplace_back(y, 0.0F, direction, delay);
}
return lines;
}
// Método para mover las líneas con suavizado
auto Instructions::moveLines(std::vector<Line>& lines, int width, float duration, Uint32 start_delay) -> bool {
Uint32 current_time = SDL_GetTicks();
// Método para mover las líneas con suavizado (usando delta_time puro)
auto Instructions::moveLines(std::vector<Line>& lines, int width, float duration, float delta_time) -> bool {
bool all_lines_off_screen = true;
for (auto& line : lines) {
// Establecer start_time en el primer cuadro de animación
if (line.start_time == 0) {
line.start_time = current_time + line.y * start_delay;
// Verificar si la línea ha superado su tiempo de retraso
if (!line.started) {
line.delay_time -= delta_time;
if (line.delay_time <= 0.0F) {
line.started = true;
line.accumulated_time = 0.0F;
} else {
all_lines_off_screen = false; // Aún hay líneas esperando para empezar
continue;
}
}
float elapsed_time = (current_time - line.start_time) / 1000.0F; // Convertir a segundos
if (elapsed_time < 0) {
all_lines_off_screen = false; // Si aún no se debe mover esta línea, no están todas fuera de pantalla
continue;
}
if (elapsed_time >= duration) {
continue; // Si la línea ha salido de los límites, no la muevas más
// Si la línea ya ha completado su movimiento, saltarla
if (line.accumulated_time >= duration) {
continue; // Esta línea ya terminó
}
float t = elapsed_time / duration;
// Acumular tiempo y calcular posición
line.accumulated_time += delta_time;
if (line.accumulated_time >= duration) {
// La línea ha completado su movimiento
line.accumulated_time = duration;
} else {
// La línea aún se está moviendo
all_lines_off_screen = false;
}
// Calcular posición con suavizado
float t = line.accumulated_time / duration;
float smooth_factor = easeInOutQuint(t);
line.x = line.direction * smooth_factor * width;
all_lines_off_screen = false; // Si alguna línea aún se está moviendo, no están todas fuera de pantalla
}
return all_lines_off_screen;
@@ -327,12 +341,13 @@ void Instructions::renderLines(SDL_Renderer* renderer, SDL_Texture* texture, con
// Gestiona la textura con los graficos
void Instructions::updateBackbuffer(float delta_time) {
// Establece la ventana del backbuffer (convertir elapsed_time_ a equivalente de counter)
float counter_equivalent = elapsed_time_ * 60.0F; // Convertir segundos a equivalente frame para UI
view_.y = std::max(0.0F, param.game.height - counter_equivalent + 100);
// Establece la ventana del backbuffer usando velocidad en pixels por segundo
// El scroll comienza desde (param.game.height + 100) y desciende a 0
float scroll_offset = elapsed_time_ * SCROLL_SPEED_PPS;
view_.y = std::max(0.0F, (param.game.height + 100.0F) - scroll_offset);
// Verifica si view_.y == 0 y gestiona el temporizador
if (view_.y == 0) {
if (view_.y == 0.0F) {
if (!start_delay_triggered_) {
// Activa el temporizador si no ha sido activado
start_delay_triggered_ = true;
@@ -341,7 +356,7 @@ void Instructions::updateBackbuffer(float delta_time) {
start_delay_timer_ += delta_time;
if (start_delay_timer_ >= START_DELAY_S) {
// Han pasado los segundos de retraso, mover líneas
all_lines_off_screen_ = moveLines(lines_, 320, LINE_MOVE_DURATION_S, static_cast<Uint32>(LINE_START_DELAY_MS));
all_lines_off_screen_ = moveLines(lines_, 320, LINE_MOVE_DURATION_S, delta_time);
}
}
}