console: update() en 4 sub-passos (cursor, typewriter, resize, open/close)

This commit is contained in:
2026-05-17 13:51:12 +02:00
parent 4e9d7e1450
commit 01e61767dc
2 changed files with 69 additions and 56 deletions
+30 -23
View File
@@ -167,13 +167,8 @@ void Console::redrawText() {
} }
// Actualiza la animación de la consola // Actualiza la animación de la consola
void Console::update(float delta_time) { // NOLINT(readability-function-cognitive-complexity) // Parpadeig del cursor (només quan ACTIVE)
if (status_ == Status::HIDDEN) { void Console::updateCursorBlink(float delta_time) {
return;
}
// Parpadeo del cursor (solo cuando activa)
if (status_ == Status::ACTIVE) {
cursor_timer_ += delta_time; cursor_timer_ += delta_time;
const float THRESHOLD = cursor_visible_ ? CURSOR_ON_TIME : CURSOR_OFF_TIME; const float THRESHOLD = cursor_visible_ ? CURSOR_ON_TIME : CURSOR_OFF_TIME;
if (cursor_timer_ >= THRESHOLD) { if (cursor_timer_ >= THRESHOLD) {
@@ -182,22 +177,21 @@ void Console::update(float delta_time) { // NOLINT(readability-function-cogniti
} }
} }
// Efecto typewriter: revelar letras una a una (solo cuando ACTIVE) // Revelat lletra a lletra de msg_lines_ (només quan ACTIVE)
if (status_ == Status::ACTIVE) { void Console::updateTypewriter(float delta_time) {
const int TOTAL_CHARS = std::accumulate(msg_lines_.begin(), msg_lines_.end(), 0, [](int acc, const auto& line) { return acc + static_cast<int>(line.size()); }); const int TOTAL_CHARS = std::accumulate(msg_lines_.begin(), msg_lines_.end(), 0, [](int acc, const auto& line) { return acc + static_cast<int>(line.size()); });
if (typewriter_chars_ < TOTAL_CHARS) { if (typewriter_chars_ >= TOTAL_CHARS) { return; }
typewriter_timer_ += delta_time; typewriter_timer_ += delta_time;
while (typewriter_timer_ >= TYPEWRITER_CHAR_DELAY && typewriter_chars_ < TOTAL_CHARS) { while (typewriter_timer_ >= TYPEWRITER_CHAR_DELAY && typewriter_chars_ < TOTAL_CHARS) {
typewriter_timer_ -= TYPEWRITER_CHAR_DELAY; typewriter_timer_ -= TYPEWRITER_CHAR_DELAY;
++typewriter_chars_; ++typewriter_chars_;
} }
} }
}
// Animación de altura (resize cuando msg_lines_ cambia); solo en ACTIVE // Animació d'altura quan msg_lines_ canvia (només quan ACTIVE i height_ != target_height_)
if (status_ == Status::ACTIVE && height_ != target_height_) { void Console::updateResizeAnimation(float delta_time) {
if (anim_progress_ == 0.0F) { if (anim_progress_ == 0.0F) {
// Iniciar animación de resize // Iniciar animació de resize
anim_start_ = height_; anim_start_ = height_;
anim_end_ = target_height_; anim_end_ = target_height_;
} }
@@ -207,34 +201,47 @@ void Console::update(float delta_time) { // NOLINT(readability-function-cogniti
height_ = target_height_; height_ = target_height_;
anim_progress_ = 0.0F; anim_progress_ = 0.0F;
} }
// Reconstruir la Surface al nuevo tamaño (pequeña: 256×~18-72px) // Reconstruir la Surface al nou tamany (xicoteta: 256×~18-72px)
const float WIDTH = Options::game.width; const float WIDTH = Options::game.width;
surface_ = std::make_shared<Surface>(WIDTH, height_); surface_ = std::make_shared<Surface>(WIDTH, height_);
sprite_->setSurface(surface_); sprite_->setSurface(surface_);
} }
// Redibujar texto cada frame // Animació RISING/VANISHING (basada en temps amb easing)
redrawText(); void Console::updateOpenCloseAnimation(float delta_time) {
// Animación de apertura/cierre (basada en tiempo con easing)
if (status_ == Status::RISING || status_ == Status::VANISHING) {
anim_progress_ = std::min(anim_progress_ + (delta_time / ANIM_DURATION), 1.0F); anim_progress_ = std::min(anim_progress_ + (delta_time / ANIM_DURATION), 1.0F);
y_ = anim_start_ + ((anim_end_ - anim_start_) * Easing::cubicInOut(anim_progress_)); y_ = anim_start_ + ((anim_end_ - anim_start_) * Easing::cubicInOut(anim_progress_));
if (anim_progress_ >= 1.0F) { if (anim_progress_ < 1.0F) { return; }
y_ = anim_end_; y_ = anim_end_;
anim_progress_ = 0.0F; anim_progress_ = 0.0F;
if (status_ == Status::RISING) { if (status_ == Status::RISING) {
status_ = Status::ACTIVE; status_ = Status::ACTIVE;
} else { return;
}
status_ = Status::HIDDEN; status_ = Status::HIDDEN;
// Resetear el mensaje una vez completamente oculta // Resetear el missatge una vegada completament oculta
msg_lines_ = {std::string(CONSOLE_NAME) + " " + std::string(CONSOLE_VERSION)}; msg_lines_ = {std::string(CONSOLE_NAME) + " " + std::string(CONSOLE_VERSION)};
target_height_ = calcTargetHeight(static_cast<int>(msg_lines_.size())); target_height_ = calcTargetHeight(static_cast<int>(msg_lines_.size()));
} }
void Console::update(float delta_time) {
if (status_ == Status::HIDDEN) { return; }
if (status_ == Status::ACTIVE) {
updateCursorBlink(delta_time);
updateTypewriter(delta_time);
if (height_ != target_height_) {
updateResizeAnimation(delta_time);
} }
} }
redrawText();
if (status_ == Status::RISING || status_ == Status::VANISHING) {
updateOpenCloseAnimation(delta_time);
}
SDL_FRect rect = {.x = 0, .y = y_, .w = Options::game.width, .h = height_}; SDL_FRect rect = {.x = 0, .y = y_, .w = Options::game.width, .h = height_};
sprite_->setPosition(rect); sprite_->setPosition(rect);
sprite_->setClip({.x = 0.0F, .y = 0.0F, .w = Options::game.width, .h = height_}); sprite_->setClip({.x = 0.0F, .y = 0.0F, .w = Options::game.width, .h = height_});
+6
View File
@@ -79,6 +79,12 @@ class Console {
void processCommand(); // Procesa el comando introducido por el usuario void processCommand(); // Procesa el comando introducido por el usuario
[[nodiscard]] auto wrapText(const std::string& text) const -> std::vector<std::string>; // Word-wrap por ancho en píxeles [[nodiscard]] auto wrapText(const std::string& text) const -> std::vector<std::string>; // Word-wrap por ancho en píxeles
// Sub-pasos de update() (extrets per reduir complexitat cognitiva)
void updateCursorBlink(float delta_time); // Parpadeig del cursor
void updateTypewriter(float delta_time); // Revelat lletra a lletra de msg_lines_
void updateResizeAnimation(float delta_time); // Animació d'altura quan msg_lines_ canvia
void updateOpenCloseAnimation(float delta_time); // Animació RISING/VANISHING
// Objetos de renderizado // Objetos de renderizado
std::shared_ptr<Text> text_; std::shared_ptr<Text> text_;
std::shared_ptr<Surface> surface_; std::shared_ptr<Surface> surface_;