feat(immunity): API setImmunity/isImmune i lectura del bloc immunity

This commit is contained in:
JailDesigner committed 2026-05-05 13:01:22 +02:00
1 parent 3985e66b94
commit 2e3816783f
3 files changed
+90

No files matched your search

+28
View File
@@ -14,6 +14,8 @@
#include "core/input/input_state.hpp"
#include "core/rendering/canvas.hpp"
#include "core/rendering/screen.hpp"
#include "core/rendering/surface.hpp"
#include "core/resources/palette_loader.hpp"
#include "core/resources/resource_cache.hpp"
#include "core/resources/resource_helper.hpp"
#include "external/fkyaml_node.hpp"
@@ -75,6 +77,7 @@ Player::JumpConfig Player::jump_config;
Player::HurtConfig Player::hurt_config;
Player::WhipConfig Player::whip_config;
Player::DeathConfig Player::death_config;
Player::ImmunityConfig Player::immunity_config;
void Player::loadFrom(const std::string& yaml_path) {
auto bytes = Resource::Helper::loadFile(yaml_path);
@@ -131,6 +134,11 @@ void Player::loadFrom(const std::string& yaml_path) {
// vides. Si manca, el respawn és immediat i les vides queden a 3
// per defecte (no és un valor crític: avui no hi ha game over).
readDeathBlock(yaml);
// Bloc opcional `immunity:` — finestra de gràcia post-spawn. Si
// `duration_ms` és 0 (o el bloc manca) la mecànica queda
// desactivada i els hazards toquen al primer frame, com abans.
readImmunityBlock(yaml);
}
void Player::readCollisionBlock(const fkyaml::node& yaml, const std::string& yaml_path) {
@@ -205,6 +213,20 @@ void Player::readDeathBlock(const fkyaml::node& yaml) {
if (d.contains("lives")) { death_config.initial_lives = d["lives"].get_value<int>(); }
}
void Player::readImmunityBlock(const fkyaml::node& yaml) {
immunity_config = ImmunityConfig{};
if (!yaml.contains("immunity")) { return; }
const auto& im = yaml["immunity"];
if (im.contains("duration_ms")) { immunity_config.duration_ms = im["duration_ms"].get_value<float>(); }
if (im.contains("blink_period_ms")) { immunity_config.blink_period_ms = im["blink_period_ms"].get_value<int>(); }
// Paleta del blink: el basename val tant de clau al Resource::Cache
// com de path (`data/player/<key>.pal`). Mateix patró que els sons.
if (im.contains("palette")) {
immunity_config.palette_key = im["palette"].get_value<std::string>();
Resource::Cache::get().loadPalette(immunity_config.palette_key, "data/player/" + immunity_config.palette_key + ".pal");
}
}
void Player::readWhipBlock(const fkyaml::node& yaml) {
whip_config = WhipConfig{};
if (!yaml.contains("whip")) { return; }
@@ -372,6 +394,12 @@ Player::Player(const std::string& anim_key, int id, float x, float y, const Cont
// GameScene el reescriu via `setSpawnPoint` quan canvia de sala.
spawn_x_ = x;
spawn_y_ = y;
// Cachegem la paleta normal del sprite ara que la Surface ja està
// carregada. El render del blink d'immunitat la restaura després
// d'aplicar la paleta plana — fer-ho via membre evita dependre de
// l'estat momentani de la Surface (que pot ser compartida entre
// jugadors del mateix personatge).
if (surface_ != nullptr) { normal_palette_ = surface_->getPalette(); }
}
void Player::update(float delta_time) {