bbbb8d47ae
Identifier-naming: rename de métodos públicos y cross-file al inglés
(camelBack), traducción de campos y locales en el proceso (TitleShip,
StageManager, SpawnController, ShipAnimator, helpers de PlayArea, etc.).
Refactor por cognitive-complexity (>25): GameScene::draw (59→3) con 9
helpers de estado, PhysicsWorld::resolveBodyCollisions (35→5) extrayendo
resolveBodyPair, Options::load{Window,Physics,Audio}ConfigFromYaml
(32/49/57→5/2/3) con templates readField, TitleScene::update (68→4) con
5 sub-pasos por estado + handleSkipInput/handleStartInput +
triggerExitForJoinedPlayers, DebrisManager::explode (39→3) con
extractSegments/spawnDebris/applyAngularVelocity/applyVisualRotation.
use-anyofallof: bucles → std::ranges::any_of/all_of en Input,
ShipAnimator y SpawnController.
readability-static-accessed-through-instance: Director::run y
VectorText::getTextWidth/Height invocados por clase.
readability-convert-member-functions-to-static: ResourcePack::decryptData.
unused-includes: eliminación de <utility>, <cstdint>, <vector>,
<iostream>, defaults.hpp y otros no usados directamente en headers y
unidades de traducción. Restablecido core/defaults.hpp en title_scene.cpp
(falsa "unused" del header).
Bug fix: eliminado isActive() duplicado en Bullet (redeclaración tras
rename de esta_activa→isActive que chocaba con el override de Entity).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
151 lines
5.1 KiB
C++
151 lines
5.1 KiB
C++
// collision_system.cpp - Implementación del sistema de colisiones
|
|
|
|
#include "game/systems/collision_system.hpp"
|
|
|
|
#include <cstdint>
|
|
|
|
#include "core/audio/audio.hpp"
|
|
#include "core/physics/collision.hpp"
|
|
#include "core/types.hpp"
|
|
|
|
namespace Systems::Collision {
|
|
|
|
void detectBulletEnemy(Context& ctx) {
|
|
constexpr float AMPLIFIER = Defaults::Game::COLLISION_BULLET_ENEMY_AMPLIFIER;
|
|
constexpr float VELOCITAT_EXPLOSIO = 80.0F; // px/s (explosión suau)
|
|
|
|
for (auto& bullet : ctx.bullets) {
|
|
for (auto& enemy : ctx.enemies) {
|
|
if (!Physics::checkCollision(bullet, enemy, AMPLIFIER)) {
|
|
continue;
|
|
}
|
|
|
|
// *** COLISIÓN bullet → enemy ***
|
|
const Vec2& enemy_pos = enemy.getCenter();
|
|
|
|
// 1. Puntos según tipo
|
|
int points = 0;
|
|
switch (enemy.getType()) {
|
|
case EnemyType::PENTAGON:
|
|
points = Defaults::Enemies::Scoring::PENTAGON_SCORE;
|
|
break;
|
|
case EnemyType::QUADRAT:
|
|
points = Defaults::Enemies::Scoring::QUADRAT_SCORE;
|
|
break;
|
|
case EnemyType::MOLINILLO:
|
|
points = Defaults::Enemies::Scoring::MOLINILLO_SCORE;
|
|
break;
|
|
}
|
|
|
|
uint8_t owner_id = bullet.getOwnerId();
|
|
ctx.score_per_player[owner_id] += points;
|
|
ctx.floating_score_manager.crear(points, enemy_pos);
|
|
|
|
// 2. Destruir enemy + crear explosión (debris hereda color del enemy)
|
|
SDL_Color enemy_color{};
|
|
switch (enemy.getType()) {
|
|
case EnemyType::PENTAGON: enemy_color = Defaults::Palette::PENTAGON; break;
|
|
case EnemyType::QUADRAT: enemy_color = Defaults::Palette::QUADRAT; break;
|
|
case EnemyType::MOLINILLO: enemy_color = Defaults::Palette::MOLINILLO; break;
|
|
}
|
|
enemy.destruir();
|
|
Vec2 vel_enemic = enemy.getVelocityVector();
|
|
ctx.debris_manager.explode(
|
|
enemy.getShape(),
|
|
enemy_pos,
|
|
0.0F, // angle (la rotación es interna del enemy)
|
|
1.0F, // escala
|
|
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)
|
|
bullet.desactivar();
|
|
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) {
|
|
if (!Defaults::Game::FRIENDLY_FIRE_ENABLED) {
|
|
return;
|
|
}
|
|
|
|
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].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;
|
|
}
|
|
|
|
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
|