Files
JailDesignerandClaude Opus 4.7 5fce52e0ad refactor(log): migra game/scenes i game/ui a Log::
- logo_scene, title_scene, intro_scene, room_fsm: missatges de
  cicle d'escena com a Log::info amb tag "scene" o "RoomFsm".
  Eliminats els TODO obsolets sobre el logger pendent.
- game_scene (16 logs): events del bus (LEVEL COMPLETE, PLAYER HIT,
  PLAYER KILLED) com a info; transicions de ROOM com a info; warns
  als casos d'àudio absent o música no arrencada.
- hud (3 logs): init i events com a info.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 09:24:11 +02:00

178 lines
7.9 KiB
C++

#include "game/scenes/room_fsm.hpp"
#include <array>
#include <string_view>
#include "core/locale/locale.hpp"
#include "core/resources/resource_helper.hpp"
#include "external/fkyaml_node.hpp"
#include "utils/log.hpp"
namespace {
// Llegeix un sub-camp sencer d'un node YAML. Si manca, retorna el
// fallback. Dissenyat per al consum puntual de `loadConfig`.
auto readChildInt(const fkyaml::node& parent, const char* key, int fallback) -> int {
if (!parent.contains(key)) { return fallback; }
return parent[key].get_value<int>();
}
} // namespace
namespace RoomFsm {
auto loadConfig(const std::string& yaml_path) -> Config {
Config cfg{};
cfg.text_enter_curve = UiConfig::curveByName("outCubic");
cfg.text_exit_curve = UiConfig::curveByName("inCubic");
auto bytes = Resource::Helper::loadFile(yaml_path);
if (bytes.empty()) {
Log::warn("RoomFsm", "{} absent — uso fallbacks compilats", yaml_path);
return cfg;
}
const auto* begin = reinterpret_cast<const char*>(bytes.data());
const auto* end = begin + bytes.size();
auto yaml = fkyaml::node::deserialize(begin, end);
if (yaml.contains("room_intro")) {
const auto& n = yaml["room_intro"];
cfg.intro_fade_in_ms = readChildInt(n, "fade_in_ms", cfg.intro_fade_in_ms);
cfg.intro_text_delay_ms = readChildInt(n, "text_delay_ms", cfg.intro_text_delay_ms);
cfg.intro_text_enter_ms = readChildInt(n, "text_enter_ms", cfg.intro_text_enter_ms);
cfg.intro_text_hold_ms = readChildInt(n, "text_hold_ms", cfg.intro_text_hold_ms);
cfg.intro_text_exit_ms = readChildInt(n, "text_exit_ms", cfg.intro_text_exit_ms);
cfg.player_spawn_delay_ms = readChildInt(n, "player_spawn_delay_ms", cfg.player_spawn_delay_ms);
// Per defecte el segon jugador cau alhora que el primer.
// El YAML pot trencar la simultaneïtat fixant un valor propi.
cfg.player2_spawn_delay_ms = readChildInt(n, "player2_spawn_delay_ms", cfg.player_spawn_delay_ms);
cfg.intro_text_line_gap_px = readChildInt(n, "line_gap_px", cfg.intro_text_line_gap_px);
if (n.contains("land_reverb")) {
cfg.intro_land_reverb_preset = n["land_reverb"].get_value<std::string>();
}
}
if (yaml.contains("room_outro")) {
const auto& n = yaml["room_outro"];
cfg.outro_text_enter_ms = readChildInt(n, "text_enter_ms", cfg.outro_text_enter_ms);
cfg.outro_text_hold_ms = readChildInt(n, "text_hold_ms", cfg.outro_text_hold_ms);
cfg.outro_text_exit_ms = readChildInt(n, "text_exit_ms", cfg.outro_text_exit_ms);
cfg.outro_fade_out_ms = readChildInt(n, "fade_out_ms", cfg.outro_fade_out_ms);
cfg.outro_text_line_gap_px = readChildInt(n, "line_gap_px", cfg.outro_text_line_gap_px);
cfg.outro_black_hold_ms = readChildInt(n, "black_hold_ms", cfg.outro_black_hold_ms);
}
if (yaml.contains("text")) {
const auto& n = yaml["text"];
if (n.contains("enter_curve")) {
cfg.text_enter_curve = UiConfig::curveByName(n["enter_curve"].get_value<std::string>());
}
if (n.contains("exit_curve")) {
cfg.text_exit_curve = UiConfig::curveByName(n["exit_curve"].get_value<std::string>());
}
}
if (yaml.contains("text_panel")) {
const auto& n = yaml["text_panel"];
if (n.contains("color") && n["color"].is_sequence() && n["color"].size() == 3) {
const auto R = n["color"][0].get_value<std::uint32_t>() & 0xFFU;
const auto G = n["color"][1].get_value<std::uint32_t>() & 0xFFU;
const auto B = n["color"][2].get_value<std::uint32_t>() & 0xFFU;
cfg.text_panel_color = (R << 16) | (G << 8) | B;
}
if (n.contains("alpha")) {
cfg.text_panel_alpha = static_cast<std::uint8_t>(n["alpha"].get_value<std::uint32_t>() & 0xFFU);
}
cfg.text_panel_height_px = readChildInt(n, "height_px", cfg.text_panel_height_px);
}
Log::info("RoomFsm", "timings carregats des de {} (intro fade {}ms, outro fade {}ms)", yaml_path, cfg.intro_fade_in_ms, cfg.outro_fade_out_ms);
return cfg;
}
auto advance(State current, Transition& t, const Config& cfg, int delta_ms) -> State {
if (current == State::PLAYING) { return current; }
t.elapsed_ms += delta_ms;
if (current == State::ROOM_INTRO) {
const std::array<int, 6> DURS = {
cfg.intro_fade_in_ms,
cfg.intro_text_delay_ms,
cfg.intro_text_enter_ms,
cfg.intro_text_hold_ms,
cfg.intro_text_exit_ms,
// Cua silenciosa fins al final de room_intro.ogg. La
// calcula GameScene després de `playMusic`. Si la música
// no arrenca, és 0 i la fase es consumeix sense esperar.
cfg.intro_music_tail_ms,
};
while (t.phase < static_cast<int>(DURS.size()) && t.elapsed_ms >= DURS[t.phase]) {
t.elapsed_ms -= DURS[t.phase];
++t.phase;
}
if (t.phase >= static_cast<int>(DURS.size())) { return State::PLAYING; }
return current;
}
// ROOM_OUTRO
const std::array<int, 6> DURS = {
cfg.outro_text_enter_ms,
cfg.outro_text_hold_ms,
cfg.outro_text_exit_ms,
// Cua silenciosa fins al final de room_outro.ogg, abans
// d'arrencar el fade-out. Així el negre cau just al final
// de la pista; sense cua si la música no arrenca.
cfg.outro_music_tail_ms,
cfg.outro_fade_out_ms,
// Pausa final en negre, després del fade-out i abans del
// canvi a INTRO. Si és 0 ms, el while-loop la consumeix
// sense esperar — comportament idèntic a l'històric.
cfg.outro_black_hold_ms,
};
while (t.phase < static_cast<int>(DURS.size()) && t.elapsed_ms >= DURS[t.phase]) {
t.elapsed_ms -= DURS[t.phase];
++t.phase;
}
// Final de l'OUTRO → ROOM_INTRO de la sala SEGÜENT. La FSM no
// sap quina és — només avisa del canvi d'estat. La GameScene,
// dins `setState(ROOM_INTRO)` amb PREV=OUTRO, demana al
// LevelManager el següent nivell i recarrega tilemap/entitats.
if (t.phase >= static_cast<int>(DURS.size())) { return State::ROOM_INTRO; }
return current;
}
namespace {
// Substitueix la primera ocurrència de `placeholder` dins de `tpl`
// pel valor d'`n` convertit a string. Utilitzat per les dues
// formatadores d'INTRO; si la clau del Locale no porta el
// placeholder (traducció buida o ja substituïda), retorna la
// plantilla intacta.
auto substitute(std::string tpl, std::string_view placeholder, int n) -> std::string {
auto pos = tpl.find(placeholder);
if (pos != std::string::npos) {
tpl.replace(pos, placeholder.size(), std::to_string(n));
}
return tpl;
}
} // namespace
auto formatIntroPyramidLine(int pyramid) -> std::string {
return substitute(Locale::get().text("game.room_intro_pyramid"), "{p}", pyramid);
}
auto formatIntroRoomLine(int room) -> std::string {
return substitute(Locale::get().text("game.room_intro_room"), "{r}", room);
}
auto stateName(State s) -> const char* {
switch (s) {
case State::ROOM_INTRO:
return "ROOM_INTRO";
case State::PLAYING:
return "PLAYING";
case State::ROOM_OUTRO:
return "ROOM_OUTRO";
}
return "?";
}
} // namespace RoomFsm