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",
"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]
#### 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:

View file

@ -23,9 +23,12 @@ use bevy::prelude::*;
use bevy_editor_pls::prelude::*;
use bevy_xpbd_3d::prelude::*;
use character_controller::*;
use input_processor::*;
use player_look::*;
mod weapon_framework;
mod player_utils;
use player_utils::character_controller::*;
use player_utils::input_processor::*;
use player_utils::player_look::*;
#[derive(Resource, Default, Debug)]
struct IsMapSpawned(bool);

View file

@ -1,3 +1,3 @@
mod character_controller;
mod input_processor;
mod player_look;
pub mod character_controller;
pub mod input_processor;
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>.
*/
use crate::{input_processor::*, player_look::*};
use bevy::prelude::*;
use crate::player_utils::{input_processor::*, player_look::*};
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};
pub struct CharacterControllerPlugin;
@ -41,7 +54,9 @@ pub struct CharacterController {
pub jump_impulse: Scalar,
pub gravity: Vector,
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 {
@ -53,7 +68,10 @@ impl Default for CharacterController {
jump_impulse: 4.0,
gravity: Vector::NEG_Y * 9.81 * 2.0,
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,
ground_caster: ShapeCaster,
restitution: Restitution,
player_look_rotatatable: PlayerLookRotatatable,
player_look_rotatable: PlayerLookRotatable,
}
impl CharacterControllerBundle {
@ -85,7 +103,7 @@ impl CharacterControllerBundle {
)
.with_max_time_of_impact(0.2),
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(
controller: &CharacterController,
delta_time: f32,
velocity: LinearVelocity,
velocity: Vec3,
) -> Vec3 {
// No acceleration in Z
// No acceleration in Y
let fall_acceleration = Vec3::new(
controller.gravity.x,
0.0,
controller.gravity.y,
controller.gravity.z,
);
// 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 {
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;
let has_limited_air_control: bool = false;
// 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.y = new_velocity.y;
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 {
let new_friction: f32 = f32::max(0.0, friction);
const MAX_ACCELERATION: f32 = 857.25;
let max_speed: f32 = 1714.5;
let force_max_acceleration: bool = false;
let analog_input_modifier: f32 = 1.0;
let analog_speed: f32 = 10.0;
if(force_max_acceleration){
acceleration = acceleration.normalize() * MAX_ACCELERATION;
for event in movement_event_reader.read() {
for (
mut velocity,
character_controller,
is_grounded,
rotation,
shape_hits,
character_transform,
) in &mut query
{
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(
mut commands: Commands,
mut query: Query<(Entity, &CharacterController, &Rotation, &ShapeHits)>,
@ -301,77 +344,77 @@ fn kinematic_controller_collisions(
}
}
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 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();
for event in movement_event_reader.read() {
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();
// for event in movement_event_reader.read() {
// 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;
// // 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;
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();
// 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();
// 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;
}
}
} else {
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 {
velocity.y = character_controller.jump_impulse;
}
}
}
}
}
}
// // 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;
// }
// }
// } else {
// 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 {
// velocity.y = character_controller.jump_impulse;
// }
// }
// }
// }
// }
// }
fn dampen_movement(mut qeury: Query<(&mut LinearVelocity, &CharacterController)>) {
for (mut velocity, character_controller) in &mut qeury {
@ -398,8 +441,8 @@ pub fn spawn_player(
..default()
},
))
.with_children(|parrent| {
parrent.spawn((
.with_children(|parent| {
parent.spawn((
PlayerCamera::default(),
Camera3dBundle {
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>.
*/
use crate::input_processor::*;
use bevy::math::Quat;
use crate::player_utils::input_processor::*;
use bevy::{
input::mouse::MouseMotion,
prelude::*,
math::*,
};
pub struct PlayerLookPlugin;
@ -30,9 +33,9 @@ impl Plugin for PlayerLookPlugin {
}
}
// marker for rotating playr horizontally
// marker for rotating player horizontally
#[derive(Component)]
pub struct PlayerLookRotatatable;
pub struct PlayerLookRotatable;
#[derive(Component)]
pub struct PlayerCamera {
@ -63,7 +66,7 @@ fn rotate_camera(
mut camera_query: Query<(&mut Transform, &mut PlayerCamera), With<Camera3d>>,
mut rotatable_query: Query<
&mut Transform,
(With<PlayerLookRotatatable>, Without<PlayerCamera>),
(With<PlayerLookRotatable>, Without<PlayerCamera>),
>,
mut gizmos: Gizmos,
) {