revisat credits.cpp, player.cpp, balloon.cpp i balloon_manager.cpp
This commit is contained in:
@@ -49,16 +49,17 @@ Credits::Credits()
|
||||
|
||||
fade_in_->setColor(param.fade.color);
|
||||
fade_in_->setType(Fade::Type::FULLSCREEN);
|
||||
fade_in_->setPostDuration(50);
|
||||
fade_in_->setPostDuration(static_cast<int>(50 * (1000.0f / 60.0f))); // 50 frames = ~833ms
|
||||
fade_in_->setMode(Fade::Mode::IN);
|
||||
fade_in_->activate();
|
||||
|
||||
fade_out_->setColor(0, 0, 0);
|
||||
fade_out_->setType(Fade::Type::FULLSCREEN);
|
||||
fade_out_->setPostDuration(400);
|
||||
fade_out_->setPostDuration(static_cast<int>(400 * (1000.0f / 60.0f))); // 400 frames = ~6667ms
|
||||
|
||||
updateRedRect();
|
||||
tiled_bg_->setColor(Color(255, 96, 96));
|
||||
tiled_bg_->setSpeed(60.0F);
|
||||
|
||||
initPlayers();
|
||||
SDL_SetTextureBlendMode(text_texture_, SDL_BLENDMODE_BLEND);
|
||||
@@ -81,7 +82,7 @@ Credits::~Credits() {
|
||||
// Calcula el deltatime
|
||||
auto Credits::calculateDeltaTime() -> float {
|
||||
const Uint64 current_time = SDL_GetTicks();
|
||||
const float delta_time = static_cast<float>(current_time - last_time_);
|
||||
const float delta_time = static_cast<float>(current_time - last_time_) / 1000.0f; // Convertir ms a segundos
|
||||
last_time_ = current_time;
|
||||
return delta_time;
|
||||
}
|
||||
@@ -112,8 +113,8 @@ void Credits::update(float deltaTime) {
|
||||
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);
|
||||
// Convertir deltaTime a equivalente de frames (60fps)
|
||||
const float frameFactor = adjusted_delta_time * 60.0f;
|
||||
counter_ += frameFactor;
|
||||
|
||||
Screen::get()->update();
|
||||
@@ -287,43 +288,14 @@ void Credits::fillCanvas() {
|
||||
SDL_SetRenderTarget(Screen::get()->getRenderer(), temp);
|
||||
}
|
||||
|
||||
// Actualiza el destino de los rectangulos de las texturas (frame-based)
|
||||
void Credits::updateTextureDstRects() {
|
||||
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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
constexpr float TEXTURE_UPDATE_INTERVAL_S = 10.0f / 60.0f; // ~0.167s (cada 10 frames)
|
||||
static float texture_accumulator = 0.0f;
|
||||
texture_accumulator += deltaTime;
|
||||
|
||||
if (texture_accumulator >= TEXTURE_UPDATE_INTERVAL) {
|
||||
texture_accumulator -= TEXTURE_UPDATE_INTERVAL;
|
||||
if (texture_accumulator >= TEXTURE_UPDATE_INTERVAL_S) {
|
||||
texture_accumulator -= TEXTURE_UPDATE_INTERVAL_S;
|
||||
|
||||
// Comprueba la posición de la textura con los titulos de credito
|
||||
if (credits_rect_dst_.y + credits_rect_dst_.h > play_area_.y) {
|
||||
@@ -351,31 +323,12 @@ void Credits::updateTextureDstRects(float deltaTime) {
|
||||
}
|
||||
}
|
||||
|
||||
// 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};
|
||||
|
||||
if (counter_ > ((SETS.size() - 1) * SPEED) * 3) {
|
||||
return;
|
||||
}
|
||||
|
||||
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 (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)
|
||||
constexpr float BALLOON_INTERVAL_S = SPEED / 60.0f; // ~3.33s (cada 200 frames)
|
||||
constexpr float POWERBALL_INTERVAL_S = (SPEED * 4) / 60.0f; // ~13.33s (cada 800 frames)
|
||||
|
||||
if (counter_ > ((SETS.size() - 1) * SPEED) * 3) {
|
||||
return;
|
||||
@@ -387,14 +340,14 @@ void Credits::throwBalloons(float deltaTime) {
|
||||
balloon_accumulator += deltaTime;
|
||||
powerball_accumulator += deltaTime;
|
||||
|
||||
if (balloon_accumulator >= BALLOON_INTERVAL) {
|
||||
balloon_accumulator -= BALLOON_INTERVAL;
|
||||
if (balloon_accumulator >= BALLOON_INTERVAL_S) {
|
||||
balloon_accumulator -= BALLOON_INTERVAL_S;
|
||||
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;
|
||||
if (powerball_accumulator >= POWERBALL_INTERVAL_S && counter_ > 0) {
|
||||
powerball_accumulator -= POWERBALL_INTERVAL_S;
|
||||
balloon_manager_->createPowerBall();
|
||||
}
|
||||
}
|
||||
@@ -466,60 +419,17 @@ void Credits::initPlayers() {
|
||||
}
|
||||
}
|
||||
|
||||
// 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 (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);
|
||||
|
||||
// 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((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((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 {
|
||||
++counter_pre_fade_;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
constexpr float BLACK_RECT_INTERVAL_S = 4.0f / 60.0f; // ~0.067s (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;
|
||||
if (black_rect_accumulator >= BLACK_RECT_INTERVAL_S) {
|
||||
black_rect_accumulator -= BLACK_RECT_INTERVAL_S;
|
||||
|
||||
// Incrementa la altura del rectangulo superior
|
||||
top_black_rect_.h = std::min(top_black_rect_.h + 1, param.game.game_area.center_y - 1);
|
||||
@@ -552,8 +462,8 @@ void Credits::updateBlackRects(float deltaTime) {
|
||||
if (counter_pre_fade_ == 400) {
|
||||
fade_out_->activate();
|
||||
} else {
|
||||
// Convertir deltaTime a factor de frame
|
||||
const float frameFactor = deltaTime / (1000.0f / 60.0f);
|
||||
// Convertir deltaTime a equivalente de frames
|
||||
const float frameFactor = deltaTime * 60.0f;
|
||||
counter_pre_fade_ += frameFactor;
|
||||
}
|
||||
}
|
||||
@@ -568,24 +478,6 @@ void Credits::updateRedRect() {
|
||||
border_rect_.h = bottom_black_rect_.y - border_rect_.y + 1;
|
||||
}
|
||||
|
||||
// Actualiza el estado de fade (frame-based)
|
||||
void Credits::updateAllFades() {
|
||||
if (fading_) {
|
||||
updateBlackRects();
|
||||
updateRedRect();
|
||||
}
|
||||
|
||||
fade_in_->update();
|
||||
if (fade_in_->hasEnded()) {
|
||||
Audio::get()->playMusic("credits.ogg");
|
||||
}
|
||||
|
||||
fade_out_->update();
|
||||
if (fade_out_->hasEnded()) {
|
||||
Section::name = Section::Name::HI_SCORE_TABLE;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el estado de fade (time-based)
|
||||
void Credits::updateAllFades(float deltaTime) {
|
||||
if (fading_) {
|
||||
|
||||
Reference in New Issue
Block a user