feat(ship): la nau entra a HURT al xocar amb un enemic, mor en un segon impacte

This commit is contained in:
2026-05-22 17:30:33 +02:00
parent c6560514d8
commit 87138f9a1f
5 changed files with 81 additions and 7 deletions
+26 -1
View File
@@ -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);
}