]> git.taranathan.com Git - asteroids.git/commitdiff
asteroid splitting
authorTaran Nathan <moogoesmeow123@gmail.com>
Thu, 25 Jun 2026 16:26:36 +0000 (18:26 +0200)
committerTaran Nathan <moogoesmeow123@gmail.com>
Thu, 25 Jun 2026 17:10:06 +0000 (19:10 +0200)
Cargo.lock
Cargo.toml
assets/boss-arm.png [new file with mode: 0644]
assets/boss-arm.svg [new file with mode: 0644]
assets/boss-core.png [new file with mode: 0644]
assets/boss-core.svg [new file with mode: 0644]
src/entities.rs
src/systems.rs

index cd0f4c02c13e88896c7d8fb63f8ecbb6f5dbe70f..aa2893648244d018bbfb2c3e7eda393b60e9e75a 100644 (file)
@@ -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"
index 02bee6d025eb7f6d042ff2c21fc8a10a1cd6e444..e4ef984d90e29e55bd4fdc5205324c45822d8c4b 100644 (file)
@@ -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 (file)
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 (file)
index 0000000..298d564
--- /dev/null
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!-- Created with Inkscape (http://www.inkscape.org/) -->
+
+<svg
+   width="1000mm"
+   height="1000mm"
+   viewBox="0 0 1000 1000"
+   version="1.1"
+   id="svg1"
+   inkscape:version="1.4.3 (0d15f75042, 2025-12-25)"
+   sodipodi:docname="boss-arm.svg"
+   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
+   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+   xmlns="http://www.w3.org/2000/svg"
+   xmlns:svg="http://www.w3.org/2000/svg">
+  <sodipodi:namedview
+     id="namedview1"
+     pagecolor="#ffffff"
+     bordercolor="#000000"
+     borderopacity="0.25"
+     inkscape:showpageshadow="2"
+     inkscape:pageopacity="0.0"
+     inkscape:pagecheckerboard="0"
+     inkscape:deskcolor="#d1d1d1"
+     inkscape:document-units="mm"
+     inkscape:zoom="0.13993469"
+     inkscape:cx="1725.8051"
+     inkscape:cy="1965.2025"
+     inkscape:window-width="1512"
+     inkscape:window-height="917"
+     inkscape:window-x="0"
+     inkscape:window-y="0"
+     inkscape:window-maximized="1"
+     inkscape:current-layer="layer1" />
+  <defs
+     id="defs1" />
+  <g
+     inkscape:label="Layer 1"
+     inkscape:groupmode="layer"
+     id="layer1">
+    <path
+       sodipodi:type="star"
+       style="fill:#494949;stroke:#000000;stroke-width:10.5833"
+       id="path1"
+       inkscape:flatsided="true"
+       sodipodi:sides="3"
+       sodipodi:cx="500"
+       sodipodi:cy="500"
+       sodipodi:r1="408.55368"
+       sodipodi:r2="204.27684"
+       sodipodi:arg1="-1.5707963"
+       sodipodi:arg2="-0.52359878"
+       inkscape:rounded="0"
+       inkscape:randomized="0"
+       d="m 500.00001,91.44632 353.81785,612.83053 -707.63573,-2e-5 z"
+       inkscape:transform-center-y="-102.13842"
+       transform="translate(0,104.78424)" />
+  </g>
+</svg>
diff --git a/assets/boss-core.png b/assets/boss-core.png
new file mode 100644 (file)
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 (file)
index 0000000..7eedf48
--- /dev/null
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!-- Created with Inkscape (http://www.inkscape.org/) -->
+
+<svg
+   width="1000mm"
+   height="1000mm"
+   viewBox="0 0 1000 1000"
+   version="1.1"
+   id="svg1"
+   inkscape:version="1.4.3 (0d15f75042, 2025-12-25)"
+   sodipodi:docname="boss-core.svg"
+   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
+   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+   xmlns="http://www.w3.org/2000/svg"
+   xmlns:svg="http://www.w3.org/2000/svg">
+  <sodipodi:namedview
+     id="namedview1"
+     pagecolor="#ffffff"
+     bordercolor="#000000"
+     borderopacity="0.25"
+     inkscape:showpageshadow="2"
+     inkscape:pageopacity="0.0"
+     inkscape:pagecheckerboard="0"
+     inkscape:deskcolor="#d1d1d1"
+     inkscape:document-units="mm"
+     inkscape:zoom="0.15158361"
+     inkscape:cx="1718.5235"
+     inkscape:cy="1622.8668"
+     inkscape:window-width="1512"
+     inkscape:window-height="917"
+     inkscape:window-x="0"
+     inkscape:window-y="0"
+     inkscape:window-maximized="1"
+     inkscape:current-layer="layer1" />
+  <defs
+     id="defs1">
+    <inkscape:path-effect
+       effect="bspline"
+       id="path-effect1"
+       is_visible="true"
+       lpeversion="1.3"
+       weight="33.333333"
+       steps="2"
+       helper_size="0"
+       apply_no_weight="true"
+       apply_with_weight="true"
+       only_selected="false"
+       uniform="false" />
+  </defs>
+  <g
+     inkscape:label="Layer 1"
+     inkscape:groupmode="layer"
+     id="layer1">
+    <circle
+       style="fill:#494949;stroke:#000000;stroke-width:10.58333333;stroke-dasharray:none"
+       id="path1"
+       cx="500"
+       cy="500"
+       r="320.36685" />
+  </g>
+</svg>
index c0d8c754e5bc3bd7df9d1b9deaf54e14317c7f65..f93eaf9a8d450a8838412d92e9e2f2616dd485bd 100644 (file)
@@ -19,7 +19,9 @@ pub struct Player {
 }
 
 #[derive(Component)]
-pub struct Asteroid;
+pub struct Asteroid {
+    pub health: i8,
+}
 
 #[derive(Component)]
 pub struct Collider {
index c2a4d425c95086c85451ac3485589049c29b86e7..e7e628d3824b4dcae4f7ab33e3ba90e9129c756c 100644 (file)
@@ -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<AssetServer>,
@@ -67,7 +68,7 @@ pub fn setup(mut commands: Commands, window_size: Res<WindowSize>, 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<WindowSize>, sprites: Res<
             },
         ));
     }
-
 }
 
 pub fn player_movement_system(
@@ -214,8 +214,9 @@ fn cleanup<T: Component>(commands: &mut Commands, query: Query<Entity, With<T>>)
 pub fn collision_system(
     mut commands: Commands,
     mut player_query: Query<(Entity, &Transform, &Collider, &mut Player)>,
-    asteroid_query: Query<(Entity, &Transform, &Collider), With<Asteroid>>,
+    asteroid_query: Query<(Entity, &Transform, &Collider, &Asteroid, &Velocity)>,
     bullet_query: Query<(Entity, &Transform, &Collider), With<Bullet>>,
+    sprites: Res<Sprites>,
     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<NextState<AppState>>,
     current_state: Res<State<AppState>>,
@@ -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<Entity, With<Node>>) {
     cleanup(&mut commands, query);
 }
-