added a few tasks for fmt, plus made fmt clean up the code

This commit is contained in:
NIMFER 2024-03-29 04:04:54 +01:00
parent 697af6868e
commit 292e407371
5 changed files with 105 additions and 74 deletions

18
.vscode/tasks.json vendored
View file

@ -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"
]
}
]
}

View file

@ -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 <https://www.gnu.org/licenses/gpl-3.0.html>.
*/
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<Assets<StandardMaterial>>,
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()
})
);
});
}
},
))
.with_children(|parrent| {
parrent.spawn((
PlayerCamera::default(),
Camera3dBundle {
transform: Transform::from_xyz(0., camera_hight, 0.),
..default()
},
));
});
}

View file

@ -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);
}
}

View file

@ -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(

View file

@ -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 <https://www.gnu.org/licenses/gpl-3.0.html>.
*/
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::<LookAction>();
}
}
@ -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<Time>,
mut mouse_motion_events: EventReader<MouseMotion>,
mut camera_query: Query<(&mut Transform, &mut PlayerCamera), With<Camera3d>>,
mut rotatable_query: Query<&mut Transform, (With<PlayerLookRotatatable>, Without<PlayerCamera>)>,
mut rotatable_query: Query<
&mut Transform,
(With<PlayerLookRotatatable>, Without<PlayerCamera>),
>,
mut gizmos: Gizmos,
){
) {
let delta_time = time.delta_seconds_f64();
for (mut transform, mut camera) in &mut camera_query {
for event in mouse_motion_events.read() {
for mut rotatable in &mut rotatable_query{
for mut rotatable in &mut rotatable_query {
camera.rotation.x -= event.delta.y * camera.mouse_gain;
camera.rotation.x = f32::clamp(camera.rotation.x, camera.rotation_limit_bottom, camera.rotation_limit_top);
camera.rotation.x = f32::clamp(
camera.rotation.x,
camera.rotation_limit_bottom,
camera.rotation_limit_top,
);
let x_rotation = Quat::from_axis_angle(Vec3::X, camera.rotation.x);
info!("{}", x_rotation);
transform.rotation = x_rotation;
camera.rotation.y -= event.delta.x * camera.mouse_gain;
let y_rotation = Quat::from_axis_angle(Vec3::Y,camera.rotation.y);
let y_rotation = Quat::from_axis_angle(Vec3::Y, camera.rotation.y);
info!("{}", y_rotation);
rotatable.rotation = y_rotation;
}
}
}
}