afegit friendly fire
This commit is contained in:
@@ -476,6 +476,7 @@ void EscenaJoc::actualitzar(float delta_time) {
|
||||
|
||||
detectar_col·lisions_bales_enemics();
|
||||
detectar_col·lisio_naus_enemics();
|
||||
detectar_col·lisions_bales_jugadors();
|
||||
debris_manager_.actualitzar(delta_time);
|
||||
gestor_puntuacio_.actualitzar(delta_time);
|
||||
break;
|
||||
@@ -1068,6 +1069,81 @@ void EscenaJoc::detectar_col·lisio_naus_enemics() {
|
||||
}
|
||||
}
|
||||
|
||||
void EscenaJoc::detectar_col·lisions_bales_jugadors() {
|
||||
// Skip if friendly fire disabled
|
||||
if (!Defaults::Game::FRIENDLY_FIRE_ENABLED) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Collision constants (exact hitbox, 1.0x amplification)
|
||||
constexpr float RADI_NAU = Defaults::Entities::SHIP_RADIUS;
|
||||
constexpr float RADI_BALA = Defaults::Entities::BULLET_RADIUS;
|
||||
constexpr float SUMA_RADIS = (RADI_NAU + RADI_BALA) *
|
||||
Defaults::Game::COLLISION_BULLET_PLAYER_AMPLIFIER; // 15.0 px
|
||||
constexpr float SUMA_RADIS_QUADRAT = SUMA_RADIS * SUMA_RADIS; // 225.0
|
||||
|
||||
// Check all active bullets
|
||||
for (auto& bala : bales_) {
|
||||
if (!bala.esta_activa()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip bullets in grace period (prevents instant self-collision)
|
||||
if (bala.get_grace_timer() > 0.0f) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const Punt& pos_bala = bala.get_centre();
|
||||
uint8_t bullet_owner = bala.get_owner_id();
|
||||
|
||||
// Check collision with BOTH players
|
||||
for (uint8_t player_id = 0; player_id < 2; player_id++) {
|
||||
// Skip if player is dead, invulnerable, or inactive
|
||||
if (itocado_per_jugador_[player_id] > 0.0f) continue;
|
||||
if (!naus_[player_id].esta_viva()) continue;
|
||||
if (naus_[player_id].es_invulnerable()) continue;
|
||||
|
||||
// Skip inactive players
|
||||
bool jugador_actiu = (player_id == 0) ? config_partida_.jugador1_actiu
|
||||
: config_partida_.jugador2_actiu;
|
||||
if (!jugador_actiu) continue;
|
||||
|
||||
const Punt& pos_nau = naus_[player_id].get_centre();
|
||||
|
||||
// Calculate squared distance (avoid sqrt)
|
||||
float dx = pos_bala.x - pos_nau.x;
|
||||
float dy = pos_bala.y - pos_nau.y;
|
||||
float distancia_quadrada = dx * dx + dy * dy;
|
||||
|
||||
// Check collision
|
||||
if (distancia_quadrada <= SUMA_RADIS_QUADRAT) {
|
||||
// *** FRIENDLY FIRE HIT ***
|
||||
|
||||
if (bullet_owner == player_id) {
|
||||
// CASE 1: Self-hit (own bullet)
|
||||
// Player loses 1 life, no gain
|
||||
tocado(player_id);
|
||||
} else {
|
||||
// CASE 2: Teammate hit
|
||||
// Victim loses 1 life
|
||||
tocado(player_id);
|
||||
|
||||
// Attacker gains 1 life (no cap)
|
||||
vides_per_jugador_[bullet_owner]++;
|
||||
}
|
||||
|
||||
// Play distinct sound
|
||||
Audio::get()->playSound(Defaults::Sound::FRIENDLY_FIRE_HIT, Audio::Group::GAME);
|
||||
|
||||
// Deactivate bullet
|
||||
bala.desactivar();
|
||||
|
||||
break; // Bullet only hits once per frame
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// [NEW] Stage system helper methods
|
||||
|
||||
void EscenaJoc::dibuixar_missatge_stage(const std::string& missatge) {
|
||||
|
||||
Reference in New Issue
Block a user