Преглед изворни кода

Code refactored into separate modules

wullewutz пре 2 година
родитељ
комит
156b2cefb8
4 измењених фајлова са 133 додато и 111 уклоњено
  1. 11 0
      src/body.rs
  2. 13 0
      src/camera.rs
  3. 7 111
      src/main.rs
  4. 102 0
      src/ship.rs

+ 11 - 0
src/body.rs

@@ -0,0 +1,11 @@
+use bevy::prelude::*;
+
+#[derive(Component)]
+pub struct Velocity {
+    pub v: Vec3,
+}
+
+#[derive(Component)]
+pub struct Rotation {
+    pub r: f32, // radiants per second
+}

+ 13 - 0
src/camera.rs

@@ -0,0 +1,13 @@
+use bevy::prelude::*;
+
+pub struct CameraPlugin;
+
+impl Plugin for CameraPlugin {
+    fn build(&self, app: &mut App) {
+        app.add_systems(Startup, spawn_camera);
+    }
+}
+
+fn spawn_camera(mut commands: Commands) {
+    commands.spawn(Camera2dBundle::default());
+}

+ 7 - 111
src/main.rs

@@ -1,119 +1,15 @@
-use bevy::prelude::*;
+mod body;
+mod camera;
+mod ship;
 
-const RCS_THRUST: f32 = 0.5;
-const MAIN_THRUST: f32 = 1.0;
+use bevy::prelude::*;
+use camera::CameraPlugin;
+use ship::ShipsPlugin;
 
 fn main() {
     App::new()
         .insert_resource(ClearColor(Color::rgb(0.3, 0.3, 0.4)))
+        .add_plugins(CameraPlugin)
         .add_plugins((DefaultPlugins, ShipsPlugin))
         .run();
 }
-
-#[derive(Component)]
-struct Velocity {
-    v: Vec3,
-}
-
-#[derive(Component)]
-struct Rotation {
-    r: f32, // radiants per second
-}
-
-#[derive(Component)]
-struct Ship;
-
-#[derive(Component)]
-struct Name(String);
-
-#[derive(Resource)]
-struct TelemetryTimer(Timer);
-
-pub struct ShipsPlugin;
-
-impl Plugin for ShipsPlugin {
-    fn build(&self, app: &mut App) {
-        app.insert_resource(TelemetryTimer(Timer::from_seconds(
-            1.0,
-            TimerMode::Repeating,
-        )))
-        //.insert_resource(FixedTime::new_from_secs(TIME_STEP))
-        .add_systems(Startup, spawn_ships)
-        .add_systems(Update, print_telemetry)
-        .add_systems(Update, apply_thrust)
-        .add_systems(Update, rotate_ship);
-    }
-}
-
-fn spawn_ships(mut commands: Commands, asset_server: Res<AssetServer>) {
-    let ship_texture = asset_server.load("ship.png");
-    commands.spawn(Camera2dBundle::default());
-    commands.spawn((
-        SpriteBundle {
-            texture: ship_texture,
-            transform: Transform::from_xyz(100., 100., 0.).with_scale(Vec3::new(0.2, 0.2, 0.2)),
-            ..default()
-        },
-        Ship,
-        Name("Player".to_string()),
-        Velocity {
-            v: Vec3::new(0.0, 0.0, 0.0),
-        },
-        Rotation {
-            r: f32::to_radians(0.),
-        },
-    ));
-}
-
-fn print_telemetry(
-    time: Res<Time>,
-    mut timer: ResMut<TelemetryTimer>,
-    q: Query<(&Name, &Velocity, &Rotation, &Transform), With<Ship>>,
-) {
-    if timer.0.tick(time.delta()).just_finished() {
-        for (name, vel, rot, trans) in &q {
-            println!(
-                "{}: v={:.4?}, speed={:.4}, point={:.4?}, rot={:.4}",
-                name.0,
-                vel.v.xy(),
-                vel.v.length(),
-                (trans.rotation * Vec3::Y).xy(),
-                rot.r
-            );
-        }
-    }
-}
-
-fn apply_thrust(
-    time: Res<Time>,
-    mut q: Query<(&mut Velocity, &mut Transform), With<Ship>>,
-    keyboard: Res<Input<KeyCode>>,
-) {
-    let mut thrust = Vec3::ZERO;
-    for (mut vel, mut trans) in &mut q {
-        for k in keyboard.get_pressed() {
-            if k == &KeyCode::W {
-                thrust = (trans.rotation * Vec3::Y) * MAIN_THRUST * time.delta_seconds();
-            }
-        }
-        vel.v += thrust;
-        trans.translation += vel.v;
-    }
-}
-
-fn rotate_ship(
-    time: Res<Time>,
-    mut q: Query<(&mut Rotation, &mut Transform), With<Ship>>,
-    keyboard: Res<Input<KeyCode>>,
-) {
-    for (mut rot, mut transform) in &mut q {
-        for k in keyboard.get_pressed() {
-            match k {
-                KeyCode::A => rot.r += f32::to_radians(RCS_THRUST) * time.delta_seconds(),
-                KeyCode::D => rot.r -= f32::to_radians(RCS_THRUST) * time.delta_seconds(),
-                _ => (),
-            }
-        }
-        transform.rotate_z(rot.r);
-    }
-}

