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