From 45482f9e118022ddb53b49b796e8941b6dd8fa53 Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Sat, 5 Apr 2025 12:48:58 +0200 Subject: [PATCH] refactor: Optimize attack dive timer logic and enemy movement checks --- src/main.rs | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6e50362..17a0bdf 100644 --- a/src/main.rs +++ b/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, With), >, mut attacking_query: Query< - (Entity, &mut Transform), - (With, Without, With), - >, // Query attacking enemies separately + (Entity, &mut Transform, &EnemyState), // Add &EnemyState here + (With, Without), // Keep With filter if desired, but we check explicitly below + >, // Query potential attackers time: Res