Tweaked values, learned how to add parents and children.

This commit is contained in:
Franklin 2023-09-13 14:31:36 -04:00
parent 69092e471a
commit 4f5b98a35f
6 changed files with 56 additions and 27 deletions

View File

@ -11,7 +11,16 @@
- [ ] Feature: Subtle Headbob, FOV change on movement (Distinguish between sprinting and walking).
- [ ] Feature: Basic ESC UI, FPS counter, stamina bar
- [ ] Discussion: PvP vs PvE
- [ ] Discussion: PvP vs PvE
PvE First, after the initial game loop is done, then PvP.
- [ ] Discussion: Realism vs arcade
Realism First, arcade last.
- [ ] Discussion: Gameplay loop
- [ ] Discussion: Is this a survival game? A Shooter? A Sandbox? All?
End goal is all. But at first this should be a sandbox where you spawn with a gun.
## Focus:
Making a game that you can leverage when you realize what's possible and what's not.
Movement system, gun handling, shooting, modding, inventory, hitreg, etc...

View File

@ -3,7 +3,7 @@ pub const PLAYER_ACCELERATION: f32 = 20.0;
pub const PLAYER_JUMP_FORCE: f32 = 900.0;
/// Time in ms that player must be grounded in order to jump again
pub const PLAYER_JUMP_COOLDOWN_MS: u128 = 75;
pub const PLAYER_SPRINT_SPEED_MULTIPLIER: f32 = 2.0;
pub const PLAYER_SPRINT_SPEED_MULTIPLIER: f32 = 2.5;
pub const PLAYER_CROUCH_SPEED_MULTIPLIER: f32 = 0.25;
pub const PLAYER_INITIAL_WEIGHT: f32 = 75.0;
pub const PLAYER_GRAVITY_SCALE: f32 = 4.0;
@ -20,3 +20,11 @@ pub const PLAYER_LATERAL_ACCELERATION_WHILE_SPRINTING_MULTIPLIER: f32 = 0.2;
pub const PLAYER_LATERAL_ACCELERATION_MULTIPLIER: f32 = 1.0;
pub const PLAYER_LINEAR_DAMPING_TIME_OFFSET_AFTER_JUMP_IN_MS: u128 = 20;
/*
pub const PLAYER_CAMERA_HEADBOB_Y_POS: f32 = 0.2;
pub const PLAYER_CAMERA_HEADBOB_Y_NEG: f32 = -0.2;
pub const PLAYER_CAMERA_HEADBOB_X_POS: f32 = 0.2;
pub const PLAYER_CAMERA_HEADBOB_X_NEG: f32 = -0.2;
*/

View File

@ -24,32 +24,27 @@ impl Default for MouseMovementSettings {
}
}
/// Synchronizes camera's translation & rotation (only 1 axis) to player.
pub fn sync_camera_to_player(
/// Synchronizes camera's translation to player.
pub fn update_camera_vertical_position(
mut player: Query<(&mut Transform, &PlayerLinearXZState), (With<Player>, Without<MainCamera>)>,
mut camera: Query<&mut Transform, (With<MainCamera>, Without<Player>)>,
time: Res<Time>,
) {
let Ok((mut player, player_linear_xz_state)) = player.get_single_mut() else { return };
let Ok((_, player_linear_xz_state)) = player.get_single_mut() else { return };
let Ok(mut camera_transform) = camera.get_single_mut() else { return };
camera_transform.translation = player.translation;
if let PlayerLinearXZState::Crouched(since) = player_linear_xz_state {
// Lerp/Smooth out this movement
let delta = time.elapsed().as_millis() - since;
if delta > PLAYER_CROUCH_TIME_MS {
camera_transform.translation.y += PLAYER_CROUCH_HEIGHT;
camera_transform.translation.y = PLAYER_CROUCH_HEIGHT;
} else {
// Starts at player_camera_height -> ends at player_crouch_height (Only works if player_crouch_height is 0)
camera_transform.translation.y += PLAYER_CAMERA_HEIGHT * (1.0 - (delta as f32 / PLAYER_CROUCH_TIME_MS as f32))
camera_transform.translation.y = PLAYER_CAMERA_HEIGHT * (1.0 - (delta as f32 / PLAYER_CROUCH_TIME_MS as f32))
}
} else {
camera_transform.translation.y += PLAYER_CAMERA_HEIGHT;
camera_transform.translation.y = PLAYER_CAMERA_HEIGHT;
}
player.rotation = camera_transform.rotation;
//camera_transform.rotate_x(radians_from_degrees(-15.0)); // Make camera slightly point downward.
}
/// Handles looking around if cursor is locked
@ -59,9 +54,10 @@ pub fn follow_cursor_with_camera(
mut motions: EventReader<MouseMotion>,
mut player_query: Query<&mut Transform, (With<Player>, Without<MainCamera>)>,
mut camera_query: Query<&mut Transform, (With<MainCamera>, Without<Player>)>,
//time: Res<Time>,
) {
if let Ok(window) = primary_window.get_single() {
for player_transform in player_query.iter_mut() {
for mut player_transform in player_query.iter_mut() {
let (mut yaw, mut pitch, _) = player_transform.rotation.to_euler(EulerRot::YXZ);
for motion in motions.iter() {
let window_scale = window.height().min(window.width());
@ -73,11 +69,12 @@ pub fn follow_cursor_with_camera(
let desired_rotation_quat =
Quat::from_axis_angle(Vec3::Y, yaw) * Quat::from_axis_angle(Vec3::X, pitch);
for mut camera_transform in camera_query.iter_mut() {
camera_transform.rotation = desired_rotation_quat;
for _camera_transform in camera_query.iter_mut() {
player_transform.rotation = desired_rotation_quat;
// headbob_camera(&mut camera_transform, time.delta_seconds_f64());
}
}
} else {
warn!("Primary window not found for `player_look`!");
}
}
}

View File

@ -69,6 +69,14 @@ impl PlayerLinearXZState {
_ => false,
}
}
/*/// If already sprinting, adds time to sprint
/// If not sprinting sets sprint.
pub fn sprint(&mut self, time: u128) {
match self {
PlayerLinearXZState::Sprinting(since) => {},
_ => *self = PlayerLinearXZState::Sprinting(time)
};
}*/
}
/// Holds all the possible ways a player can be attempting to move at any time.

View File

@ -14,6 +14,14 @@ use super::player_movement::{PlayerLinearXZState, PlayerLinearYState};
pub fn spawn_player(mut commands: Commands, asset_server: Res<AssetServer>) {
let skybox_handle = asset_server.load(CUBEMAPS[0].0);
let camera = commands
.spawn(MainCamera)
.insert(Camera3dBundle {
transform: Transform::from_xyz(0.0, 0.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
})
.insert(Skybox(skybox_handle.clone())).id();
commands
.spawn(Player)
.insert(RigidBody::Dynamic)
@ -44,15 +52,12 @@ pub fn spawn_player(mut commands: Commands, asset_server: Res<AssetServer>) {
torque_impulse: Vec3::ZERO,
})
.insert(PlayerLinearYState::Falling)
.insert(PlayerLinearXZState::Stopped);
.insert(PlayerLinearXZState::Stopped)
.push_children(&[camera]);
commands
.spawn(MainCamera)
.insert(Camera3dBundle {
transform: Transform::from_xyz(0.0, 0.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
})
.insert(Skybox(skybox_handle.clone()));
commands.insert_resource(Cubemap {
is_loaded: false,

View File

@ -4,7 +4,7 @@ use crate::{
comps::core::controller::{capture_cursor, capture_input},
logic::core::player::{
camera_player_sync::{
follow_cursor_with_camera, sync_camera_to_player, MouseMovementSettings,
follow_cursor_with_camera, MouseMovementSettings, update_camera_vertical_position,
},
player_vertical_sync::sync_player_y_state,
spawn_player::spawn_player,
@ -22,14 +22,16 @@ pub fn load_scene(application: &mut App) {
application.add_systems(Startup, spawn_ground);
application.add_systems(Startup, spawn_obstacles);
application.add_systems(Startup, spawn_player);
// Update
application.add_systems(Update, capture_input);
application.add_systems(Update, sync_camera_to_player);
//application.add_systems(Update, sync_camera_to_player);
application.add_systems(Update, capture_cursor);
application.add_systems(Update, sync_player_y_state);
application.add_systems(Update, follow_cursor_with_camera);
application.add_systems(Update, asset_loaded);
application.add_systems(Update, update_camera_vertical_position);
application.add_systems(Startup, setup_lighting);
}