refactor: Optimize attack dive timer logic and enemy movement checks
This commit is contained in:
parent
66fd1e8b1b
commit
45482f9e11
40
src/main.rs
40
src/main.rs
|
@ -208,8 +208,10 @@ fn trigger_attack_dives(
|
|||
) {
|
||||
timer.timer.tick(time.delta());
|
||||
|
||||
// Tick the timer regardless
|
||||
timer.timer.tick(time.delta());
|
||||
// Timer is ticked implicitly by the system scheduler when the run condition is met,
|
||||
// or we can tick it explicitly *after* checking formation_complete.
|
||||
// Let's rely on the run condition and internal check for now.
|
||||
// timer.timer.tick(time.delta()); // Removed redundant tick
|
||||
|
||||
// Only proceed if the timer finished AND the formation is complete
|
||||
if timer.timer.just_finished() && formation_state.formation_complete {
|
||||
|
@ -313,7 +315,9 @@ fn check_formation_complete(
|
|||
formation_state.total_spawned_this_stage
|
||||
);
|
||||
formation_state.formation_complete = true;
|
||||
attack_dive_timer.timer.reset(); // Reset the dive timer to ensure a delay
|
||||
attack_dive_timer.timer.reset(); // Reset the dive timer
|
||||
attack_dive_timer.timer.unpause(); // Start the timer now
|
||||
println!("Formation complete! Attack timer unpaused and reset.");
|
||||
}
|
||||
// else { // Optional log
|
||||
// println!("Checking formation complete: Still entering...");
|
||||
|
@ -402,8 +406,12 @@ fn main() {
|
|||
formation_complete: false,
|
||||
}) // Initialize formation state with flag
|
||||
.insert_resource(AttackDiveTimer {
|
||||
timer: Timer::new(Duration::from_secs_f32(3.0), TimerMode::Repeating),
|
||||
}) // Initialize attack timer (e.g., every 3 seconds)
|
||||
timer: {
|
||||
let mut timer = Timer::new(Duration::from_secs_f32(3.0), TimerMode::Repeating);
|
||||
timer.pause(); // Start paused until formation is complete
|
||||
timer
|
||||
},
|
||||
}) // Initialize attack timer (e.g., every 3 seconds), initially paused
|
||||
// Add Systems
|
||||
.add_systems(Startup, setup)
|
||||
// Systems running only when Playing
|
||||
|
@ -664,9 +672,9 @@ fn move_enemies(
|
|||
(With<Enemy>, With<FormationTarget>),
|
||||
>,
|
||||
mut attacking_query: Query<
|
||||
(Entity, &mut Transform),
|
||||
(With<Enemy>, Without<FormationTarget>, With<EnemyState>),
|
||||
>, // Query attacking enemies separately
|
||||
(Entity, &mut Transform, &EnemyState), // Add &EnemyState here
|
||||
(With<Enemy>, Without<FormationTarget>), // Keep With<EnemyState> filter if desired, but we check explicitly below
|
||||
>, // Query potential attackers
|
||||
time: Res<Time>,
|
||||
mut commands: Commands,
|
||||
stage: Res<CurrentStage>, // Add stage resource for speed calculation
|
||||
|
@ -703,15 +711,13 @@ fn move_enemies(
|
|||
|
||||
// --- Handle Attacking Enemies ---
|
||||
let attack_speed = current_speed * 1.5; // Make them dive faster
|
||||
for (entity, mut transform) in attacking_query.iter_mut() {
|
||||
// Check state again in case the query picks up something unintended
|
||||
// (Though With<EnemyState> and Without<FormationTarget> should be sufficient)
|
||||
// We need a way to get the state here if we want to be super safe,
|
||||
// maybe query state separately or adjust the query structure.
|
||||
// For now, assume this query correctly gets Attacking enemies.
|
||||
|
||||
// Move straight down
|
||||
transform.translation.y -= attack_speed * time.delta_seconds();
|
||||
for (entity, mut transform, state) in attacking_query.iter_mut() { // Get state from query
|
||||
// *** Explicitly check if the enemy is actually in the Attacking state ***
|
||||
if *state == EnemyState::Attacking {
|
||||
// Move straight down ONLY if attacking
|
||||
transform.translation.y -= attack_speed * time.delta_seconds();
|
||||
}
|
||||
// Enemies that are InFormation but Without<FormationTarget> will now be ignored by this movement logic.
|
||||
|
||||
// Despawn if off screen
|
||||
if transform.translation.y < -WINDOW_HEIGHT / 2.0 - ENEMY_SIZE.y {
|
||||
|
|
Loading…
Reference in a new issue