From 6acfee2f95078f4b10356c089592b7ab058a4dcd Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Sat, 5 Apr 2025 12:53:14 +0200 Subject: [PATCH] feat: Enhance enemy attack dive mechanics with swooping movement towards the center --- README.md | 2 +- src/main.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c2bb897..1f04e1e 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ nix develop --command bash -c "cargo build" * **Enemy Attack Dives:** * ~~Give enemies an `Attacking` state/component.~~ **(DONE)** * ~~Periodically trigger enemies in the formation to switch to the `Attacking` state.~~ **(DONE - Random selection after formation complete)** - * Define attack paths (swooping dives towards the player area). **(Basic downward dive implemented)** + * ~~Define attack paths (swooping dives towards the player area).~~ **(DONE - Basic swoop towards center implemented)** * ~~Make enemies fire bullets (downwards or towards the player) during their dives.~~ **(DONE - Downward)** * After an attack dive, enemies could return to their formation position or fly off-screen. **(Fly off-screen implemented)** * **Enemy Variety:** diff --git a/src/main.rs b/src/main.rs index 17a0bdf..cbbbc91 100644 --- a/src/main.rs +++ b/src/main.rs @@ -714,8 +714,30 @@ fn move_enemies( 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(); + // --- Swooping Dive Logic --- + let delta_seconds = time.delta_seconds(); + let vertical_movement = attack_speed * delta_seconds; + + // Horizontal movement: Move towards the center (x=0) + let horizontal_speed_factor = 0.5; // Adjust this to control the swoop intensity + let horizontal_movement = if transform.translation.x < 0.0 { + attack_speed * horizontal_speed_factor * delta_seconds + } else if transform.translation.x > 0.0 { + -attack_speed * horizontal_speed_factor * delta_seconds + } else { + 0.0 // No horizontal movement if exactly at center + }; + + // Apply movement + transform.translation.y -= vertical_movement; + transform.translation.x += horizontal_movement; + + // Ensure enemy doesn't overshoot the center horizontally if moving towards it + if (transform.translation.x - horizontal_movement < 0.0 && transform.translation.x > 0.0) + || (transform.translation.x - horizontal_movement > 0.0 && transform.translation.x < 0.0) + { + transform.translation.x = 0.0; + } } // Enemies that are InFormation but Without will now be ignored by this movement logic.