+ 102 - 0
src/ship.rs

@@ -0,0 +1,102 @@
+use crate::body::{Rotation, Velocity};
+use bevy::prelude::*;
+
+pub struct ShipsPlugin;
+
+#[derive(Component)]
+struct Ship;
+
+#[derive(Component)]
+struct Name(String);
+
+#[derive(Resource)]
+struct TelemetryTimer(Timer);
+
+const RCS_THRUST: f32 = 0.5;
+const MAIN_THRUST: f32 = 1.0;
+
+impl Plugin for ShipsPlugin {
+    fn build(&self, app: &mut App) {
+        app.insert_resource(TelemetryTimer(Timer::from_seconds(
+            1.0,
+            TimerMode::Repeating,
+        )))
+        //.insert_resource(FixedTime::new_from_secs(TIME_STEP))
+        .add_systems(Startup, spawn_ships)
+        .add_systems(Update, print_telemetry)
+        .add_systems(Update, rotate_ship)
+        .add_systems(Update, apply_thrust);
+    }
+}
+
+fn spawn_ships(mut commands: Commands, asset_server: Res<AssetServer>) {
+    let ship_texture = asset_server.load("ship.png");
+    commands.spawn((
+        SpriteBundle {
+            texture: ship_texture,
+            transform: Transform::from_xyz(100., 100., 0.).with_scale(Vec3::new(0.2, 0.2, 0.2)),
+            ..default()
+        },
+        Ship,
+        Name("Player".to_string()),
+        Velocity {
+            v: Vec3::new(0.0, 0.0, 0.0),
+        },
+        Rotation {
+            r: f32::to_radians(0.),
+        },
+    ));
+}
+
+fn print_telemetry(
+    time: Res<Time>,
+    mut timer: ResMut<TelemetryTimer>,
+    q: Query<(&Name, &Velocity, &Rotation, &Transform), With<Ship>>,
+) {
+    if timer.0.tick(time.delta()).just_finished() {
+        for (name, vel, rot, trans) in &q {
+            println!(
+                "{}: v={:.4?}, speed={:.4}, point={:.4?}, rot={:.4}",
+                name.0,
+                vel.v.xy(),
+                vel.v.length(),
+                (trans.rotation * Vec3::Y).xy(),
+                rot.r
+            );
+        }
+    }
+}
+
+fn apply_thrust(
+    time: Res<Time>,
+    mut q: Query<(&mut Velocity, &mut Transform), With<Ship>>,
+    keyboard: Res<Input<KeyCode>>,
+) {
+    let mut thrust = Vec3::ZERO;
+    for (mut vel, mut trans) in &mut q {
+        for k in keyboard.get_pressed() {
+            if k == &KeyCode::W {
+                thrust = (trans.rotation * Vec3::Y) * MAIN_THRUST * time.delta_seconds();
+            }
+        }
+        vel.v += thrust;
+        trans.translation += vel.v;
+    }
+}
+
+fn rotate_ship(
+    time: Res<Time>,
+    mut q: Query<(&mut Rotation, &mut Transform), With<Ship>>,
+    keyboard: Res<Input<KeyCode>>,
+) {
+    for (mut rot, mut transform) in &mut q {
+        for k in keyboard.get_pressed() {
+            match k {
+                KeyCode::A => rot.r += f32::to_radians(RCS_THRUST) * time.delta_seconds(),
+                KeyCode::D => rot.r -= f32::to_radians(RCS_THRUST) * time.delta_seconds(),
+                _ => (),
+            }
+        }
+        transform.rotate_z(rot.r);
+    }
+}