arreglada la logica d'estats en Ending2

This commit is contained in:
2025-03-21 13:24:32 +01:00
parent 0f8de0d8b5
commit 428fe664bd
5 changed files with 155 additions and 82 deletions

View File

@@ -17,12 +17,7 @@
#include "utils.h" // Para PaletteColor, stringToColor
// Constructor
Ending2::Ending2()
: counter_enabled_(false),
pre_counter_(0),
post_counter_(0),
post_counter_enabled_(false),
ticks_(0)
Ending2::Ending2() : state_(EndingState::PRE_CREDITS, SDL_GetTicks(), STATE_PRE_CREDITS_DURATION_)
{
options.section.section = Section::ENDING2;
options.section.subsection = Subsection::NONE;
@@ -65,27 +60,32 @@ void Ending2::update()
// Comprueba las entradas
checkInput();
// Actualiza los contadores
updateCounters();
// Actualiza el estado
updateState();
if (counter_enabled_)
switch (state_.state)
{
// Actualiza los sprites
updateSprites();
case EndingState::CREDITS:
// Actualiza los sprites, los textos y los textos del final
for (int i = 0; i < 25; ++i)
{
updateSprites();
updateTextSprites();
updateTexts();
}
break;
// Actualiza los sprites de texto
updateTextSprites();
case EndingState::FADING:
// Actualiza el fade final y el volumen de la música
updateFinalFade();
updateMusicVolume();
break;
// Actualiza los sprites de texto del final
updateTexts();
default:
// No hacer nada si el estado no corresponde a un caso manejado
break;
}
// Actualiza el fade final
updateFinalFade();
// Actualiza el volumen de la musica
updateMusicVolume();
// Actualiza el objeto
Screen::get()->update();
}
@@ -167,28 +167,42 @@ void Ending2::run()
JA_SetVolume(128);
}
// Actualiza los contadores
void Ending2::updateCounters()
// Actualiza el estado
void Ending2::updateState()
{
// Incrementa el contador
if (pre_counter_ < 200)
switch (state_.state)
{
pre_counter_++;
}
else
{
counter_enabled_ = true;
}
case EndingState::PRE_CREDITS:
if (state_.hasEnded(EndingState::PRE_CREDITS))
{
state_.set(EndingState::CREDITS, 0);
}
break;
if (post_counter_enabled_)
{
post_counter_++;
}
case EndingState::CREDITS:
if (texts_.back()->getPosY() <= GAMECANVAS_CENTER_Y)
{
state_.set(EndingState::POST_CREDITS, STATE_POST_CREDITS_DURATION_);
}
break;
if (post_counter_ > 600)
{
options.section.section = Section::LOGO;
options.section.subsection = Subsection::LOGO_TO_INTRO;
case EndingState::POST_CREDITS:
if (state_.hasEnded(EndingState::POST_CREDITS))
{
state_.set(EndingState::FADING, STATE_FADE_DURATION_);
}
break;
case EndingState::FADING:
if (state_.hasEnded(EndingState::FADING))
{
options.section.section = Section::LOGO;
options.section.subsection = Subsection::LOGO_TO_INTRO;
}
break;
default:
break;
}
}
@@ -307,16 +321,9 @@ void Ending2::updateTextSprites()
// Actualiza los sprites de texto del final
void Ending2::updateTexts()
{
if (texts_.back()->getPosY() > GAMECANVAS_CENTER_Y)
for (auto sprite : texts_)
{
for (auto sprite : texts_)
{
sprite->update();
}
}
else
{
post_counter_enabled_ = true;
sprite->update();
}
}
@@ -498,25 +505,34 @@ void Ending2::createTexts()
// Actualiza el fade final
void Ending2::updateFinalFade()
{ /*
// La variable step va de 0 a 40 en el tramo de postCounter que va de 500 a 540. Al dividirlo por 40, va de 0.0f a 1.0f
const float STEP = std::min(std::max(post_counter_, 500) - 500, 40) / 40.0f;
const int INDEX = (colors_.size() - 1) * STEP;
for (const auto &text : texts_)
{
text->getTexture()->setColor(colors_.at(INDEX).r, colors_.at(INDEX).g, colors_.at(INDEX).b);
}
*/
{
for (auto sprite : texts_)
{
sprite->getSurface()->fadeSubPalette(0);
}
}
// Actualiza el volumen de la musica
void Ending2::updateMusicVolume()
{
if (post_counter_ > 0)
{
const float step = (600.0f - post_counter_) / 600.0f;
const int volume = 128 * step;
JA_SetVolume(volume);
}
}
// Constante para la duración en milisegundos
constexpr Uint32 VOLUME_FADE_DURATION = 3000;
// Tiempo actual
const Uint32 CURRENT_TICKS = SDL_GetTicks();
// Calcular el tiempo transcurrido desde init_ticks
Uint32 elapsed_ticks = CURRENT_TICKS - state_.init_ticks;
// Limitar el tiempo máximo a la duración definida
elapsed_ticks = std::min(elapsed_ticks, VOLUME_FADE_DURATION);
// Calcular el step basado en la duración
const float STEP = (static_cast<float>(VOLUME_FADE_DURATION) - elapsed_ticks) / VOLUME_FADE_DURATION;
// Calcular el volumen en función del step
const int VOLUME = static_cast<int>(128 * STEP);
// Actualizar el volumen
JA_SetVolume(VOLUME);
}