From 292e4073717cfb19598a7a104e655cdada560e16 Mon Sep 17 00:00:00 2001 From: NIMFER Date: Fri, 29 Mar 2024 04:04:54 +0100 Subject: [PATCH] added a few tasks for fmt, plus made fmt clean up the code --- .vscode/tasks.json | 18 +++++++++ src/charcacter_controller.rs | 78 +++++++++++++++++++++--------------- src/input_processor.rs | 5 +-- src/lib.rs | 35 ++++++++-------- src/player_look.rs | 43 ++++++++++---------- 5 files changed, 105 insertions(+), 74 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 683cde1..7856b66 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -30,6 +30,24 @@ "isDefault": true }, "problemMatcher": [] + }, + { + "label": "check rust formatting", + "type": "shell", + "command": "nix-shell", + "args": [ + "--run", + "steam-run cargo fmt --all -- --check" + ] + }, + { + "label": "format rust", + "type": "shell", + "command": "nix-shell", + "args": [ + "--run", + "steam-run cargo fmt --all" + ] } ] } \ No newline at end of file diff --git a/src/charcacter_controller.rs b/src/charcacter_controller.rs index af54e98..5cdf0d0 100644 --- a/src/charcacter_controller.rs +++ b/src/charcacter_controller.rs @@ -15,9 +15,9 @@ You should have received a copy of the GNU General Public License 3.0 along with this program. If not, see . */ +use crate::{input_processor::*, player_look::*}; use bevy::{prelude::*, transform::commands}; use bevy_xpbd_3d::{math::*, prelude::*}; -use crate::{input_processor::*, player_look::*}; pub struct CharacterControllerPlugin; @@ -61,7 +61,7 @@ pub struct CharacterControllerBundle { ground_caster: ShapeCaster, locked_axis: LockedAxes, restitution: Restitution, - player_look_rotatatable: PlayerLookRotatatable + player_look_rotatatable: PlayerLookRotatatable, } impl CharacterControllerBundle { @@ -136,32 +136,45 @@ fn move_character( )>, ) { let delta_time = time.delta_seconds_f64().adjust_precision(); - + for event in movement_event_reader.read() { - for (mut velocity, character_controller, is_grounded, rotation, shape_hits, character_transform) in &mut query + for ( + mut velocity, + character_controller, + is_grounded, + rotation, + shape_hits, + character_transform, + ) in &mut query { match event { MovementAction::Move(direction) => { // Get the forward direction of the character in the local coordinate system let forward = character_transform.forward().xyz().normalize(); - + // Calculate the movement in the local coordinate system - let local_movement = forward * direction.y * character_controller.movement_acceleration * delta_time + - character_transform.rotation.mul_vec3(Vec3::new(direction.x, 0.0, 0.0)) - * character_controller.movement_acceleration * delta_time; - + let local_movement = forward + * direction.y + * character_controller.movement_acceleration + * delta_time + + character_transform + .rotation + .mul_vec3(Vec3::new(direction.x, 0.0, 0.0)) + * character_controller.movement_acceleration + * delta_time; + if is_grounded { let mut slope_factor = 1.0; for hit in shape_hits.iter() { let angle = rotation.rotate(-hit.normal2).angle_between(Vector::Y).abs(); if angle <= character_controller.max_slope_angle { - slope_factor *= (angle / character_controller.max_slope_angle).cos(); - + slope_factor *= + (angle / character_controller.max_slope_angle).cos(); + // Update linear velocity velocity.x += local_movement.x * slope_factor; velocity.z += local_movement.z * slope_factor; - } else { velocity.x = 0.0; velocity.z = 0.0; @@ -171,7 +184,6 @@ fn move_character( velocity.x += local_movement.x * character_controller.air_control_factor; velocity.z += local_movement.z * character_controller.air_control_factor; } - } MovementAction::Jump => { if is_grounded { @@ -197,24 +209,24 @@ pub fn spawn_player( mut materials: ResMut>, camera_hight: f32, character_bundle: CharacterControllerBundle, -){ - commands.spawn(( - character_bundle, - PbrBundle { - mesh: meshes.add(Capsule3d::new(0.5, 1.0)), - material: materials.add(Color::rgb_u8(124, 144, 255)), - transform: Transform::from_xyz(0.0, 5.0, 0.0), - ..default() - } - )).with_children(|parrent|{ - parrent.spawn(( - PlayerCamera::default(), - Camera3dBundle { - transform: Transform::from_xyz(0., camera_hight, 0.), +) { + commands + .spawn(( + character_bundle, + PbrBundle { + mesh: meshes.add(Capsule3d::new(0.5, 1.0)), + material: materials.add(Color::rgb_u8(124, 144, 255)), + transform: Transform::from_xyz(0.0, 5.0, 0.0), ..default() - }) - ); - }); - - -} \ No newline at end of file + }, + )) + .with_children(|parrent| { + parrent.spawn(( + PlayerCamera::default(), + Camera3dBundle { + transform: Transform::from_xyz(0., camera_hight, 0.), + ..default() + }, + )); + }); +} diff --git a/src/input_processor.rs b/src/input_processor.rs index d6677d4..e57c3d2 100644 --- a/src/input_processor.rs +++ b/src/input_processor.rs @@ -25,9 +25,9 @@ pub enum MovementAction { } #[derive(Event)] -pub enum LookAction{ +pub enum LookAction { Mouse(Vec2), - Gamepad(Vec2) + Gamepad(Vec2), } pub struct InputProcessorPlugin; @@ -61,4 +61,3 @@ fn keyboard_input( movement_event_writer.send(MovementAction::Jump); } } - diff --git a/src/lib.rs b/src/lib.rs index 1bfa41c..57aa7c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,12 +23,12 @@ use bevy::prelude::*; use bevy_editor_pls::prelude::*; use bevy_xpbd_3d::prelude::*; -mod input_processor; mod charcacter_controller; +mod input_processor; mod player_look; -use input_processor::*; use charcacter_controller::*; +use input_processor::*; use player_look::*; #[derive(Resource, Default, Debug)] @@ -38,9 +38,14 @@ pub struct GamePlugin; impl Plugin for GamePlugin { fn build(&self, app: &mut App) { - app.add_plugins((PhysicsPlugins::default(), InputProcessorPlugin, CharacterControllerPlugin, PlayerLookPlugin)) - .add_systems(Startup, setup) - .add_systems(Update, spawn_map); + app.add_plugins(( + PhysicsPlugins::default(), + InputProcessorPlugin, + CharacterControllerPlugin, + PlayerLookPlugin, + )) + .add_systems(Startup, setup) + .add_systems(Update, spawn_map); #[cfg(debug_assertions)] { @@ -55,7 +60,6 @@ impl Plugin for GamePlugin { } } - /// set up a simple 3D scene fn setup( mut commands: Commands, @@ -71,17 +75,14 @@ fn setup( transform: Transform::from_xyz(4.0, 8.0, 4.0), ..default() }); - //player - spawn_player( - commands, - meshes, - materials, - 0.5, - CharacterControllerBundle::new( - Collider::capsule(1.0, 0.5), - CharacterController::default() - ) - ); + //player + spawn_player( + commands, + meshes, + materials, + 0.5, + CharacterControllerBundle::new(Collider::capsule(1.0, 0.5), CharacterController::default()), + ); } fn spawn_map( diff --git a/src/player_look.rs b/src/player_look.rs index 6514fda..7bec026 100644 --- a/src/player_look.rs +++ b/src/player_look.rs @@ -15,6 +15,7 @@ You should have received a copy of the GNU General Public License 3.0 along with this program. If not, see . */ +use crate::{charcacter_controller, input_processor::*, CharacterController}; use bevy::{ input::{ mouse::{MouseButtonInput, MouseMotion, MouseWheel}, @@ -22,7 +23,6 @@ use bevy::{ }, prelude::*, }; -use crate::{charcacter_controller, input_processor::*, CharacterController}; pub struct PlayerLookPlugin; @@ -30,7 +30,6 @@ impl Plugin for PlayerLookPlugin { fn build(&self, app: &mut App) { app.add_systems(Update, rotate_camera); app.add_event::(); - } } @@ -39,58 +38,60 @@ impl Plugin for PlayerLookPlugin { pub struct PlayerLookRotatatable; #[derive(Component)] -pub struct PlayerCamera{ +pub struct PlayerCamera { mouse_gain: f32, rotation: Vec3, rotation_limit_top: f32, rotation_limit_bottom: f32, } -impl Default for PlayerCamera{ +impl Default for PlayerCamera { fn default() -> Self { - Self{ + Self { mouse_gain: 0.005, - rotation: Vec3 { x: 0.0, y: 0.0, z: 0.0 }, + rotation: Vec3 { + x: 0.0, + y: 0.0, + z: 0.0, + }, rotation_limit_top: 1.0, rotation_limit_bottom: -1.0, } } } - fn rotate_camera( time: Res