Quellcode durchsuchen

KSP like main engine thrust controls

wullewutz vor 2 Jahren
Ursprung
Commit
411eb48e6c
3 geänderte Dateien mit 21 neuen und 10 gelöschten Zeilen
  1. 3 1
      README.md
  2. 5 0
      src/body.rs
  3. 13 9
      src/ship.rs

+ 3 - 1
README.md

@@ -16,4 +16,6 @@ cargo run
 
 Use A and D to rotate ship.
 
-Use W to create forward thrust with the main engine.
+LeftShift to increase main engine thrust.
+LeftCtrl to decrease main engine thrust.
+X to shut down main engine completely.

+ 5 - 0
src/body.rs

@@ -9,3 +9,8 @@ pub struct Velocity {
 pub struct Rotation {
     pub r: f32, // radiants per second
 }
+
+#[derive(Component)]
+pub struct Thrust {
+    pub t: f32,
+}

+ 13 - 9
src/ship.rs

@@ -1,4 +1,4 @@
-use crate::body::{Rotation, Velocity};
+use crate::body::{Rotation, Thrust, Velocity};
 use bevy::prelude::*;
 
 pub struct ShipsPlugin;
@@ -13,7 +13,7 @@ struct Name(String);
 struct TelemetryTimer(Timer);
 
 const RCS_THRUST: f32 = 0.5;
-const MAIN_THRUST: f32 = 1.0;
+const MAIN_THRUST: f32 = 0.1;
 
 impl Plugin for ShipsPlugin {
     fn build(&self, app: &mut App) {
@@ -39,27 +39,31 @@ fn spawn_ships(mut commands: Commands, asset_server: Res<AssetServer>) {
         Ship,
         Name("Player".to_string()),
         Velocity {
-            v: Vec3::new(0.0, 0.0, 0.0),
+            v: Vec3::new(0., 0., 0.),
         },
         Rotation {
             r: f32::to_radians(0.),
         },
+        Thrust { t: 0. },
     ));
 }
 
 fn apply_thrust(
     time: Res<Time>,
-    mut q: Query<(&mut Velocity, &mut Transform), With<Ship>>,
+    mut q: Query<(&mut Velocity, &mut Thrust, &mut Transform), With<Ship>>,
     keyboard: Res<Input<KeyCode>>,
 ) {
-    let mut thrust = Vec3::ZERO;
-    for (mut vel, mut trans) in &mut q {
+    for (mut vel, mut thr, 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();
+            match k {
+                KeyCode::ShiftLeft => thr.t += MAIN_THRUST * time.delta_seconds(),
+                KeyCode::ControlLeft => thr.t -= MAIN_THRUST * time.delta_seconds(),
+                KeyCode::X => thr.t = 0.,
+                _ => (),
             }
         }
-        vel.v += thrust;
+        thr.t = thr.t.clamp(0., 100.);
+        vel.v += (trans.rotation * Vec3::Y) * thr.t;
         trans.translation += vel.v;
     }
 }