diff --git a/src/charcacter_controller.rs b/src/charcacter_controller.rs index 676ae2c..1977937 100644 --- a/src/charcacter_controller.rs +++ b/src/charcacter_controller.rs @@ -20,19 +20,10 @@ use bevy_xpbd_3d::{math::*, prelude::*}; pub struct CharacterControllerPlugin; -impl Plugin for CharacterControllerPlugin{ +impl Plugin for CharacterControllerPlugin { fn build(&self, app: &mut App) { - app.add_systems(Update,( - keyboard_input, - move_character, - ) - ); - app.add_systems(FixedUpdate, ( - - dampen_movement, - update_grounded - ) - ); + app.add_systems(Update, (keyboard_input, move_character)); + app.add_systems(FixedUpdate, (dampen_movement, update_grounded)); app.add_event::(); } } @@ -40,7 +31,7 @@ impl Plugin for CharacterControllerPlugin{ #[derive(Event)] pub enum MovementAction { Move(Vector2), - Jump + Jump, } #[derive(Component)] @@ -48,7 +39,7 @@ pub enum MovementAction { pub struct Grounded; #[derive(Component)] -pub struct CharacterController{ +pub struct CharacterController { pub movement_acceleration: Scalar, pub air_control_factor: Scalar, pub movement_dampening_factor: Scalar, @@ -58,7 +49,7 @@ pub struct CharacterController{ impl Default for CharacterController { fn default() -> Self { - CharacterController{ + CharacterController { movement_acceleration: 20.0, air_control_factor: 0.5, movement_dampening_factor: 0.95, @@ -69,7 +60,7 @@ impl Default for CharacterController { } #[derive(Bundle)] -pub struct CharacterControllerBundle{ +pub struct CharacterControllerBundle { character_controller: CharacterController, rigid_body: RigidBody, collider: Collider, @@ -83,8 +74,7 @@ impl CharacterControllerBundle { let mut caster_shape = controller_collider.clone(); caster_shape.set_scale(Vector::ONE * 0.99, 10); - - Self{ + Self { character_controller: controller, rigid_body: RigidBody::Dynamic, collider: controller_collider, @@ -93,7 +83,8 @@ impl CharacterControllerBundle { Vector::ZERO, Quaternion::default(), Direction3d::NEG_Y, - ).with_max_time_of_impact(0.2), + ) + .with_max_time_of_impact(0.2), locked_axis: LockedAxes::ROTATION_LOCKED, restitution: Restitution::new(0.0), } @@ -110,14 +101,18 @@ impl Default for CharacterControllerBundle { } } -pub fn update_grounded(mut commands: Commands, mut query: Query<(Entity, &CharacterController, &Rotation, &ShapeHits)>){ - +pub fn update_grounded( + mut commands: Commands, + mut query: Query<(Entity, &CharacterController, &Rotation, &ShapeHits)>, +) { for (entity, character_controller, rotation, hits) in &mut query { - let is_grounded = hits.iter().any(|hit| { let angle = rotation.rotate(-hit.normal2).angle_between(Vector::Y).abs(); - println!("slope angle: {} / {}", angle, character_controller.max_slope_angle); + println!( + "slope angle: {} / {}", + angle, character_controller.max_slope_angle + ); angle <= character_controller.max_slope_angle }); @@ -130,10 +125,12 @@ pub fn update_grounded(mut commands: Commands, mut query: Query<(Entity, &Charac commands.entity(entity).remove::(); } } - } -pub fn keyboard_input(mut movement_event_writer: EventWriter, keyboard_input: Res>){ +pub fn keyboard_input( + mut movement_event_writer: EventWriter, + keyboard_input: Res>, +) { let up = keyboard_input.any_pressed([KeyCode::KeyW, KeyCode::ArrowUp]); let down = keyboard_input.any_pressed([KeyCode::KeyS, KeyCode::ArrowDown]); let left = keyboard_input.any_pressed([KeyCode::KeyA, KeyCode::ArrowLeft]); @@ -155,9 +152,14 @@ pub fn keyboard_input(mut movement_event_writer: EventWriter, ke pub fn move_character( time: Res