refactor(entities): unifica loader d'animacio via AnimationComponent::fromYaml i elimina fps:

This commit is contained in:
JailDesigner committed 2026-05-13 10:06:56 +02:00
1 parent a779533d61
commit b76a3eb1f3
21 files changed
+306 -231

No files matched your search

+91 -4
View File
@@ -242,11 +242,12 @@ namespace {
void testAnimationComponent() {
std::cout << "[T6] animation component\n";
AnimationComponent anim({10, 20, 30}, 10.0F, true);
// 100 ms per frame: a tick(0.15) avança 1 frame; tick(0.10) un altre.
AnimationComponent anim = AnimationComponent::uniform({10, 20, 30}, 100.0F, true);
REQUIRE(anim.frameCount() == 3);
REQUIRE(anim.currentFrame() == 10);
anim.tick(0.15F); // 1.5 frames de 10 fps → hauria d'avançar a l'índex 1
anim.tick(0.15F); // 1.5 frames de 100 ms → hauria d'avançar a l'índex 1
REQUIRE(anim.currentFrame() == 20);
anim.tick(0.10F);
@@ -257,16 +258,101 @@ namespace {
REQUIRE(anim.currentFrame() == 10);
// Variant no-loop: es para al darrer i queda completed.
AnimationComponent once({1, 2}, 10.0F, false);
AnimationComponent once = AnimationComponent::uniform({1, 2}, 100.0F, false);
once.tick(1.0F); // molts frames; ha d'arribar al final i quedar-se.
REQUIRE(once.currentFrame() == 2);
REQUIRE(once.completed());
// Variant d'1 frame no-loop: completed des del principi.
AnimationComponent single({5}, 1.0F, false);
AnimationComponent single = AnimationComponent::uniform({5}, 1000.0F, false);
REQUIRE(single.completed());
}
// Cobertura específica de `AnimationComponent::fromYaml`: format
// `frame_ms` escalar i llista per-frame, gestió de `fps:` (rebutjat),
// i defaults quan la clau no existeix.
void testAnimationFromYaml() {
std::cout << "[T6b] animation fromYaml\n";
// 1) Format escalar `frame_ms`: tots els frames duren el mateix.
{
const std::string YAML = R"(
animations:
walk: { frames: [0, 1, 2, 3], frame_ms: 100, loop: true }
)";
auto node = fkyaml::node::deserialize(YAML);
auto anim = AnimationComponent::fromYaml(node, "walk");
REQUIRE(anim.frameCount() == 4);
REQUIRE(anim.currentFrame() == 0);
anim.tick(0.10F);
REQUIRE(anim.currentFrame() == 1);
}
// 2) Format llista `frame_ms`: el darrer frame aguanta més.
{
const std::string YAML = R"(
animations:
raise:
frames: [10, 11, 12]
frame_ms: [100, 100, 500]
loop: false
)";
auto node = fkyaml::node::deserialize(YAML);
auto anim = AnimationComponent::fromYaml(node, "raise");
REQUIRE(anim.frameCount() == 3);
anim.tick(0.10F);
REQUIRE(anim.currentFrame() == 11);
anim.tick(0.10F);
REQUIRE(anim.currentFrame() == 12);
// El darrer frame dura 500 ms: tick de 400 ms no completa l'anim.
anim.tick(0.40F);
REQUIRE(!anim.completed());
// 100 ms més (total 500 ms al frame 12) sí la completa.
anim.tick(0.10F);
REQUIRE(anim.completed());
}
// 3) Clau no existent: defaults segurs (1 frame loop, no peta).
{
const std::string YAML = R"(
animations:
walk: { frames: [0], frame_ms: 100, loop: true }
)";
auto node = fkyaml::node::deserialize(YAML);
auto anim = AnimationComponent::fromYaml(node, "missing");
REQUIRE(anim.frameCount() == 1);
REQUIRE(anim.loops());
}
// 4) `fps:` no és suportat: ha de llençar excepció.
{
const std::string YAML = R"(
animations:
walk: { frames: [0, 1], fps: 8, loop: true }
)";
auto node = fkyaml::node::deserialize(YAML);
bool threw = false;
try {
(void) AnimationComponent::fromYaml(node, "walk");
} catch (const std::runtime_error&) {
threw = true;
}
REQUIRE(threw);
}
// 5) Flip_h al YAML es preserva al component.
{
const std::string YAML = R"(
animations:
west: { frames: [0, 1], frame_ms: 100, loop: true, flip_h: true }
)";
auto node = fkyaml::node::deserialize(YAML);
auto anim = AnimationComponent::fromYaml(node, "west");
REQUIRE(anim.flipH());
REQUIRE(anim.flipMode() == SDL_FLIP_HORIZONTAL);
}
}
void testAllTemplatesLoad() {
std::cout << "[T7] plantilles YAML\n";
const std::vector<std::string> TYPES = {
@@ -697,6 +783,7 @@ auto main() -> int {
testSarcophagusPerimeter();
testEntityManagerEndToEnd();
testAnimationComponent();
testAnimationFromYaml();
testAllTemplatesLoad();
testDecorationFlags();
testLoadFromLevel();