Sway and Bob works pretty well. Values are to be tweaked

This commit is contained in:
Franklin 2023-12-03 18:18:03 -04:00
parent 9f5491948e
commit e99708cef9
7 changed files with 113 additions and 25 deletions

View File

@ -15,9 +15,11 @@ use super::markers::player::Player;
/// System that captures input and fires events
pub fn capture_input(
commands: Commands,
keyboard_input: Res<Input<KeyCode>>,
player_query: Query<
(
Entity,
&mut Velocity,
&mut ExternalImpulse,
&mut PlayerLinearYState,
@ -60,6 +62,7 @@ pub fn capture_input(
};
if game_ui_state.any_window() {
move_player(
commands,
PlayerMovementInput::default(),
player_query,
time,
@ -67,6 +70,7 @@ pub fn capture_input(
);
} else {
move_player(
commands,
player_movement_input,
player_query,
time,

View File

@ -27,6 +27,13 @@ impl Default for MouseMovementSettings {
}
}
#[derive(Component, Clone, Debug, Reflect, Default)]
#[reflect(Component)]
pub struct HandSwaying {
pub sway_pos: Vec3,
pub sway_rot: Quat,
}
/// Synchronizes camera's translation to player.
pub fn update_camera_vertical_position(
mut player: Query<(&mut Transform, &PlayerLinearXZState), (With<Player>, Without<PlayerEye>)>,
@ -71,12 +78,13 @@ pub struct FollowCursorWithCameraQueryParams<'w, 's> {
player_query: Query<'w, 's, &'static mut Transform, (With<Player>, Without<MainCamera>)>,
camera_query: Query<'w, 's, &'static mut Transform, (With<MainCamera>, Without<Player>)>,
player_model_query: Query<'w, 's, &'static mut Transform, (With<PlayerCharacter>, Without<Player>, Without<MainCamera>)>,
in_player_hands_query: Query<'w, 's, &'static mut Transform, (With<InPlayerHands>, Without<Player>, Without<MainCamera>, Without<PlayerCharacter>)>,
in_player_hands_query: Query<'w, 's, Entity, (With<InPlayerHands>, Without<Player>, Without<MainCamera>, Without<PlayerCharacter>)>,
//in_player_hands_parent_query: Query<'w, 's, &'static mut Transform, (With<InPlayerHandsParent>, Without<Player>, Without<MainCamera>, Without<PlayerCharacter>, Without<InPlayerHands>)>
}
/// Handles looking around if cursor is locked
pub fn follow_cursor_with_camera(
mut commands: Commands,
settings: Res<MouseMovementSettings>,
mut motions: EventReader<MouseMotion>,
keyboard_input: Res<Input<KeyCode>>,
@ -134,6 +142,11 @@ pub fn follow_cursor_with_camera(
for mut camera_transform in query.camera_query.iter_mut() {
for mut player_model_transform in query.player_model_query.iter_mut() {
let (mut yaw, mut pitch) = (player_transform.rotation.to_euler(EulerRot::YXZ).0, camera_transform.rotation.to_euler(EulerRot::YXZ).1) ;
if motions.len() == 0 {
for in_player_hands_entity in query.in_player_hands_query.iter() {
commands.entity(in_player_hands_entity).insert(HandSwaying { sway_pos: Vec3::ZERO, sway_rot: Quat::default() });
}
}
for motion in motions.read() {
let window_scale = window.height().min(window.width());
let mut weapon_sway_vec = Vec3::ZERO;
@ -175,9 +188,10 @@ pub fn follow_cursor_with_camera(
weapon_sway_vec_rot.y = weapon_sway_vec_rot.y.clamp(-0.5, 0.5);
}
// Weapon Sway
for mut in_player_hands_transform in query.in_player_hands_query.iter_mut() {
in_player_hands_transform.translation = in_player_hands_transform.translation.lerp(weapon_sway_vec, time.delta_seconds() * 1.0);
in_player_hands_transform.rotation = in_player_hands_transform.rotation.lerp(Quat::from_euler(EulerRot::XYZ, weapon_sway_vec_rot.x, weapon_sway_vec_rot.y, weapon_sway_vec_rot.y), time.delta_seconds() * 3.0);
for in_player_hands_entity in query.in_player_hands_query.iter() {
commands.entity(in_player_hands_entity).insert(HandSwaying { sway_pos: weapon_sway_vec, sway_rot: Quat::from_euler(EulerRot::XYZ, weapon_sway_vec_rot.x, weapon_sway_vec_rot.y, weapon_sway_vec_rot.y) });
//in_player_hands_transform.translation = in_player_hands_transform.translation.lerp(weapon_sway_vec, time.delta_seconds() * 1.0);
//in_player_hands_transform.rotation = in_player_hands_transform.rotation.lerp(Quat::from_euler(EulerRot::XYZ, weapon_sway_vec_rot.x, weapon_sway_vec_rot.y, weapon_sway_vec_rot.y), time.delta_seconds() * 3.0);
}
}

View File

@ -3,42 +3,75 @@ use bevy_rapier3d::dynamics::Velocity;
use crate::comps::core::markers::{player::Player, holdable::InPlayerHands, firearm_scene::FirearmScene};
use super::player_movement::PlayerLinearYState;
use super::{player_movement::{PlayerLinearYState, PlayerMovementInput, PlayerLinearXZState}, camera_player_sync::HandSwaying};
#[derive(Component, Clone, Debug, Reflect, Default)]
#[reflect(Component)]
pub struct HandBobbing {
pub speed_curve: f32,
pub bob_position: Vec3,
pub bob_euler_rotation: Vec3,
}
/// The maximum limits of travel from move input
const TRAVEL_LIMIT: Vec3 = Vec3 { x: 0.025, y: 0.025, z: 0.025 };
const TRAVEL_LIMIT: Vec3 = Vec3 { x: 0.01, y: 0.01, z: 0.01 };
/// The maximum limits of travel from bobbing over time
const BOBBING_LIMIT: Vec3 = Vec3 { x: 0.01, y: 0.01, z: 0.01 };
const BOBBING_ROT_MULTIPLIER: Vec3 = Vec3 { x: 2.0, y: 2.0, z: 2.0};
pub fn hand_bobbing_on_player_movement(
player_query: Query<(&Velocity, &PlayerLinearYState), With<Player>>,
player_query: Query<(&Velocity, &PlayerLinearYState, &PlayerLinearXZState, &PlayerMovementInput), With<Player>>,
in_player_hands_query: Query<Entity, With<InPlayerHands>>,
in_player_hands_query: Query<(Entity, &HandSwaying), With<InPlayerHands>>,
mut firearm_scene_query: Query<(&Parent, &mut Transform, &mut HandBobbing), (With<Handle<Scene>>, With<FirearmScene>)>,
time: Res<Time>,
) {
for in_player_hands in in_player_hands_query.iter() {
for (in_player_hands, hand_swaying) in in_player_hands_query.iter() {
for (firearm_scene_parent, mut firearm_scene_transform, mut hand_bobbing) in firearm_scene_query.iter_mut() {
if in_player_hands == firearm_scene_parent.get() {
/*for (player_velocity, player_linear_y_state) in player_query.iter() {
hand_bobbing.speed_curve += time.delta_seconds() *
if player_linear_y_state.is_grounded(&0.1) { player_velocity.linvel.length() } else { 1.0 } + 0.01;
hand_bobbing.bob_position.x = (hand_bobbing.speed_curve.cos() * BOBBING_LIMIT.x * (
if player_linear_y_state.is_grounded(&0.1) { 1.0 } else { 0.0 }
)) - (player_velocity.linvel.normalize().x * TRAVEL_LIMIT.x);
hand_bobbing.bob_position.y = (hand_bobbing.speed_curve.sin() * BOBBING_LIMIT.y) - (player_velocity.linvel.y * TRAVEL_LIMIT.y);
hand_bobbing.bob_position.z = player_velocity.linvel.normalize().z * TRAVEL_LIMIT.z * -1.0;
for (player_velocity, player_linear_y_state, player_linear_xz_state, player_movement_input) in player_query.iter() {
firearm_scene_transform.translation = hand_bobbing.bob_position;
}*/
}
// Speed curve
hand_bobbing.speed_curve += time.delta_seconds() *
if player_linear_y_state.is_grounded(&0.25) {
if player_linear_xz_state.is_sprinting() { player_velocity.linvel.length() / 2.0 } else { player_velocity.linvel.length() }
} else { 1.0 };
// Bobbing translation
hand_bobbing.bob_position.x = (hand_bobbing.speed_curve.cos() * BOBBING_LIMIT.x * (
if player_linear_y_state.is_grounded(&0.25) { 1.0 } else { 0.0 }
)) - (player_movement_input.get_horizontal_value_normalized() * TRAVEL_LIMIT.x);
hand_bobbing.bob_position.y = (hand_bobbing.speed_curve.sin() * BOBBING_LIMIT.y) - (player_velocity.linvel.y * TRAVEL_LIMIT.y);
hand_bobbing.bob_position.z = player_movement_input.get_vertical_value_normalized() * TRAVEL_LIMIT.z * -1.0;
// Bobbing rotation
let input = player_movement_input.get_input();
hand_bobbing.bob_euler_rotation.x = if input == Vec2::ZERO {
BOBBING_ROT_MULTIPLIER.x * ((hand_bobbing.speed_curve * 2.0).sin() / 2.0)
} else {
BOBBING_ROT_MULTIPLIER.x * (hand_bobbing.speed_curve * 2.0).sin()
};
hand_bobbing.bob_euler_rotation.y = if input == Vec2::ZERO { 0.0 } else {
BOBBING_ROT_MULTIPLIER.y * hand_bobbing.speed_curve.cos()
};
hand_bobbing.bob_euler_rotation.z = if input == Vec2::ZERO { 0.0 } else {
BOBBING_ROT_MULTIPLIER.z * hand_bobbing.speed_curve.cos() * input.x
};
// Applying bobbing + sway to actual transform
firearm_scene_transform.translation = firearm_scene_transform.translation.lerp(
hand_bobbing.bob_position + hand_swaying.sway_pos, time.delta_seconds() * 5.0);
firearm_scene_transform.rotation = firearm_scene_transform.rotation.slerp(
hand_swaying.sway_rot * Quat::from_euler(EulerRot::XYZ,
hand_bobbing.bob_euler_rotation.x.to_radians(), hand_bobbing.bob_euler_rotation.y.to_radians(), hand_bobbing.bob_euler_rotation.z.to_radians()
), time.delta_seconds() * 12.0);
}
}
}
}
}

View File

@ -74,7 +74,8 @@ impl PlayerLinearXZState {
}
/// Holds all the possible ways a player can be attempting to move at any time.
#[derive(Default, Reflect)]
#[derive(Default, Reflect, Component, Clone)]
#[reflect(Component)]
pub struct PlayerMovementInput {
/// Means the player is pressing the space bar key. (JUMP)
/// ## DOES NOT MEAN the player should gain upwards velocity
@ -102,9 +103,11 @@ pub struct PlayerMovementInput {
*/
/// Applies game logic to determine how player should move.
pub fn move_player(
mut commands: Commands,
player_movement_input: PlayerMovementInput,
mut query: Query<
(
Entity,
&mut Velocity,
&mut ExternalImpulse,
&mut PlayerLinearYState,
@ -119,6 +122,7 @@ pub fn move_player(
player_values_state: Res<PlayerValuesState>,
) {
for (
player_entity,
mut player_velocity,
mut player_external_force,
mut player_linear_y_state,
@ -128,6 +132,7 @@ pub fn move_player(
mut player_firing_info,
) in &mut query
{
commands.entity(player_entity).insert(player_movement_input.clone());
if player_linear_xz_state.is_sprinting() {
player_firing_info.gun_ready_pose = GunReadyPose::LowReady;
}
@ -283,3 +288,34 @@ fn apply_movement_acceleration_to_vec(
) -> Vec3 {
current_linvel + (direction * player_acceleration * delta_time_secs * multiplier)
}
impl PlayerMovementInput {
pub fn get_horizontal_value_normalized(&self) -> f32 {
let mut val = 0.0;
if self.left {
val -= 1.0;
}
if self.right {
val += 1.0;
}
val
}
/// Vertical == forward and backward
pub fn get_vertical_value_normalized(&self) -> f32 {
let mut val = 0.0;
if self.back {
val -= 1.0;
}
if self.front {
val += 1.0;
}
val
}
pub fn get_input(&self) -> Vec2 {
Vec2 {
x: self.get_horizontal_value_normalized(),
y: self.get_vertical_value_normalized(),
}
}
}

View File

@ -4,7 +4,7 @@ use crate::{
comps::core::{
markers::player::{Player, PlayerData},
spawners::{
guns::{glock17_spawner::Glock17SpawnPoint, ak105_spawner::Ak105SpawnPoint},
guns::ak105_spawner::Ak105SpawnPoint,
player::PlayerSpawnPoint,
},
},
@ -31,7 +31,7 @@ pub fn set_spawn_points(mut commands: Commands) {
},
});*/
commands.spawn(Ak105SpawnPoint ::new_fully_kitted_default({
commands.spawn(Ak105SpawnPoint::new_fully_kitted_default({
let mut transform = Transform::from_xyz(18.0, 10.0, 18.0);
transform.rotate_z(utils::rad_deg::radians_from_degrees(-90.0));
transform

View File

@ -108,7 +108,7 @@ fn spawn_firearm_on_player_hands(
ItemState::Weapon(firearm_state),
Name::new("Firearm Gltf Asset"),
FirearmScene,
HandBobbing { speed_curve: 0.0, bob_position: Vec3::ZERO },
HandBobbing { speed_curve: 0.0, bob_position: Vec3::ZERO, bob_euler_rotation: Vec3::ZERO },
))
.id();
let firearm_entity = commands

View File

@ -12,7 +12,7 @@ use crate::{
player::{
camera_player_sync::MouseMovementSettings,
player_movement::{PlayerLinearXZState, PlayerLinearYState, PlayerMovementInput},
player_values_state::PlayerValuesState, player_settings::PlayerSettings,
player_values_state::PlayerValuesState, player_settings::PlayerSettings, hand_bobbing::HandBobbing,
},
},
scenes::scene1::skybox::Cubemap,
@ -58,6 +58,7 @@ impl Plugin for MainEditorUiPlugin {
.register_type::<InspectScreenSlotUiMarker>()
.register_type::<PlayerControls>()
.register_type::<SightReticle>()
.register_type::<HandBobbing>()
//.register_type::<AllAnimations>()
//.register_type::<FirearmAnimations>()
.register_type::<GltfAssetType>()