refactor: consolidar Ship::isAlive/isHit/isActive en isActive()

Els tres mètodes retornaven el mateix bool a partir d'is_hit_:
  isActive() = !is_hit_   (override de Entity)
  isAlive()  = !is_hit_
  isHit()    =  is_hit_

Eren tres formes diferents de preguntar el mateix, repartides sense
criteri pels call-sites (collision_system, game_scene). Conservem
isActive() perquè és l'override polimòrfic d'Entity i esborrem els
altres dos. Actualitzats els 5 call-sites externs.

Hallazgo #11 de CODE_REVIEW.md.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-20 17:37:37 +02:00
parent e1d6cd1bb9
commit 97c98272c9
3 changed files with 167 additions and 164 deletions
+41 -43
View File
@@ -11,54 +11,52 @@
#include "core/types.hpp" #include "core/types.hpp"
class Ship : public Entities::Entity { class Ship : public Entities::Entity {
public: public:
Ship() Ship()
: Entity(nullptr) {} : Entity(nullptr) {}
explicit Ship(Rendering::Renderer* renderer, const char* shape_file = "ship.shp"); explicit Ship(Rendering::Renderer* renderer, const char* shape_file = "ship.shp");
void init() override { init(nullptr, false); } void init() override { init(nullptr, false); }
void init(const Vec2* spawn_point, bool activar_invulnerabilitat = false); void init(const Vec2* spawn_point, bool activar_invulnerabilitat = false);
void processInput(float delta_time, uint8_t player_id); void processInput(float delta_time, uint8_t player_id);
void update(float delta_time) override; void update(float delta_time) override;
void postUpdate(float delta_time) override; void postUpdate(float delta_time) override;
void draw() const override; void draw() const override;
// Override: Interfaz de Entity // Override: Interfaz de Entity
[[nodiscard]] auto isActive() const -> bool override { return !is_hit_; } [[nodiscard]] auto isActive() const -> bool override { return !is_hit_; }
// Override: Interfaz de colisión // Override: Interfaz de colisión
[[nodiscard]] auto getCollisionRadius() const -> float override { [[nodiscard]] auto getCollisionRadius() const -> float override {
return Defaults::Entities::SHIP_RADIUS; return Defaults::Entities::SHIP_RADIUS;
} }
[[nodiscard]] auto isCollidable() const -> bool override { [[nodiscard]] auto isCollidable() const -> bool override {
return !is_hit_ && invulnerable_timer_ <= 0.0F; return !is_hit_ && invulnerable_timer_ <= 0.0F;
} }
// Getters (API pública sin cambios) // Getters
[[nodiscard]] auto isAlive() const -> bool { return !is_hit_; } [[nodiscard]] auto isInvulnerable() const -> bool { return invulnerable_timer_ > 0.0F; }
[[nodiscard]] auto isHit() const -> bool { return is_hit_; } // Velocidad como vector cartesiano (ahora viene directa del body_).
[[nodiscard]] auto isInvulnerable() const -> bool { return invulnerable_timer_ > 0.0F; } [[nodiscard]] auto getVelocityVector() const -> Vec2 { return body_.velocity; }
// Velocidad como vector cartesiano (ahora viene directa del body_). // Velocidad escalar (utilidad para draw y debugging).
[[nodiscard]] auto getVelocityVector() const -> Vec2 { return body_.velocity; } [[nodiscard]] auto getSpeed() const -> float { return body_.velocity.length(); }
// Velocidad escalar (utilidad para draw y debugging).
[[nodiscard]] auto getSpeed() const -> float { return body_.velocity.length(); }
// Setters // Setters
void setCenter(const Vec2& nou_centre) { void setCenter(const Vec2& nou_centre) {
center_ = nou_centre; center_ = nou_centre;
body_.position = nou_centre; body_.position = nou_centre;
} }
// Colisiones // Colisiones
void markHit() { void markHit() {
is_hit_ = true; is_hit_ = true;
body_.velocity = Vec2{}; // Detener al morir body_.velocity = Vec2{}; // Detener al morir
} }
private: private:
// Miembros específicos de Ship (heredados: renderer_, shape_, center_, angle_, brightness_, body_). // 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", // 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. // que es el estado coherente al que llevan tanto init() como el ctor con renderer.
bool is_hit_{false}; bool is_hit_{false};
float invulnerable_timer_{0.0F}; // 0.0f = vulnerable, >0.0f = invulnerable float invulnerable_timer_{0.0F}; // 0.0f = vulnerable, >0.0f = invulnerable
}; };
+3 -3
View File
@@ -573,11 +573,11 @@ void GameScene::drawInitHudState() {
Systems::InitHud::drawScoreboardAnimated(text_, buildScoreboard(), score_progress); Systems::InitHud::drawScoreboardAnimated(text_, buildScoreboard(), score_progress);
} }
if (ship1_progress > 0.0F && match_config_.jugador1_actiu && !ships_[0].isHit()) { if (ship1_progress > 0.0F && match_config_.jugador1_actiu && ships_[0].isActive()) {
ships_[0].draw(); ships_[0].draw();
} }
if (ship2_progress > 0.0F && match_config_.jugador2_actiu && !ships_[1].isHit()) { if (ship2_progress > 0.0F && match_config_.jugador2_actiu && ships_[1].isActive()) {
ships_[1].draw(); ships_[1].draw();
} }
} }
@@ -820,7 +820,7 @@ void GameScene::fireBullet(uint8_t player_id) {
if (hit_timer_per_player_[player_id] > 0.0F) { if (hit_timer_per_player_[player_id] > 0.0F) {
return; return;
} }
if (!ships_[player_id].isAlive()) { if (!ships_[player_id].isActive()) {
return; return;
} }
+123 -118
View File
@@ -10,141 +10,146 @@
namespace Systems::Collision { namespace Systems::Collision {
void detectBulletEnemy(Context& ctx) { void detectBulletEnemy(Context& ctx) {
constexpr float AMPLIFIER = Defaults::Game::COLLISION_BULLET_ENEMY_AMPLIFIER; constexpr float AMPLIFIER = Defaults::Game::COLLISION_BULLET_ENEMY_AMPLIFIER;
constexpr float VELOCITAT_EXPLOSIO = 80.0F; // px/s (explosión suau) constexpr float VELOCITAT_EXPLOSIO = 80.0F; // px/s (explosión suau)
for (auto& bullet : ctx.bullets) { for (auto& bullet : ctx.bullets) {
for (auto& enemy : ctx.enemies) { for (auto& enemy : ctx.enemies) {
if (!Physics::checkCollision(bullet, enemy, AMPLIFIER)) { if (!Physics::checkCollision(bullet, enemy, AMPLIFIER)) {
continue; continue;
} }
// *** COLISIÓN bullet → enemy *** // *** COLISIÓN bullet → enemy ***
const Vec2& enemy_pos = enemy.getCenter(); const Vec2& enemy_pos = enemy.getCenter();
// 1. Puntos según tipo // 1. Puntos según tipo
int points = 0; int points = 0;
switch (enemy.getType()) { switch (enemy.getType()) {
case EnemyType::PENTAGON: case EnemyType::PENTAGON:
points = Defaults::Enemies::Scoring::PENTAGON_SCORE; points = Defaults::Enemies::Scoring::PENTAGON_SCORE;
break; break;
case EnemyType::QUADRAT: case EnemyType::QUADRAT:
points = Defaults::Enemies::Scoring::QUADRAT_SCORE; points = Defaults::Enemies::Scoring::QUADRAT_SCORE;
break; break;
case EnemyType::MOLINILLO: case EnemyType::MOLINILLO:
points = Defaults::Enemies::Scoring::MOLINILLO_SCORE; points = Defaults::Enemies::Scoring::MOLINILLO_SCORE;
break; break;
} }
uint8_t owner_id = bullet.getOwnerId(); uint8_t owner_id = bullet.getOwnerId();
ctx.score_per_player[owner_id] += points; ctx.score_per_player[owner_id] += points;
ctx.floating_score_manager.crear(points, enemy_pos); ctx.floating_score_manager.crear(points, enemy_pos);
// 2. Destruir enemy + crear explosión (debris hereda color del enemy) // 2. Destruir enemy + crear explosión (debris hereda color del enemy)
SDL_Color enemy_color{}; SDL_Color enemy_color{};
switch (enemy.getType()) { switch (enemy.getType()) {
case EnemyType::PENTAGON: enemy_color = Defaults::Palette::PENTAGON; break; case EnemyType::PENTAGON:
case EnemyType::QUADRAT: enemy_color = Defaults::Palette::QUADRAT; break; enemy_color = Defaults::Palette::PENTAGON;
case EnemyType::MOLINILLO: enemy_color = Defaults::Palette::MOLINILLO; break; break;
} case EnemyType::QUADRAT:
enemy.destruir(); enemy_color = Defaults::Palette::QUADRAT;
Vec2 vel_enemic = enemy.getVelocityVector(); break;
ctx.debris_manager.explode( case EnemyType::MOLINILLO:
enemy.getShape(), enemy_color = Defaults::Palette::MOLINILLO;
enemy_pos, break;
0.0F, // angle (la rotación es interna del enemy) }
1.0F, // escala enemy.destruir();
VELOCITAT_EXPLOSIO, Vec2 vel_enemic = enemy.getVelocityVector();
enemy.getBrightness(), ctx.debris_manager.explode(
vel_enemic, enemy.getShape(),
enemy.getRotationDelta(), enemy_pos,
0.0F, // sin herencia visual 0.0F, // angle (la rotación es interna del enemy)
Defaults::Sound::EXPLOSION, 1.0F, // escala
enemy_color VELOCITAT_EXPLOSIO,
); enemy.getBrightness(),
vel_enemic,
enemy.getRotationDelta(),
0.0F, // sin herencia visual
Defaults::Sound::EXPLOSION,
enemy_color);
// 3. Desactivar bullet (solo destruye 1 enemy) // 3. Desactivar bullet (solo destruye 1 enemy)
bullet.desactivar(); bullet.desactivar();
break; break;
}
}
}
void detectShipEnemy(Context& ctx) {
constexpr float AMPLIFIER = Defaults::Game::COLLISION_SHIP_ENEMY_AMPLIFIER;
for (uint8_t i = 0; i < 2; i++) {
// Skip si ya tocado / muerto / invulnerable
if (ctx.hit_timer_per_player[i] > 0.0F ||
!ctx.ships[i].isAlive() ||
ctx.ships[i].isInvulnerable()) {
continue;
}
for (const auto& enemy : ctx.enemies) {
if (enemy.isInvulnerable()) {
continue;
}
if (Physics::checkCollision(ctx.ships[i], enemy, AMPLIFIER)) {
ctx.on_player_hit(i);
break; // Solo una colisión por player por frame
} }
} }
} }
}
void detectBulletPlayer(Context& ctx) { void detectShipEnemy(Context& ctx) {
if (!Defaults::Game::FRIENDLY_FIRE_ENABLED) { constexpr float AMPLIFIER = Defaults::Game::COLLISION_SHIP_ENEMY_AMPLIFIER;
return;
}
constexpr float AMPLIFIER = Defaults::Game::COLLISION_BULLET_PLAYER_AMPLIFIER; for (uint8_t i = 0; i < 2; i++) {
// Skip si ya tocado / muerto / invulnerable
for (auto& bullet : ctx.bullets) { if (ctx.hit_timer_per_player[i] > 0.0F ||
if (!bullet.isActive() || bullet.getGraceTimer() > 0.0F) { !ctx.ships[i].isActive() ||
continue; ctx.ships[i].isInvulnerable()) {
}
const uint8_t BULLET_OWNER = bullet.getOwnerId();
for (uint8_t player_id = 0; player_id < 2; player_id++) {
if (ctx.hit_timer_per_player[player_id] > 0.0F ||
!ctx.ships[player_id].isAlive() ||
ctx.ships[player_id].isInvulnerable()) {
continue;
}
const bool JUGADOR_ACTIU = (player_id == 0)
? ctx.match_config.jugador1_actiu
: ctx.match_config.jugador2_actiu;
if (!JUGADOR_ACTIU) {
continue; continue;
} }
if (!Physics::checkCollision(bullet, ctx.ships[player_id], AMPLIFIER)) { for (const auto& enemy : ctx.enemies) {
continue; if (enemy.isInvulnerable()) {
continue;
}
if (Physics::checkCollision(ctx.ships[i], enemy, AMPLIFIER)) {
ctx.on_player_hit(i);
break; // Solo una colisión por player por frame
}
} }
// *** FRIENDLY FIRE HIT ***
if (BULLET_OWNER == player_id) {
// Self-hit: víctima pierde 1 vida.
ctx.on_player_hit(player_id);
} else {
// Teammate hit: víctima pierde 1, atacante gana 1.
ctx.on_player_hit(player_id);
ctx.lives_per_player[BULLET_OWNER]++;
}
Audio::get()->playSound(Defaults::Sound::FRIENDLY_FIRE_HIT, Audio::Group::GAME);
bullet.desactivar();
break; // Una bullet solo impacta una vez por frame
} }
} }
}
void detectAll(Context& ctx) { void detectBulletPlayer(Context& ctx) {
detectBulletEnemy(ctx); if (!Defaults::Game::FRIENDLY_FIRE_ENABLED) {
detectShipEnemy(ctx); return;
detectBulletPlayer(ctx); }
}
constexpr float AMPLIFIER = Defaults::Game::COLLISION_BULLET_PLAYER_AMPLIFIER;
for (auto& bullet : ctx.bullets) {
if (!bullet.isActive() || bullet.getGraceTimer() > 0.0F) {
continue;
}
const uint8_t BULLET_OWNER = bullet.getOwnerId();
for (uint8_t player_id = 0; player_id < 2; player_id++) {
if (ctx.hit_timer_per_player[player_id] > 0.0F ||
!ctx.ships[player_id].isActive() ||
ctx.ships[player_id].isInvulnerable()) {
continue;
}
const bool JUGADOR_ACTIU = (player_id == 0)
? ctx.match_config.jugador1_actiu
: ctx.match_config.jugador2_actiu;
if (!JUGADOR_ACTIU) {
continue;
}
if (!Physics::checkCollision(bullet, ctx.ships[player_id], AMPLIFIER)) {
continue;
}
// *** FRIENDLY FIRE HIT ***
if (BULLET_OWNER == player_id) {
// Self-hit: víctima pierde 1 vida.
ctx.on_player_hit(player_id);
} else {
// Teammate hit: víctima pierde 1, atacante gana 1.
ctx.on_player_hit(player_id);
ctx.lives_per_player[BULLET_OWNER]++;
}
Audio::get()->playSound(Defaults::Sound::FRIENDLY_FIRE_HIT, Audio::Group::GAME);
bullet.desactivar();
break; // Una bullet solo impacta una vez por frame
}
}
}
void detectAll(Context& ctx) {
detectBulletEnemy(ctx);
detectShipEnemy(ctx);
detectBulletPlayer(ctx);
}
} // namespace Systems::Collision } // namespace Systems::Collision