Prechádzať zdrojové kódy

added player movement

Clemsim 2 rokov pred
rodič
commit
8755002b14
4 zmenil súbory, kde vykonal 80 pridanie a 50 odobranie
  1. 1 0
      src/config.rs
  2. 34 50
      src/main.rs
  3. 1 0
      src/player/mod.rs
  4. 44 0
      src/player/movement.rs

+ 1 - 0
src/config.rs

@@ -0,0 +1 @@
+pub const SPEED : f32 = 5.0;

+ 34 - 50
src/main.rs

@@ -1,56 +1,40 @@
 use bevy::prelude::*;
 
-struct Plane {
-    mesh: Mesh,
-    vertex_buffer: VertexBuffer,
+mod config;
+mod player;
+
+fn main(){
+    App::new()
+        .add_plugins(DefaultPlugins)
+        .add_systems(Startup, setup1)
+        .add_systems(Update, player::movement::player_movement);
 }
 
-impl Plane {
-    fn new(app: &mut App) -> Self {
-        let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
-        mesh.add_vertices(&[
-            Vertex::new(Position([0.0, 0.0, 0.0]), Normal([0.0, 0.0, 1.0]), Uv([0.0, 0.0])),
-            Vertex::new(Position([10.0, 0.0, 0.0]), Normal([0.0, 0.0, 1.0]), Uv([1.0, 0.0])),
-            Vertex::new(Position([10.0, 10.0, 0.0]), Normal([0.0, 0.0, 1.0]), Uv([1.0, 1.0])),
-        ]);
-        mesh.set_indices(&[0, 1, 2]);
-
-        let vertex_buffer = VertexBuffer::from_mesh(mesh);
-
-        Self { mesh, vertex_buffer }
-    }
-
-    fn update_vertex_positions(&self, time: f32) {
-        let amplitude = 1.0;
-        let frequency = 1.0;
-
-        for vertex in self.vertex_buffer.iter_mut() {
-            vertex.position[0] += amplitude * (f32::sin(frequency * time) - f32::sin(frequency * (time - 1.0)));
-        }
-    }
-}
-
-fn main() {
-    let mut app = App::new();
-
-    app.add_startup_system(setup);
-    app.add_system(update);
-
-    app.run();
-}
-
-fn setup(mut commands: Commands) {
-    let plane = Plane::new(&mut commands);
-
-    commands.spawn_bundle((
-        plane.mesh,
-        plane.vertex_buffer,
-        GlobalTransform::identity(),
+fn setup1(
+    mut commands: Commands,
+    mut meshes : ResMut<Assets<Mesh>>,
+    mut materials : ResMut<Assets<StandardMaterial>>
+){
+    commands.spawn(PbrBundle{
+        mesh: meshes.add(Mesh::from(shape::Cube { size: 2.0 })),
+        material: materials.add(Color::rgb(0.5, 0.5, 1.0).into()),
+        ..Default::default()
+    });
+
+    commands.spawn((Camera3dBundle {
+        transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
+        ..default()
+    },player::movement::Player
     ));
-}
 
-fn update(time: Res<Time>, mut planes: Query<&mut Plane>) {
-    for mut plane in planes.iter_mut() {
-        plane.update_vertex_positions(time.seconds());
-    }
-}
+    commands.spawn(PointLightBundle {
+        point_light: PointLight {
+            intensity: 1500.0,
+            shadows_enabled: true,
+            ..default()
+        },
+        transform: Transform::from_xyz(4.0, 8.0, 4.0),
+        ..default()
+    });
+
+}

+ 1 - 0
src/player/mod.rs

@@ -0,0 +1 @@
+pub mod movement;

+ 44 - 0
src/player/movement.rs

@@ -0,0 +1,44 @@
+use bevy::prelude::*;
+use crate::config;
+
+#[derive(Component)]
+
+pub struct Player;
+
+
+pub fn player_movement(
+    keyboard_input: Res<Input<KeyCode>>,
+    mut player_query : Query<&mut Transform, With<Player>>,
+    time: Res<Time>
+){
+    if let Ok(mut transform) = player_query.get_single_mut() {
+        let mut direction = Vec3::ZERO;
+
+        if keyboard_input.pressed(KeyCode::Left) || keyboard_input.pressed(KeyCode::A){
+            direction += Vec3::new(-1.0, 0.0, 0.0)
+        }
+        if keyboard_input.pressed(KeyCode::Right) || keyboard_input.pressed(KeyCode::D){
+            direction += Vec3::new(1.0, 0.0, 0.0)
+        }
+        if keyboard_input.pressed(KeyCode::Up) || keyboard_input.pressed(KeyCode::W){
+            direction += Vec3::new(0.0, 0.0, -1.0)
+        }
+        if keyboard_input.pressed(KeyCode::Down) || keyboard_input.pressed(KeyCode::S){
+            direction += Vec3::new(0.0, 0.0, 1.0)
+        }
+        if keyboard_input.pressed(KeyCode::Space) {
+            direction += Vec3::new(0.0, 1.0, 0.0)
+        }
+        if keyboard_input.pressed(KeyCode::ShiftLeft) || keyboard_input.pressed(KeyCode::ShiftRight) {
+            direction += Vec3::new(0.0, -1.0, 0.0)
+        }
+        if direction.length()> 0.0{
+            direction = direction.normalize()
+        }
+
+
+        transform.translation += direction * (config::SPEED * time.delta_seconds())
+    }
+
+
+}