58 lines
2.0 KiB
C++
58 lines
2.0 KiB
C++
#include "game/entities/concrete/dust.hpp"
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "game/entities/entity_factory.hpp"
|
|
|
|
namespace {
|
|
|
|
// Llig una animació opcional del bloc `animations` del YAML, amb
|
|
// valors per defecte segurs (1 frame, fps=1, sense loop). Si la
|
|
// clau no existeix, retornem una animació "ja completada" — així,
|
|
// un Dust spawnejat amb una variant inexistent al YAML mor al
|
|
// pròxim frame sense petar.
|
|
auto buildAnim(const fkyaml::node& yaml, const std::string& key) -> AnimationComponent {
|
|
std::vector<int> frames = {0};
|
|
float fps = 1.0F;
|
|
bool loop = false;
|
|
if (yaml.contains("animations") && yaml["animations"].contains(key)) {
|
|
const auto& anim = yaml["animations"][key];
|
|
if (anim.contains("frames")) {
|
|
frames.clear();
|
|
for (const auto& f : anim["frames"]) {
|
|
// cppcheck-suppress useStlAlgorithm
|
|
frames.push_back(f.get_value<int>());
|
|
}
|
|
if (frames.empty()) { frames.push_back(0); }
|
|
}
|
|
if (anim.contains("fps")) { fps = anim["fps"].get_value<float>(); }
|
|
if (anim.contains("loop")) { loop = anim["loop"].get_value<bool>(); }
|
|
}
|
|
return {frames, fps, loop};
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Dust::Dust(const fkyaml::node& yaml)
|
|
: anim_jump_(buildAnim(yaml, "jump")),
|
|
anim_land_(buildAnim(yaml, "land")) {
|
|
name_ = yaml.contains("name") ? yaml["name"].get_value<std::string>() : std::string("dust");
|
|
collides_ = false;
|
|
interactable_ = false;
|
|
// Per defecte JUMP; el spawner pot canviar-la abans del primer tick.
|
|
anim_ = anim_jump_;
|
|
}
|
|
|
|
void Dust::setVariant(Variant v) {
|
|
anim_ = (v == Variant::JUMP) ? anim_jump_ : anim_land_;
|
|
if (anim_.has_value()) { anim_->reset(); }
|
|
}
|
|
|
|
void Dust::update(float delta_time) {
|
|
Entity::update(delta_time);
|
|
if (anim_.has_value() && anim_->completed()) { markDead(); }
|
|
}
|
|
|
|
REGISTER_ENTITY("dust", Dust)
|