2
1

2 Achegas b3cc625709 ... acb5496ca0

Autor SHA1 Mensaxe Data
  wullewutz acb5496ca0 Update README %!s(int64=2) %!d(string=hai) anos
  wullewutz 716c327dcb Working thrusters %!s(int64=2) %!d(string=hai) anos
Modificáronse 4 ficheiros con 298 adicións e 221 borrados
  1. 236 194
      Cargo.lock
  2. 1 1
      Cargo.toml
  3. 6 3
      README.md
  4. 55 23
      src/main.rs

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 236 - 194
Cargo.lock


+ 1 - 1
Cargo.toml

@@ -6,4 +6,4 @@ edition = "2021"
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
-bevy = "0.11"
+bevy = "0.12"

+ 6 - 3
README.md

@@ -1,8 +1,6 @@
 # void
 
-Most parts of the universe are void.
-
-In the rest, there are spaceships.
+Most parts of the universe are void. The rest are spaceships.
 
 ## Build
 
@@ -14,3 +12,8 @@ cd void
 cargo run
 ```
 
+### How to play
+
+Use A and D to rotate ship.
+
+Use W to create forward thrust with the main engine.

+ 55 - 23
src/main.rs

@@ -1,6 +1,7 @@
 use bevy::prelude::*;
 
-const TIME_STEP: f32 = 1.0 / 60.0;
+const RCS_THRUST: f32 = 0.5;
+const MAIN_THRUST: f32 = 1.0;
 
 fn main() {
     App::new()
@@ -10,9 +11,13 @@ fn main() {
 }
 
 #[derive(Component)]
-struct Position {
-    x: f32,
-    y: f32,
+struct Velocity {
+    v: Vec3,
+}
+
+#[derive(Component)]
+struct Rotation {
+    r: f32, // radiants per second
 }
 
 #[derive(Component)]
@@ -29,13 +34,14 @@ pub struct ShipsPlugin;
 impl Plugin for ShipsPlugin {
     fn build(&self, app: &mut App) {
         app.insert_resource(TelemetryTimer(Timer::from_seconds(
-            2.0,
+            1.0,
             TimerMode::Repeating,
         )))
-        .insert_resource(FixedTime::new_from_secs(TIME_STEP))
+        //.insert_resource(FixedTime::new_from_secs(TIME_STEP))
         .add_systems(Startup, spawn_ships)
         .add_systems(Update, print_telemetry)
-        .add_systems(Update, move_ship);
+        .add_systems(Update, apply_thrust)
+        .add_systems(Update, rotate_ship);
     }
 }
 
@@ -45,43 +51,69 @@ fn spawn_ships(mut commands: Commands, asset_server: Res<AssetServer>) {
     commands.spawn((
         SpriteBundle {
             texture: ship_texture,
-            transform: Transform::from_xyz(100., 100., 0.),
+            transform: Transform::from_xyz(100., 100., 0.).with_scale(Vec3::new(0.2, 0.2, 0.2)),
             ..default()
         },
         Ship,
         Name("Player".to_string()),
-        Position { x: 100.0, y: 100.0 },
+        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>,
-    query: Query<(&Position, &Name), With<Ship>>,
+    q: Query<(&Name, &Velocity, &Rotation, &Transform), With<Ship>>,
 ) {
     if timer.0.tick(time.delta()).just_finished() {
-        for (position, name) in &query {
-            println!("{}: x = {}, y = {}", name.0, position.x, position.y);
+        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 move_ship(
+fn rotate_ship(
     time: Res<Time>,
-    mut sprite_position: Query<(&mut Position, &mut Transform), With<Ship>>,
-    input: Res<Input<KeyCode>>,
+    mut q: Query<(&mut Rotation, &mut Transform), With<Ship>>,
+    keyboard: Res<Input<KeyCode>>,
 ) {
-    for (mut pos, mut transform) in &mut sprite_position {
-        for k in input.get_pressed() {
+    for (mut rot, mut transform) in &mut q {
+        for k in keyboard.get_pressed() {
             match k {
-                KeyCode::S => pos.y -= 150. * time.delta_seconds(),
-                KeyCode::W => pos.y += 150. * time.delta_seconds(),
-                KeyCode::A => pos.x -= 150. * time.delta_seconds(),
-                KeyCode::D => pos.x += 150. * time.delta_seconds(),
+                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.translation.x = pos.x;
-        transform.translation.y = pos.y;
+        transform.rotate_z(rot.r);
     }
 }

Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio