From: Taran Nathan Date: Thu, 25 Jun 2026 16:26:36 +0000 (+0200) Subject: asteroid splitting X-Git-Url: https://git.taranathan.com/?a=commitdiff_plain;h=03724aca208d300aa96bfc5c2152c5e4d533effe;p=asteroids.git asteroid splitting --- diff --git a/Cargo.lock b/Cargo.lock index cd0f4c0..aa28936 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -214,6 +214,15 @@ dependencies = [ "syn", ] +[[package]] +name = "asteroids" +version = "0.1.0" +dependencies = [ + "bevy", + "bevy_dylib", + "rand", +] + [[package]] name = "async-broadcast" version = "0.7.2" @@ -2341,15 +2350,6 @@ dependencies = [ "pin-project-lite", ] -[[package]] -name = "game" -version = "0.1.0" -dependencies = [ - "bevy", - "bevy_dylib", - "rand", -] - [[package]] name = "gethostname" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index 02bee6d..e4ef984 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "game" +name = "asteroids" version = "0.1.0" edition = "2024" diff --git a/assets/boss-arm.png b/assets/boss-arm.png new file mode 100644 index 0000000..f998419 Binary files /dev/null and b/assets/boss-arm.png differ diff --git a/assets/boss-arm.svg b/assets/boss-arm.svg new file mode 100644 index 0000000..298d564 --- /dev/null +++ b/assets/boss-arm.svg @@ -0,0 +1,59 @@ + + + + + + + + + + diff --git a/assets/boss-core.png b/assets/boss-core.png new file mode 100644 index 0000000..c4d9c84 Binary files /dev/null and b/assets/boss-core.png differ diff --git a/assets/boss-core.svg b/assets/boss-core.svg new file mode 100644 index 0000000..7eedf48 --- /dev/null +++ b/assets/boss-core.svg @@ -0,0 +1,61 @@ + + + + + + + + + + + + diff --git a/src/entities.rs b/src/entities.rs index c0d8c75..f93eaf9 100644 --- a/src/entities.rs +++ b/src/entities.rs @@ -19,7 +19,9 @@ pub struct Player { } #[derive(Component)] -pub struct Asteroid; +pub struct Asteroid { + pub health: i8, +} #[derive(Component)] pub struct Collider { diff --git a/src/systems.rs b/src/systems.rs index c2a4d42..e7e628d 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -1,7 +1,6 @@ use bevy::{ app::AppExit, color::palettes::css::{GREEN, RED}, - ecs::system::IntoResult, prelude::*, }; use rand::Rng; @@ -22,9 +21,11 @@ const BULLET_Z: f32 = 2.0; const ASTEROID_SIZE: f32 = 150.0; const ASTEROID_SPEED: f32 = 2.0; -const INITIAL_ASTEROID_COUNT: i32 = 5; +const INITIAL_ASTEROID_COUNT: i32 = 1; const ASTEROID_Z: f32 = 1.0; +const VELOCITY_CONTINUITY: f32 = 0.5; + pub fn load_sprites( mut commands: Commands, asset_server: Res, @@ -67,7 +68,7 @@ pub fn setup(mut commands: Commands, window_size: Res, sprites: Res< ..default() }, Sprite::from_image(sprites.asteroid_sprite.clone()), - Asteroid, + Asteroid { health: 2 }, Velocity { linear: Vec2::new(angle.cos(), angle.sin()) * ASTEROID_SPEED, angular: rng.random_range(-0.5..0.5), @@ -77,7 +78,6 @@ pub fn setup(mut commands: Commands, window_size: Res, sprites: Res< }, )); } - } pub fn player_movement_system( @@ -214,8 +214,9 @@ fn cleanup(commands: &mut Commands, query: Query>) pub fn collision_system( mut commands: Commands, mut player_query: Query<(Entity, &Transform, &Collider, &mut Player)>, - asteroid_query: Query<(Entity, &Transform, &Collider), With>, + asteroid_query: Query<(Entity, &Transform, &Collider, &Asteroid, &Velocity)>, bullet_query: Query<(Entity, &Transform, &Collider), With>, + sprites: Res, mut gizmos: Gizmos, ) { if let Ok((player_entity, player_transform, player_collider, mut player)) = @@ -229,7 +230,9 @@ pub fn collision_system( ) .resolution(64); - for (asteroid_entity, asteroid_transform, asteroid_collider) in &asteroid_query { + for (asteroid_entity, asteroid_transform, asteroid_collider, asteroid, asteroid_velocity) in + &asteroid_query + { gizmos .circle_2d( Isometry2d::new(asteroid_transform.translation.truncate(), 0.0.into()), @@ -255,22 +258,77 @@ pub fn collision_system( } } - for (bullet_entity, bullet_transform, bullet_collider) in &bullet_query { - for (asteroid_entity, asteroid_transform, asteroid_collider) in &asteroid_query { + 'bullet: for (bullet_entity, bullet_transform, bullet_collider) in &bullet_query { + '_asteroid: for ( + asteroid_entity, + asteroid_transform, + asteroid_collider, + asteroid, + asteroid_velocity, + ) in &asteroid_query + { let distance = bullet_transform .translation .truncate() .distance(asteroid_transform.translation.truncate()); if distance < bullet_collider.radius + asteroid_collider.radius { + let new_health = asteroid.health - 1; + let asteroid_pos = asteroid_transform.translation.xy(); + let size = 0.5 * new_health as f32; + commands.entity(bullet_entity).despawn(); commands.entity(asteroid_entity).despawn(); - break; + + dbg!(&new_health); + if new_health > 0 { + spawn_asteroid( + asteroid_pos, + new_health, + size, + &asteroid_velocity.linear, + &mut commands, + &sprites, + ); + } + continue 'bullet; } } } } +fn spawn_asteroid( + location: Vec2, + health: i8, + size: f32, + past_velocity: &Vec2, + commands: &mut Commands, + sprites: &Sprites, +) { + let mut rng = rand::rng(); + let angle = rng.random_range(0.0..2.0 * PI); + let angle_vec = Vec2::new(angle.cos(), angle.sin()); + + commands.spawn(( + Transform { + translation: Vec3::new(location.x, location.y, ASTEROID_Z), + scale: Vec3::splat(0.5) * size, + rotation: Quat::from_rotation_z(rng.random_range(0.0..2.0 * PI)), + ..default() + }, + Sprite::from_image(sprites.asteroid_sprite.clone()), + Asteroid { health }, + Velocity { + linear: (angle_vec + (VELOCITY_CONTINUITY * past_velocity)).normalize_or(angle_vec) + * ASTEROID_SPEED, + angular: rng.random_range(-0.5..0.5), + }, + Collider { + radius: (ASTEROID_SIZE / 2.0) * size, + }, + )); +} + pub fn toggle_pause_system( mut next_state: ResMut>, current_state: Res>, @@ -355,8 +413,13 @@ pub fn button_quit( ) { for (entity, interaction, button, children, node, mut border_color) in interaction_query { match interaction { - Interaction::Pressed => { writer.write(AppExit::Success); }, - Interaction::Hovered => *border_color = BorderColor::all(Color::srgb(166.0 / 255., 255. / 255., 196. / 255.)), + Interaction::Pressed => { + writer.write(AppExit::Success); + } + Interaction::Hovered => { + *border_color = + BorderColor::all(Color::srgb(166.0 / 255., 255. / 255., 196. / 255.)) + } Interaction::None => (), } } @@ -365,4 +428,3 @@ pub fn button_quit( pub fn despawn_menu(mut commands: Commands, query: Query>) { cleanup(&mut commands, query); } -