refactor: clean up formatting and improve readability in components and player logic

This commit is contained in:
Harald Hoyer 2025-04-16 08:46:12 +02:00
parent 008f9cc24a
commit d27d27bb5a
3 changed files with 47 additions and 23 deletions

View file

@ -31,7 +31,8 @@ pub const BULLET_ENEMY_COLLISION_THRESHOLD: f32 = (BULLET_SIZE.x + ENEMY_SIZE.x)
// 22.5
pub const PLAYER_ENEMY_COLLISION_THRESHOLD: f32 = (PLAYER_SIZE.x + ENEMY_SIZE.x) * 0.5;
// 35.0
pub const ENEMY_BULLET_PLAYER_COLLISION_THRESHOLD: f32 = (ENEMY_BULLET_SIZE.x + PLAYER_SIZE.x) * 0.5;
pub const ENEMY_BULLET_PLAYER_COLLISION_THRESHOLD: f32 =
(ENEMY_BULLET_SIZE.x + PLAYER_SIZE.x) * 0.5;
// Tractor beam constants
pub const TRACTOR_BEAM_WIDTH: f32 = 20.0;

View file

@ -1,7 +1,7 @@
use bevy::prelude::*;
use std::time::Duration;
use crate::components::{Bullet, Enemy, Invincible, Player, Captured};
use crate::components::{Bullet, Captured, Enemy, Invincible, Player};
use crate::constants::{
BULLET_SIZE, PLAYER_ENEMY_COLLISION_THRESHOLD, PLAYER_INVINCIBILITY_DURATION, PLAYER_SIZE,
PLAYER_SPEED, WINDOW_HEIGHT, WINDOW_WIDTH,
@ -84,7 +84,10 @@ pub fn handle_captured_player(
// Check if the boss exists
let boss_exists = enemy_query.get(captured.boss_entity).is_ok();
let boss_pos = if boss_exists {
enemy_query.get(captured.boss_entity).map(|t| t.translation).ok()
enemy_query
.get(captured.boss_entity)
.map(|t| t.translation)
.ok()
} else {
None
};
@ -93,7 +96,12 @@ pub fn handle_captured_player(
let mut timer_copy = captured.timer.clone();
timer_copy.tick(time.delta());
to_process.push((entity, transform.translation, boss_pos, timer_copy.finished()));
to_process.push((
entity,
transform.translation,
boss_pos,
timer_copy.finished(),
));
}
// Now process each player separately
@ -107,12 +115,18 @@ pub fn handle_captured_player(
// Boss exists, update player position
let target_pos = boss_pos - Vec3::new(0.0, PLAYER_SIZE.y + 10.0, 0.0);
transform.translation = current_pos.lerp(target_pos, 0.2);
},
}
None => {
// Boss is gone, release player but lose a life
println!("Boss is gone, releasing captured player!");
commands.entity(entity).remove::<Captured>();
lose_life_and_respawn(&mut commands, &mut lives, &mut respawn_timer, &mut next_state, entity);
lose_life_and_respawn(
&mut commands,
&mut lives,
&mut respawn_timer,
&mut next_state,
entity,
);
}
}
@ -120,7 +134,13 @@ pub fn handle_captured_player(
if timer_would_finish || captured.timer.finished() {
println!("Player escaped from capture after timer expired!");
commands.entity(entity).remove::<Captured>();
lose_life_and_respawn(&mut commands, &mut lives, &mut respawn_timer, &mut next_state, entity);
lose_life_and_respawn(
&mut commands,
&mut lives,
&mut respawn_timer,
&mut next_state,
entity,
);
}
}
}
@ -192,7 +212,10 @@ pub fn check_player_enemy_collisions(
mut respawn_timer: ResMut<PlayerRespawnTimer>,
mut next_state: ResMut<NextState<AppState>>, // Resource to change state
// Query player without Invincible component - relies on run_if condition too
player_query: Query<(Entity, &Transform), (With<Player>, Without<Invincible>, Without<Captured>)>, // Don't check collisions for captured players
player_query: Query<
(Entity, &Transform),
(With<Player>, Without<Invincible>, Without<Captured>),
>, // Don't check collisions for captured players
enemy_query: Query<(Entity, &Transform), With<Enemy>>,
) {
// This system only runs if player exists and is not invincible, due to run_if