delta-time: balloon.cpp

delta-time: balloon_manager.cpp
delta-time: credits.cpp
This commit is contained in:
2025-09-16 22:38:48 +02:00
parent a96a17e11b
commit a15e29344f
7 changed files with 367 additions and 50 deletions

View File

@@ -78,20 +78,31 @@ Credits::~Credits() {
Options::gamepad_manager.clearPlayers();
}
// Calcula el deltatime
auto Credits::calculateDeltaTime() -> float {
const Uint64 current_time = SDL_GetTicks();
const float delta_time = static_cast<float>(current_time - last_time_);
last_time_ = current_time;
return delta_time;
}
// Bucle principal
void Credits::run() {
last_time_ = SDL_GetTicks();
while (Section::name == Section::Name::CREDITS) {
checkInput();
update();
const float delta_time = calculateDeltaTime();
update(delta_time);
checkEvents(); // Tiene que ir antes del render
render();
}
}
// Actualiza las variables
// Actualiza las variables (frame-based)
void Credits::update() {
if (SDL_GetTicks() - ticks_ > param.game.speed) {
ticks_ = SDL_GetTicks();
if (SDL_GetTicks() - last_time_ > param.game.speed) {
last_time_ = SDL_GetTicks();
const int REPEAT = want_to_pass_ ? 4 : 1;
for (int i = 0; i < REPEAT; ++i) {
tiled_bg_->update();
@@ -111,6 +122,28 @@ void Credits::update() {
Audio::update();
}
// Actualiza las variables (time-based)
void Credits::update(float deltaTime) {
const float multiplier = want_to_pass_ ? 4.0f : 1.0f;
const float adjusted_delta_time = deltaTime * multiplier;
tiled_bg_->update(adjusted_delta_time);
cycleColors();
balloon_manager_->update(adjusted_delta_time);
updateTextureDstRects(adjusted_delta_time);
throwBalloons(adjusted_delta_time);
updatePlayers(adjusted_delta_time);
updateAllFades(adjusted_delta_time);
// Convertir deltaTime a factor de frame (asumiendo 60fps)
const float frameFactor = adjusted_delta_time / (1000.0f / 60.0f);
counter_ += frameFactor;
Screen::get()->update();
fillCanvas();
Audio::update();
}
// Dibuja Credits::en patalla
void Credits::render() {
static auto *const SCREEN = Screen::get();
@@ -277,9 +310,9 @@ void Credits::fillCanvas() {
SDL_SetRenderTarget(Screen::get()->getRenderer(), temp);
}
// Actualiza el destino de los rectangulos de las texturas
// Actualiza el destino de los rectangulos de las texturas (frame-based)
void Credits::updateTextureDstRects() {
if (counter_ % 10 == 0) {
if (static_cast<int>(counter_) % 10 == 0) {
// Comprueba la posición de la textura con los titulos de credito
if (credits_rect_dst_.y + credits_rect_dst_.h > play_area_.y) {
--credits_rect_dst_.y;
@@ -306,7 +339,42 @@ void Credits::updateTextureDstRects() {
}
}
// Tira globos al escenario
// Actualiza el destino de los rectangulos de las texturas (time-based)
void Credits::updateTextureDstRects(float deltaTime) {
constexpr float TEXTURE_UPDATE_INTERVAL = 10 * (1000.0f / 60.0f); // 166.67ms (cada 10 frames)
static float texture_accumulator = 0.0f;
texture_accumulator += deltaTime;
if (texture_accumulator >= TEXTURE_UPDATE_INTERVAL) {
texture_accumulator -= TEXTURE_UPDATE_INTERVAL;
// Comprueba la posición de la textura con los titulos de credito
if (credits_rect_dst_.y + credits_rect_dst_.h > play_area_.y) {
--credits_rect_dst_.y;
}
// Comprueba la posición de la textura con el mini_logo
if (mini_logo_rect_dst_.y == mini_logo_final_pos_) {
mini_logo_on_position_ = true;
// Si el jugador quiere pasar los titulos de credito, el fade se inicia solo
if (want_to_pass_) {
fading_ = true;
}
// Se activa el contador para evitar que la sección sea infinita
if (counter_prevent_endless_ == 1000) {
fading_ = true;
} else {
++counter_prevent_endless_;
}
} else {
--mini_logo_rect_dst_.y;
}
}
}
// Tira globos al escenario (frame-based)
void Credits::throwBalloons() {
constexpr int SPEED = 200;
const std::vector<int> SETS = {0, 63, 25, 67, 17, 75, 13, 50};
@@ -315,12 +383,41 @@ void Credits::throwBalloons() {
return;
}
if (counter_ % SPEED == 0) {
const int INDEX = (counter_ / SPEED) % SETS.size();
if (static_cast<int>(counter_) % SPEED == 0) {
const int INDEX = (static_cast<int>(counter_) / SPEED) % SETS.size();
balloon_manager_->deployFormation(SETS.at(INDEX), -60);
}
if (counter_ % (SPEED * 4) == 0 && counter_ > 0) {
if (static_cast<int>(counter_) % (SPEED * 4) == 0 && counter_ > 0) {
balloon_manager_->createPowerBall();
}
}
// Tira globos al escenario (time-based)
void Credits::throwBalloons(float deltaTime) {
constexpr int SPEED = 200;
const std::vector<int> SETS = {0, 63, 25, 67, 17, 75, 13, 50};
constexpr float BALLOON_INTERVAL = SPEED * (1000.0f / 60.0f); // 3333.33ms (cada 200 frames)
constexpr float POWERBALL_INTERVAL = (SPEED * 4) * (1000.0f / 60.0f); // 13333.33ms (cada 800 frames)
if (counter_ > ((SETS.size() - 1) * SPEED) * 3) {
return;
}
static float balloon_accumulator = 0.0f;
static float powerball_accumulator = 0.0f;
balloon_accumulator += deltaTime;
powerball_accumulator += deltaTime;
if (balloon_accumulator >= BALLOON_INTERVAL) {
balloon_accumulator -= BALLOON_INTERVAL;
const int INDEX = (static_cast<int>(counter_ / SPEED)) % SETS.size();
balloon_manager_->deployFormation(SETS.at(INDEX), -60);
}
if (powerball_accumulator >= POWERBALL_INTERVAL && counter_ > 0) {
powerball_accumulator -= POWERBALL_INTERVAL;
balloon_manager_->createPowerBall();
}
}
@@ -392,12 +489,12 @@ void Credits::initPlayers() {
}
}
// Actualiza los rectangulos negros
// Actualiza los rectangulos negros (frame-based)
void Credits::updateBlackRects() {
static int current_step_ = steps_;
if (top_black_rect_.h != param.game.game_area.center_y - 1 && bottom_black_rect_.y != param.game.game_area.center_y + 1) {
// Si los rectangulos superior e inferior no han llegado al centro
if (counter_ % 4 == 0) {
if (static_cast<int>(counter_) % 4 == 0) {
// Incrementa la altura del rectangulo superior
top_black_rect_.h = std::min(top_black_rect_.h + 1, param.game.game_area.center_y - 1);
@@ -435,6 +532,57 @@ void Credits::updateBlackRects() {
}
}
// Actualiza los rectangulos negros (time-based)
void Credits::updateBlackRects(float deltaTime) {
static float current_step_ = static_cast<float>(steps_);
constexpr float BLACK_RECT_INTERVAL = 4 * (1000.0f / 60.0f); // 66.67ms (cada 4 frames)
static float black_rect_accumulator = 0.0f;
if (top_black_rect_.h != param.game.game_area.center_y - 1 && bottom_black_rect_.y != param.game.game_area.center_y + 1) {
// Si los rectangulos superior e inferior no han llegado al centro
black_rect_accumulator += deltaTime;
if (black_rect_accumulator >= BLACK_RECT_INTERVAL) {
black_rect_accumulator -= BLACK_RECT_INTERVAL;
// Incrementa la altura del rectangulo superior
top_black_rect_.h = std::min(top_black_rect_.h + 1, param.game.game_area.center_y - 1);
// Incrementa la altura y modifica la posición del rectangulo inferior
++bottom_black_rect_.h;
bottom_black_rect_.y = std::max(bottom_black_rect_.y - 1, param.game.game_area.center_y + 1);
--current_step_;
setVolume(static_cast<int>(initial_volume_ * current_step_ / steps_));
}
} else {
// Si los rectangulos superior e inferior han llegado al centro
if (left_black_rect_.w != param.game.game_area.center_x && right_black_rect_.x != param.game.game_area.center_x) {
constexpr int SPEED = 2;
// Si los rectangulos izquierdo y derecho no han llegado al centro
// Incrementa la anchura del rectangulo situado a la izquierda
left_black_rect_.w = std::min(left_black_rect_.w + SPEED, param.game.game_area.center_x);
// Incrementa la anchura y modifica la posición del rectangulo situado a la derecha
right_black_rect_.w += SPEED;
right_black_rect_.x = std::max(right_black_rect_.x - SPEED, param.game.game_area.center_x);
--current_step_;
setVolume(static_cast<int>(initial_volume_ * current_step_ / steps_));
} else {
// Si los rectangulos izquierdo y derecho han llegado al centro
setVolume(0);
Audio::get()->stopMusic();
if (counter_pre_fade_ == 400) {
fade_out_->activate();
} else {
// Convertir deltaTime a factor de frame
const float frameFactor = deltaTime / (1000.0f / 60.0f);
counter_pre_fade_ += frameFactor;
}
}
}
}
// Actualiza el rectangulo rojo
void Credits::updateRedRect() {
border_rect_.x = left_black_rect_.x + left_black_rect_.w;
@@ -443,7 +591,7 @@ void Credits::updateRedRect() {
border_rect_.h = bottom_black_rect_.y - border_rect_.y + 1;
}
// Actualiza el estado de fade
// Actualiza el estado de fade (frame-based)
void Credits::updateAllFades() {
if (fading_) {
updateBlackRects();
@@ -461,6 +609,24 @@ void Credits::updateAllFades() {
}
}
// Actualiza el estado de fade (time-based)
void Credits::updateAllFades(float deltaTime) {
if (fading_) {
updateBlackRects(deltaTime);
updateRedRect();
}
fade_in_->update(); // Fade aún no migrado a deltaTime
if (fade_in_->hasEnded()) {
Audio::get()->playMusic("credits.ogg");
}
fade_out_->update(); // Fade aún no migrado a deltaTime
if (fade_out_->hasEnded()) {
Section::name = Section::Name::HI_SCORE_TABLE;
}
}
// Establece el nivel de volumen
void Credits::setVolume(int amount) {
Options::audio.music.volume = std::clamp(amount, 0, 100);
@@ -508,13 +674,20 @@ void Credits::cycleColors() {
tiled_bg_->setColor(color_);
}
// Actualza los jugadores
// Actualza los jugadores (frame-based)
void Credits::updatePlayers() {
for (auto &player : players_) {
player->update();
}
}
// Actualza los jugadores (time-based)
void Credits::updatePlayers(float deltaTime) {
for (auto &player : players_) {
player->update(deltaTime);
}
}
// Renderiza los jugadores
void Credits::renderPlayers() {
for (auto const &player : players_) {