3 Commits a58840a383 ... cfd447c88d

Author SHA1 Message Date
  Getty Ritter cfd447c88d add some basic camera movement 2 months ago
  Getty Ritter 782fd6be3b fmt 2 months ago
  Getty Ritter 0b00facf81 create something like a map 2 months ago
3 changed files with 122 additions and 18 deletions
  1. 37 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 84 18
      src/main.rs

+ 37 - 0
Cargo.lock

@@ -2788,6 +2788,12 @@ dependencies = [
  "unicode-xid",
 ]
 
+[[package]]
+name = "ppv-lite86"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
+
 [[package]]
 name = "presser"
 version = "0.3.1"
@@ -2843,6 +2849,36 @@ version = "0.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "17fd96390ed3feda12e1dfe2645ed587e0bea749e319333f104a33ff62f77a0b"
 
+[[package]]
+name = "rand"
+version = "0.8.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
+dependencies = [
+ "libc",
+ "rand_chacha",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
+dependencies = [
+ "ppv-lite86",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
+dependencies = [
+ "getrandom",
+]
+
 [[package]]
 name = "range-alloc"
 version = "0.1.3"
@@ -3116,6 +3152,7 @@ name = "stories"
 version = "0.1.0"
 dependencies = [
  "bevy",
+ "rand",
 ]
 
 [[package]]

+ 1 - 0
Cargo.toml

@@ -5,3 +5,4 @@ edition = "2021"
 
 [dependencies]
 bevy = "*"
+rand = "*"

+ 84 - 18
src/main.rs

@@ -1,21 +1,25 @@
 use bevy::prelude::*;
 
 #[derive(Component)]
-struct Rotator;
+struct Mover;
 
 fn main() {
     App::new()
+        .insert_resource(Movement { dx: 0.0, dy: 0.0 })
         .add_plugins(DefaultPlugins)
         .add_systems(Startup, setup)
-        .add_systems(Update, rotate)
+        .add_systems(Update, (input, decay, movement))
         .run();
 }
 
 fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
-    commands.spawn(Camera3dBundle {
-        transform: Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y),
-        ..default()
-    });
+    commands.spawn((
+        Camera3dBundle {
+            transform: Transform::from_xyz(0.0, 12.0, 18.0).looking_at(Vec3::ZERO, Vec3::Y),
+            ..default()
+        },
+        Mover,
+    ));
 
     commands.spawn(PointLightBundle {
         point_light: PointLight {
@@ -26,22 +30,84 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
         ..default()
     });
 
-    for x in -1..2 {
-        for y in -1..2 {
-            commands.spawn((
-                SceneBundle {
-                    scene: asset_server.load("test-cube.glb#Scene0"),
-                    transform: Transform::from_xyz((x * 3) as f32, 0.0, (y * 3) as f32),
-                    ..default()
-                },
-                Rotator,
-            ));
+    for x in 0..24 {
+        for y in 0..24 {
+            let x = x - 12;
+            let y = y - 12;
+            let z = if x == -12 || x == 11 || y == -12 || y == 11 || rand::random::<f32>() < 0.25 {
+                0.0
+            } else {
+                -2.0
+            };
+            let rotation = match rand::random::<u8>() % 4 {
+                0 => 0.0,
+                1 => 0.5,
+                2 => 1.0,
+                _ => 1.5,
+            } * std::f32::consts::PI;
+            commands.spawn(SceneBundle {
+                scene: asset_server.load("test-cube.glb#Scene0"),
+                transform: Transform::from_xyz((x * 2) as f32, z, (y * 2) as f32)
+                    .with_rotation(Quat::from_rotation_y(rotation)),
+                ..default()
+            });
         }
     }
 }
 
-fn rotate(mut query: Query<&mut Transform, With<Rotator>>, time: Res<Time>) {
+#[derive(Debug, Resource)]
+struct Movement {
+    dx: f32,
+    dy: f32,
+}
+
+impl Default for Movement {
+    fn default() -> Movement {
+        Movement { dx: 0.0, dy: 0.0 }
+    }
+}
+
+const ACCEL: f32 = 0.002;
+
+fn input(keyboard: Res<ButtonInput<KeyCode>>, mut mvmt: ResMut<Movement>) {
+    if keyboard.pressed(KeyCode::ArrowUp) {
+        mvmt.dy = (mvmt.dy - ACCEL).clamp(-0.5, 0.5);
+    }
+    if keyboard.pressed(KeyCode::ArrowDown) {
+        mvmt.dy = (mvmt.dy + ACCEL).clamp(-0.5, 0.5);
+    }
+    if keyboard.pressed(KeyCode::ArrowLeft) {
+        mvmt.dx = (mvmt.dx - ACCEL).clamp(-0.5, 0.5);
+    }
+    if keyboard.pressed(KeyCode::ArrowRight) {
+        mvmt.dx = (mvmt.dx + ACCEL).clamp(-0.5, 0.5);
+    }
+}
+
+fn decay(mut mvmt: ResMut<Movement>) {
+    if mvmt.dx.abs() < 0.001 {
+        mvmt.dx = 0.0;
+    } else {
+        mvmt.dx *= 0.90;
+    }
+
+    if mvmt.dy.abs() < 0.001 {
+        mvmt.dy = 0.0;
+    } else {
+        mvmt.dy *= 0.90;
+    }
+}
+
+fn movement(mut query: Query<&mut Transform, With<Mover>>, mvmt: Res<Movement>, time: Res<Time>) {
+    if time.delta_seconds() == 0.0 {
+        return;
+    }
+
     for mut transform in &mut query {
-        transform.rotate_y(time.delta_seconds() / 2.);
+        transform.translation += Vec3::new(
+            mvmt.dx / time.delta_seconds(),
+            0.0,
+            mvmt.dy / time.delta_seconds(),
+        );
     }
 }