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
|
"isDefault": true
|
||||||
},
|
},
|
||||||
"problemMatcher": []
|
"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>.
|
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::{prelude::*, transform::commands};
|
||||||
use bevy_xpbd_3d::{math::*, prelude::*};
|
use bevy_xpbd_3d::{math::*, prelude::*};
|
||||||
use crate::{input_processor::*, player_look::*};
|
|
||||||
|
|
||||||
pub struct CharacterControllerPlugin;
|
pub struct CharacterControllerPlugin;
|
||||||
|
|
||||||
|
|
@ -61,7 +61,7 @@ pub struct CharacterControllerBundle {
|
||||||
ground_caster: ShapeCaster,
|
ground_caster: ShapeCaster,
|
||||||
locked_axis: LockedAxes,
|
locked_axis: LockedAxes,
|
||||||
restitution: Restitution,
|
restitution: Restitution,
|
||||||
player_look_rotatatable: PlayerLookRotatatable
|
player_look_rotatatable: PlayerLookRotatatable,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CharacterControllerBundle {
|
impl CharacterControllerBundle {
|
||||||
|
|
@ -138,7 +138,14 @@ fn move_character(
|
||||||
let delta_time = time.delta_seconds_f64().adjust_precision();
|
let delta_time = time.delta_seconds_f64().adjust_precision();
|
||||||
|
|
||||||
for event in movement_event_reader.read() {
|
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 {
|
match event {
|
||||||
MovementAction::Move(direction) => {
|
MovementAction::Move(direction) => {
|
||||||
|
|
@ -146,9 +153,15 @@ fn move_character(
|
||||||
let forward = character_transform.forward().xyz().normalize();
|
let forward = character_transform.forward().xyz().normalize();
|
||||||
|
|
||||||
// Calculate the movement in the local coordinate system
|
// Calculate the movement in the local coordinate system
|
||||||
let local_movement = forward * direction.y * character_controller.movement_acceleration * delta_time +
|
let local_movement = forward
|
||||||
character_transform.rotation.mul_vec3(Vec3::new(direction.x, 0.0, 0.0))
|
* direction.y
|
||||||
* character_controller.movement_acceleration * delta_time;
|
* 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 {
|
if is_grounded {
|
||||||
let mut slope_factor = 1.0;
|
let mut slope_factor = 1.0;
|
||||||
|
|
@ -156,12 +169,12 @@ fn move_character(
|
||||||
let angle =
|
let angle =
|
||||||
rotation.rotate(-hit.normal2).angle_between(Vector::Y).abs();
|
rotation.rotate(-hit.normal2).angle_between(Vector::Y).abs();
|
||||||
if angle <= character_controller.max_slope_angle {
|
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
|
// Update linear velocity
|
||||||
velocity.x += local_movement.x * slope_factor;
|
velocity.x += local_movement.x * slope_factor;
|
||||||
velocity.z += local_movement.z * slope_factor;
|
velocity.z += local_movement.z * slope_factor;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
velocity.x = 0.0;
|
velocity.x = 0.0;
|
||||||
velocity.z = 0.0;
|
velocity.z = 0.0;
|
||||||
|
|
@ -171,7 +184,6 @@ fn move_character(
|
||||||
velocity.x += local_movement.x * character_controller.air_control_factor;
|
velocity.x += local_movement.x * character_controller.air_control_factor;
|
||||||
velocity.z += local_movement.z * character_controller.air_control_factor;
|
velocity.z += local_movement.z * character_controller.air_control_factor;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
MovementAction::Jump => {
|
MovementAction::Jump => {
|
||||||
if is_grounded {
|
if is_grounded {
|
||||||
|
|
@ -197,24 +209,24 @@ pub fn spawn_player(
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
camera_hight: f32,
|
camera_hight: f32,
|
||||||
character_bundle: CharacterControllerBundle,
|
character_bundle: CharacterControllerBundle,
|
||||||
){
|
) {
|
||||||
commands.spawn((
|
commands
|
||||||
|
.spawn((
|
||||||
character_bundle,
|
character_bundle,
|
||||||
PbrBundle {
|
PbrBundle {
|
||||||
mesh: meshes.add(Capsule3d::new(0.5, 1.0)),
|
mesh: meshes.add(Capsule3d::new(0.5, 1.0)),
|
||||||
material: materials.add(Color::rgb_u8(124, 144, 255)),
|
material: materials.add(Color::rgb_u8(124, 144, 255)),
|
||||||
transform: Transform::from_xyz(0.0, 5.0, 0.0),
|
transform: Transform::from_xyz(0.0, 5.0, 0.0),
|
||||||
..default()
|
..default()
|
||||||
}
|
},
|
||||||
)).with_children(|parrent|{
|
))
|
||||||
|
.with_children(|parrent| {
|
||||||
parrent.spawn((
|
parrent.spawn((
|
||||||
PlayerCamera::default(),
|
PlayerCamera::default(),
|
||||||
Camera3dBundle {
|
Camera3dBundle {
|
||||||
transform: Transform::from_xyz(0., camera_hight, 0.),
|
transform: Transform::from_xyz(0., camera_hight, 0.),
|
||||||
..default()
|
..default()
|
||||||
})
|
},
|
||||||
);
|
));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -25,9 +25,9 @@ pub enum MovementAction {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Event)]
|
#[derive(Event)]
|
||||||
pub enum LookAction{
|
pub enum LookAction {
|
||||||
Mouse(Vec2),
|
Mouse(Vec2),
|
||||||
Gamepad(Vec2)
|
Gamepad(Vec2),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct InputProcessorPlugin;
|
pub struct InputProcessorPlugin;
|
||||||
|
|
@ -61,4 +61,3 @@ fn keyboard_input(
|
||||||
movement_event_writer.send(MovementAction::Jump);
|
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_editor_pls::prelude::*;
|
||||||
use bevy_xpbd_3d::prelude::*;
|
use bevy_xpbd_3d::prelude::*;
|
||||||
|
|
||||||
mod input_processor;
|
|
||||||
mod charcacter_controller;
|
mod charcacter_controller;
|
||||||
|
mod input_processor;
|
||||||
mod player_look;
|
mod player_look;
|
||||||
|
|
||||||
use input_processor::*;
|
|
||||||
use charcacter_controller::*;
|
use charcacter_controller::*;
|
||||||
|
use input_processor::*;
|
||||||
use player_look::*;
|
use player_look::*;
|
||||||
|
|
||||||
#[derive(Resource, Default, Debug)]
|
#[derive(Resource, Default, Debug)]
|
||||||
|
|
@ -38,7 +38,12 @@ pub struct GamePlugin;
|
||||||
|
|
||||||
impl Plugin for GamePlugin {
|
impl Plugin for GamePlugin {
|
||||||
fn build(&self, app: &mut App) {
|
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(Startup, setup)
|
||||||
.add_systems(Update, spawn_map);
|
.add_systems(Update, spawn_map);
|
||||||
|
|
||||||
|
|
@ -55,7 +60,6 @@ impl Plugin for GamePlugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// set up a simple 3D scene
|
/// set up a simple 3D scene
|
||||||
fn setup(
|
fn setup(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
|
|
@ -77,10 +81,7 @@ fn setup(
|
||||||
meshes,
|
meshes,
|
||||||
materials,
|
materials,
|
||||||
0.5,
|
0.5,
|
||||||
CharacterControllerBundle::new(
|
CharacterControllerBundle::new(Collider::capsule(1.0, 0.5), CharacterController::default()),
|
||||||
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>.
|
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::{
|
use bevy::{
|
||||||
input::{
|
input::{
|
||||||
mouse::{MouseButtonInput, MouseMotion, MouseWheel},
|
mouse::{MouseButtonInput, MouseMotion, MouseWheel},
|
||||||
|
|
@ -22,7 +23,6 @@ use bevy::{
|
||||||
},
|
},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
use crate::{charcacter_controller, input_processor::*, CharacterController};
|
|
||||||
|
|
||||||
pub struct PlayerLookPlugin;
|
pub struct PlayerLookPlugin;
|
||||||
|
|
||||||
|
|
@ -30,7 +30,6 @@ impl Plugin for PlayerLookPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(Update, rotate_camera);
|
app.add_systems(Update, rotate_camera);
|
||||||
app.add_event::<LookAction>();
|
app.add_event::<LookAction>();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,58 +38,60 @@ impl Plugin for PlayerLookPlugin {
|
||||||
pub struct PlayerLookRotatatable;
|
pub struct PlayerLookRotatatable;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct PlayerCamera{
|
pub struct PlayerCamera {
|
||||||
mouse_gain: f32,
|
mouse_gain: f32,
|
||||||
rotation: Vec3,
|
rotation: Vec3,
|
||||||
rotation_limit_top: f32,
|
rotation_limit_top: f32,
|
||||||
rotation_limit_bottom: f32,
|
rotation_limit_bottom: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for PlayerCamera{
|
impl Default for PlayerCamera {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self{
|
Self {
|
||||||
mouse_gain: 0.005,
|
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_top: 1.0,
|
||||||
rotation_limit_bottom: -1.0,
|
rotation_limit_bottom: -1.0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn rotate_camera(
|
fn rotate_camera(
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
mut mouse_motion_events: EventReader<MouseMotion>,
|
mut mouse_motion_events: EventReader<MouseMotion>,
|
||||||
mut camera_query: Query<(&mut Transform, &mut PlayerCamera), With<Camera3d>>,
|
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,
|
mut gizmos: Gizmos,
|
||||||
){
|
) {
|
||||||
let delta_time = time.delta_seconds_f64();
|
let delta_time = time.delta_seconds_f64();
|
||||||
|
|
||||||
for (mut transform, mut camera) in &mut camera_query {
|
for (mut transform, mut camera) in &mut camera_query {
|
||||||
|
|
||||||
for event in mouse_motion_events.read() {
|
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 -= 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);
|
let x_rotation = Quat::from_axis_angle(Vec3::X, camera.rotation.x);
|
||||||
info!("{}", x_rotation);
|
info!("{}", x_rotation);
|
||||||
|
|
||||||
transform.rotation = x_rotation;
|
transform.rotation = x_rotation;
|
||||||
|
|
||||||
|
|
||||||
camera.rotation.y -= event.delta.x * camera.mouse_gain;
|
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);
|
info!("{}", y_rotation);
|
||||||
|
|
||||||
rotatable.rotation = y_rotation;
|
rotatable.rotation = y_rotation;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue