orginizing project src

This commit is contained in:
Mikolaj Wojciech Gorski 2024-04-05 21:43:43 +02:00
parent bb8a94f07e
commit e62e15b7e5
6 changed files with 176 additions and 105 deletions

View file

@ -1,3 +1,9 @@
{ {
"rust-analyzer.debug.engine": "vadimcn.vscode-lldb", "rust-analyzer.debug.engine": "vadimcn.vscode-lldb",
"cSpell.words": [
"Gamepad",
"qeury",
"Substep",
"xpbd"
],
} }

View file

@ -58,8 +58,24 @@ The name is a bit confusing/misleading , so I am open for alternative names.
[SECTION STILL BEING WORKED ON] [SECTION STILL BEING WORKED ON]
#### Nix #### Nix
Enter the directory project directory and run `nix-shell` command and then you can use cargo as per usual`cargo run`, or use `nix-shell --run "cargo run"` if you don't want to enter the shell.
Enter the directory project directory and run `nix-shell` command, or use `nix-shell --run "cargo run"` if you don't want to enter the shell. #### Ubuntu
Fist you need to install rust, follow the instructions on the official rust lang website:
https://www.rust-lang.org/tools/install
Next, you need following packages:
- `pkg-config`
- `libx11-dev`
- `libasound2-dev`
- `libudev-dev`
you can get them via `apt`
```bash
apt-get update; apt-get install --no-install-recommends -y libasound2-dev libudev-dev libwayland-dev
```
now you should be good to go, and can run `cargo run` to build the project
### Building: ### Building:

View file

@ -23,9 +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::*;
use character_controller::*; mod weapon_framework;
use input_processor::*; mod player_utils;
use player_look::*;
use player_utils::character_controller::*;
use player_utils::input_processor::*;
use player_utils::player_look::*;
#[derive(Resource, Default, Debug)] #[derive(Resource, Default, Debug)]
struct IsMapSpawned(bool); struct IsMapSpawned(bool);

View file

@ -1,3 +1,3 @@
mod character_controller; pub mod character_controller;
mod input_processor; pub mod input_processor;
mod player_look; pub mod player_look;

View file

@ -15,8 +15,21 @@ 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 crate::player_utils::{input_processor::*, player_look::*};
use bevy::prelude::*; use bevy::{
prelude::*,
math::Vec3,
time::Time,
ecs::{
event::EventReader,
system::*,
query::*,
},
transform::components::Transform,
core_pipeline::core_3d::Camera3d,
gizmos::gizmos::Gizmos,
};
use bevy_xpbd_3d::{math::*, prelude::*, SubstepSchedule, SubstepSet}; use bevy_xpbd_3d::{math::*, prelude::*, SubstepSchedule, SubstepSet};
pub struct CharacterControllerPlugin; pub struct CharacterControllerPlugin;
@ -41,7 +54,9 @@ pub struct CharacterController {
pub jump_impulse: Scalar, pub jump_impulse: Scalar,
pub gravity: Vector, pub gravity: Vector,
pub max_slope_angle: Scalar, pub max_slope_angle: Scalar,
pub speed_limit: f32, pub axis_speed_limit: f32,
pub max_acceleration: f32,
pub friction: f32,
} }
impl Default for CharacterController { impl Default for CharacterController {
@ -53,7 +68,10 @@ impl Default for CharacterController {
jump_impulse: 4.0, jump_impulse: 4.0,
gravity: Vector::NEG_Y * 9.81 * 2.0, gravity: Vector::NEG_Y * 9.81 * 2.0,
max_slope_angle: (45.0 as Scalar).to_radians(), max_slope_angle: (45.0 as Scalar).to_radians(),
speed_limit: 6668.5, axis_speed_limit: 6668.5,
max_acceleration: 857.25,
friction: 0.0,
} }
} }
} }
@ -65,7 +83,7 @@ pub struct CharacterControllerBundle {
collider: Collider, collider: Collider,
ground_caster: ShapeCaster, ground_caster: ShapeCaster,
restitution: Restitution, restitution: Restitution,
player_look_rotatatable: PlayerLookRotatatable, player_look_rotatable: PlayerLookRotatable,
} }
impl CharacterControllerBundle { impl CharacterControllerBundle {
@ -85,7 +103,7 @@ impl CharacterControllerBundle {
) )
.with_max_time_of_impact(0.2), .with_max_time_of_impact(0.2),
restitution: Restitution::new(0.0).with_combine_rule(CoefficientCombine::Min), restitution: Restitution::new(0.0).with_combine_rule(CoefficientCombine::Min),
player_look_rotatatable: PlayerLookRotatatable, player_look_rotatable: PlayerLookRotatable,
} }
} }
} }
@ -103,13 +121,13 @@ impl Default for CharacterControllerBundle {
fn get_lateral_acceleration( fn get_lateral_acceleration(
controller: &CharacterController, controller: &CharacterController,
delta_time: f32, delta_time: f32,
velocity: LinearVelocity, velocity: Vec3,
) -> Vec3 { ) -> Vec3 {
// No acceleration in Z // No acceleration in Y
let fall_acceleration = Vec3::new( let fall_acceleration = Vec3::new(
controller.gravity.x, controller.gravity.x,
0.0, 0.0,
controller.gravity.y, controller.gravity.z,
); );
// Bound acceleration, falling object has minimal ability to impact acceleration // Bound acceleration, falling object has minimal ability to impact acceleration
@ -171,38 +189,63 @@ fn apply_gravity(
for (character_controller, mut linear_velocity) in &mut controllers { for (character_controller, mut linear_velocity) in &mut controllers {
let fall_acceleration: Vec3 = get_lateral_acceleration(character_controller, delta_time, linear_velocity); let fall_acceleration: Vec3 = get_lateral_acceleration(character_controller, delta_time, linear_velocity.0);
fall_acceleration.y = 0.0; fall_acceleration.y = 0.0;
let has_limited_air_control: bool = false; let has_limited_air_control: bool = false;
// Ugly fix for mismatch types, find a proper solution later // Ugly fix for mismatch types, find a proper solution later
let new_velocity: Vec3 = new_fall_velocity(linear_velocity.clone(), character_controller.gravity, delta_time, character_controller.speed_limit); let new_velocity: Vec3 = new_fall_velocity(linear_velocity.clone(), character_controller.gravity, delta_time, character_controller.axis_speed_limit);
linear_velocity.x = new_velocity.x; linear_velocity.x = new_velocity.x;
linear_velocity.y = new_velocity.y; linear_velocity.y = new_velocity.y;
linear_velocity.z = new_velocity.z; linear_velocity.z = new_velocity.z;
} }
} }
fn move_character(
time: Res<Time>,
mut movement_event_reader: EventReader<MovementAction>,
mut query: Query<(
&mut LinearVelocity,
&CharacterController,
Has<Grounded>,
&Rotation,
&ShapeHits,
&Transform,
)>,
) {
let delta_time = time.delta_seconds_f64().adjust_precision();
fn calculate_velocity(delta_time: f32, friction: f32, fluid: bool , braking_deceleration: f32, acceleration: Vec3, velocity: LinearVelocity) -> Vec3 { for event in movement_event_reader.read() {
let new_friction: f32 = f32::max(0.0, friction); for (
const MAX_ACCELERATION: f32 = 857.25; mut velocity,
let max_speed: f32 = 1714.5; character_controller,
let force_max_acceleration: bool = false; is_grounded,
let analog_input_modifier: f32 = 1.0; rotation,
let analog_speed: f32 = 10.0; shape_hits,
character_transform,
if(force_max_acceleration){ ) in &mut query
{
acceleration = acceleration.normalize() * MAX_ACCELERATION; match event {
MovementAction::Move(direction) => {
if is_grounded {
velocity.x = f32::clamp(velocity.x, -character_controller.axis_speed_limit, character_controller.axis_speed_limit);
velocity.y = f32::clamp(velocity.y, -character_controller.axis_speed_limit, character_controller.axis_speed_limit);
} else {
}
}
MovementAction::Jump => {
if is_grounded {
}
}
}
}
} }
max_speed = f32::max(max_speed * analog_input_modifier, analog_speed)
} }
fn update_grounded( fn update_grounded(
mut commands: Commands, mut commands: Commands,
mut query: Query<(Entity, &CharacterController, &Rotation, &ShapeHits)>, mut query: Query<(Entity, &CharacterController, &Rotation, &ShapeHits)>,
@ -301,77 +344,77 @@ fn kinematic_controller_collisions(
} }
} }
fn move_character( // fn move_character(
time: Res<Time>, // time: Res<Time>,
mut movement_event_reader: EventReader<MovementAction>, // mut movement_event_reader: EventReader<MovementAction>,
mut query: Query<( // mut query: Query<(
&mut LinearVelocity, // &mut LinearVelocity,
&CharacterController, // &CharacterController,
Has<Grounded>, // Has<Grounded>,
&Rotation, // &Rotation,
&ShapeHits, // &ShapeHits,
&Transform, // &Transform,
)>, // )>,
) { // ) {
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 ( // for (
mut velocity, // mut velocity,
character_controller, // character_controller,
is_grounded, // is_grounded,
rotation, // rotation,
shape_hits, // shape_hits,
character_transform, // character_transform,
) in &mut query // ) in &mut query
{ // {
match event { // match event {
MovementAction::Move(direction) => { // MovementAction::Move(direction) => {
// Get the forward direction of the character in the local coordinate system // // Get the forward direction of the character in the local coordinate system
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 // let local_movement = forward
* direction.y // * direction.y
* character_controller.movement_acceleration // * character_controller.movement_acceleration
* delta_time // * delta_time
+ character_transform // + character_transform
.rotation // .rotation
.mul_vec3(Vec3::new(direction.x, 0.0, 0.0)) // .mul_vec3(Vec3::new(direction.x, 0.0, 0.0))
* character_controller.movement_acceleration // * character_controller.movement_acceleration
* delta_time; // * delta_time;
if is_grounded { // if is_grounded {
let mut slope_factor = 1.0; // let mut slope_factor = 1.0;
for hit in shape_hits.iter() { // for hit in shape_hits.iter() {
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 *= // slope_factor *=
(angle / character_controller.max_slope_angle).cos(); // (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;
} // }
} // }
} else { // } else {
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 {
velocity.y = character_controller.jump_impulse; // velocity.y = character_controller.jump_impulse;
} // }
} // }
} // }
} // }
} // }
} // }
fn dampen_movement(mut qeury: Query<(&mut LinearVelocity, &CharacterController)>) { fn dampen_movement(mut qeury: Query<(&mut LinearVelocity, &CharacterController)>) {
for (mut velocity, character_controller) in &mut qeury { for (mut velocity, character_controller) in &mut qeury {
@ -398,8 +441,8 @@ pub fn spawn_player(
..default() ..default()
}, },
)) ))
.with_children(|parrent| { .with_children(|parent| {
parrent.spawn(( parent.spawn((
PlayerCamera::default(), PlayerCamera::default(),
Camera3dBundle { Camera3dBundle {
transform: Transform::from_xyz(0., camera_hight, 0.), transform: Transform::from_xyz(0., camera_hight, 0.),

View file

@ -15,10 +15,13 @@ 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::*; use bevy::math::Quat;
use crate::player_utils::input_processor::*;
use bevy::{ use bevy::{
input::mouse::MouseMotion, input::mouse::MouseMotion,
prelude::*, prelude::*,
math::*,
}; };
pub struct PlayerLookPlugin; pub struct PlayerLookPlugin;
@ -30,9 +33,9 @@ impl Plugin for PlayerLookPlugin {
} }
} }
// marker for rotating playr horizontally // marker for rotating player horizontally
#[derive(Component)] #[derive(Component)]
pub struct PlayerLookRotatatable; pub struct PlayerLookRotatable;
#[derive(Component)] #[derive(Component)]
pub struct PlayerCamera { pub struct PlayerCamera {
@ -63,7 +66,7 @@ fn rotate_camera(
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 rotatable_query: Query<
&mut Transform, &mut Transform,
(With<PlayerLookRotatatable>, Without<PlayerCamera>), (With<PlayerLookRotatable>, Without<PlayerCamera>),
>, >,
mut gizmos: Gizmos, mut gizmos: Gizmos,
) { ) {