diff --git a/src/components/player.rs b/src/components/player.rs index a6091d5..1bd3bbd 100644 --- a/src/components/player.rs +++ b/src/components/player.rs @@ -2,20 +2,48 @@ use bevy::prelude::*; use bevy_rapier3d::prelude::GravityScale; #[derive(Component, Default, Reflect)] -#[require(PlayerVelocity)] +#[require(PlayerVelocity, MoveSpeed, JumpSpeed, PlayerDrag)] pub struct PlayerMotion(pub Vec3); #[derive(Component, Default, Reflect)] pub struct PlayerForce(pub Vec3); -#[derive(Component, Default, Reflect)] +#[derive(Component, Reflect)] #[require(GravityScale)] pub struct GravityDirection(pub Option); +impl Default for GravityDirection { + fn default() -> Self { + Self::DOWN + } +} + impl GravityDirection { pub const DOWN: GravityDirection = GravityDirection(Some(Dir3::NEG_Y)); + #[allow(dead_code)] pub const NONE: GravityDirection = GravityDirection(None); } #[derive(Component, Default, Reflect)] pub struct PlayerVelocity(pub Vec3); + +#[derive(Component, Reflect)] +pub struct MoveSpeed(pub f32); + +impl Default for MoveSpeed { + fn default() -> Self { + Self(10.0) + } +} + +#[derive(Component, Reflect)] +pub struct JumpSpeed(pub f32); + +impl Default for JumpSpeed { + fn default() -> Self { + Self(10.0) + } +} + +#[derive(Component, Default, Reflect)] +pub struct PlayerDrag(pub f32); diff --git a/src/plugins/game.rs b/src/plugins/game.rs index c207159..16d2d40 100644 --- a/src/plugins/game.rs +++ b/src/plugins/game.rs @@ -1,7 +1,7 @@ use crate::{ components::{ camera::{CameraPitch, CameraRoot, FollowCam, MainCamera, Unfocused}, - player::GravityDirection, + player::{GravityDirection, MoveSpeed, PlayerDrag}, tags::{Player, Ship}, }, plugins::*, @@ -48,7 +48,7 @@ fn setup_scene( .spawn(( Name::new("Player"), Player, - GravityDirection::DOWN, + PlayerDrag(0.5), Collider::capsule_y(0.5, 0.5), RigidBody::KinematicPositionBased, KinematicCharacterController::default(), diff --git a/src/plugins/player.rs b/src/plugins/player.rs index 31e51bb..7f17165 100644 --- a/src/plugins/player.rs +++ b/src/plugins/player.rs @@ -1,9 +1,14 @@ +use std::f32::EPSILON; + use bevy::prelude::*; use bevy_rapier3d::prelude::*; -use crate::components::{ - player::{GravityDirection, PlayerForce, PlayerMotion, PlayerVelocity}, - tags::Player, +use crate::{ + components::{ + player::{GravityDirection, JumpSpeed, MoveSpeed, PlayerDrag, PlayerForce, PlayerMotion, PlayerVelocity}, + tags::Player, + }, + utils::rotation::get_alignment_rotation, }; pub struct PlayerPlugin; @@ -11,15 +16,16 @@ pub struct PlayerPlugin; impl Plugin for PlayerPlugin { fn build(&self, app: &mut App) { app.add_systems(PreUpdate, keyboard_input.in_set(PlayerInputSystems)); - app.add_systems(Update, (apply_gravity, apply_forces, apply_motion).chain()); + app.add_systems(Update, (apply_gravity, apply_forces, apply_motion, apply_drag).chain()); + app.add_systems(Update, align_with_gravity); } } #[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)] pub struct PlayerInputSystems; -fn apply_forces(player: Single<(&mut PlayerVelocity, &mut PlayerForce), With>, time: Res