added a few tasks for fmt, plus made fmt clean up the code
This commit is contained in:
parent
697af6868e
commit
292e407371
5 changed files with 105 additions and 74 deletions
18
.vscode/tasks.json
vendored
18
.vscode/tasks.json
vendored
|
|
@ -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"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
@ -138,7 +138,14 @@ 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) => {
|
||||
|
|
@ -146,9 +153,15 @@ fn move_character(
|
|||
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;
|
||||
|
|
@ -156,12 +169,12 @@ fn move_character(
|
|||
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 {
|
||||
|
|
@ -198,23 +210,23 @@ pub fn spawn_player(
|
|||
camera_hight: f32,
|
||||
character_bundle: CharacterControllerBundle,
|
||||
) {
|
||||
commands.spawn((
|
||||
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|{
|
||||
},
|
||||
))
|
||||
.with_children(|parrent| {
|
||||
parrent.spawn((
|
||||
PlayerCamera::default(),
|
||||
Camera3dBundle {
|
||||
transform: Transform::from_xyz(0., camera_hight, 0.),
|
||||
..default()
|
||||
})
|
||||
);
|
||||
},
|
||||
));
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -27,7 +27,7 @@ pub enum MovementAction {
|
|||
#[derive(Event)]
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
17
src/lib.rs
17
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,7 +38,12 @@ pub struct GamePlugin;
|
|||
|
||||
impl Plugin for GamePlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_plugins((PhysicsPlugins::default(), InputProcessorPlugin, CharacterControllerPlugin, PlayerLookPlugin))
|
||||
app.add_plugins((
|
||||
PhysicsPlugins::default(),
|
||||
InputProcessorPlugin,
|
||||
CharacterControllerPlugin,
|
||||
PlayerLookPlugin,
|
||||
))
|
||||
.add_systems(Startup, setup)
|
||||
.add_systems(Update, spawn_map);
|
||||
|
||||
|
|
@ -55,7 +60,6 @@ impl Plugin for GamePlugin {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/// set up a simple 3D scene
|
||||
fn setup(
|
||||
mut commands: Commands,
|
||||
|
|
@ -77,10 +81,7 @@ fn setup(
|
|||
meshes,
|
||||
materials,
|
||||
0.5,
|
||||
CharacterControllerBundle::new(
|
||||
Collider::capsule(1.0, 0.5),
|
||||
CharacterController::default()
|
||||
)
|
||||
CharacterControllerBundle::new(Collider::capsule(1.0, 0.5), CharacterController::default()),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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>();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -50,47 +49,49 @@ impl Default for PlayerCamera{
|
|||
fn default() -> 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 {
|
||||
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);
|
||||
info!("{}", y_rotation);
|
||||
|
||||
rotatable.rotation = y_rotation;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue