feat(ship): la nau entra a HURT al xocar amb un enemic, mor en un segon impacte
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
|
||||
#include "core/audio/audio.hpp"
|
||||
#include "core/defaults.hpp"
|
||||
#include "core/entities/entity.hpp"
|
||||
#include "core/graphics/shape_loader.hpp"
|
||||
@@ -62,6 +63,8 @@ void Ship::init(const Vec2* spawn_point, bool activar_invulnerabilitat) {
|
||||
// Activar invulnerabilidad solo si es respawn
|
||||
invulnerable_timer_ = activar_invulnerabilitat ? Defaults::Ship::INVULNERABILITY_DURATION : 0.0F;
|
||||
is_hit_ = false;
|
||||
hurt_timer_ = 0.0F;
|
||||
touching_enemy_prev_frame_ = false;
|
||||
}
|
||||
|
||||
void Ship::processInput(float delta_time, uint8_t player_id) {
|
||||
@@ -115,6 +118,12 @@ void Ship::update(float delta_time) {
|
||||
invulnerable_timer_ = std::max(invulnerable_timer_, 0.0F);
|
||||
}
|
||||
|
||||
// Decrementar timer d'estat HURT (a 0 → torna a normal sense efecte extern)
|
||||
if (hurt_timer_ > 0.0F) {
|
||||
hurt_timer_ -= delta_time;
|
||||
hurt_timer_ = std::max(hurt_timer_, 0.0F);
|
||||
}
|
||||
|
||||
// El movimiento real lo hace PhysicsWorld::update().
|
||||
// Aquí solo lógica de estado.
|
||||
|
||||
@@ -157,5 +166,21 @@ void Ship::draw() const {
|
||||
const float VISUAL_PUSH = SPEED / Defaults::Ship::VISUAL_PUSH_DIVISOR;
|
||||
const float SCALE = 1.0F + (VISUAL_PUSH / Defaults::Ship::VISUAL_SCALE_DIVISOR);
|
||||
|
||||
Rendering::renderShape(renderer_, shape_, center_, angle_, SCALE, 1.0F, brightness_, Defaults::Palette::SHIP);
|
||||
// Parpelleig daurat mentre està ferida: alterna color normal ↔ color hurt
|
||||
// a Hurt::BLINK_HZ (mateixa estètica que el wounded dels enemics).
|
||||
SDL_Color color = color_normal_;
|
||||
if (hurt_timer_ > 0.0F) {
|
||||
const float CYCLE = 1.0F / Defaults::Ship::Hurt::BLINK_HZ;
|
||||
const float T = std::fmod(hurt_timer_, CYCLE);
|
||||
if (T < (CYCLE / 2.0F)) {
|
||||
color = color_hurt_;
|
||||
}
|
||||
}
|
||||
|
||||
Rendering::renderShape(renderer_, shape_, center_, angle_, SCALE, 1.0F, brightness_, color);
|
||||
}
|
||||
|
||||
void Ship::herir() {
|
||||
hurt_timer_ = Defaults::Ship::Hurt::DURATION;
|
||||
Audio::get()->playSound(Defaults::Sound::HIT, Audio::Group::GAME);
|
||||
}
|
||||
|
||||
@@ -53,10 +53,31 @@ class Ship : public Entities::Entity {
|
||||
body_.velocity = Vec2{}; // Detener al morir
|
||||
}
|
||||
|
||||
// Estat "ferit": primera col·lisió amb enemic dispara HURT; segona durant HURT mata.
|
||||
void herir();
|
||||
[[nodiscard]] auto isHurt() const -> bool { return hurt_timer_ > 0.0F; }
|
||||
[[nodiscard]] auto getHurtTimer() const -> float { return hurt_timer_; }
|
||||
|
||||
// Edge-trigger del contacte amb enemics: un impacte només compta a la transició
|
||||
// no-tocant → tocant. Sense açò, el contacte continu durant el rebot frame-a-frame
|
||||
// dispararia HURT i mort en frames consecutius.
|
||||
[[nodiscard]] auto wasTouchingEnemyPrevFrame() const -> bool { return touching_enemy_prev_frame_; }
|
||||
void setTouchingEnemyPrevFrame(bool touching) { touching_enemy_prev_frame_ = touching; }
|
||||
|
||||
private:
|
||||
// Miembros específicos de Ship (heredados: renderer_, shape_, center_, angle_, brightness_, body_).
|
||||
// Inicializados en la declaración: el ctor por defecto deja la nave "viva y sin invulnerabilidad",
|
||||
// que es el estado coherente al que llevan tanto init() como el ctor con renderer.
|
||||
bool is_hit_{false};
|
||||
float invulnerable_timer_{0.0F}; // 0.0f = vulnerable, >0.0f = invulnerable
|
||||
|
||||
// Colors de la nau (propietats, prep per migració a YAML).
|
||||
SDL_Color color_normal_{Defaults::Palette::SHIP};
|
||||
SDL_Color color_hurt_{Defaults::Palette::WOUNDED};
|
||||
|
||||
// >0 → estat HURT (parpelleig color_normal_ ↔ color_hurt_).
|
||||
float hurt_timer_{0.0F};
|
||||
|
||||
// Edge-trigger: true si el frame anterior la nau ja estava en contacte amb un enemic.
|
||||
bool touching_enemy_prev_frame_{false};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user