283 lines
10 KiB
Rust
283 lines
10 KiB
Rust
use bevy::prelude::*;
|
|
use bevy::input::mouse::MouseMotion;
|
|
use bevy::window::PrimaryWindow;
|
|
use bevy::core_pipeline::bloom::{BloomCompositeMode, BloomSettings};
|
|
use bevy::core_pipeline::tonemapping::Tonemapping;
|
|
use bevy::transform::TransformSystem;
|
|
use bevy_xpbd_3d::prelude::*;
|
|
use std::f32::consts::*;
|
|
use crate::{settings, audio, actor};
|
|
|
|
pub struct CameraControllerPlugin;
|
|
|
|
impl Plugin for CameraControllerPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.add_systems(Startup, setup_camera);
|
|
app.add_systems(Update, handle_input);
|
|
app.add_systems(Update, manage_player_actor.after(handle_input));
|
|
app.add_systems(PostUpdate, sync_camera_to_player
|
|
.after(PhysicsSet::Sync)
|
|
.before(TransformSystem::TransformPropagate));
|
|
app.add_systems(Update, update_fov);
|
|
app.add_systems(Update, apply_input_to_player);
|
|
}
|
|
}
|
|
|
|
// Based on Valorant's default sensitivity, not entirely sure why it is exactly 1.0 / 180.0,
|
|
// but I'm guessing it is a misunderstanding between degrees/radians and then sticking with
|
|
// it because it felt nice.
|
|
pub const RADIANS_PER_DOT: f32 = 1.0 / 180.0;
|
|
|
|
fn setup_camera(
|
|
mut commands: Commands,
|
|
) {
|
|
// Add player
|
|
commands.spawn((
|
|
Camera3dBundle {
|
|
camera: Camera {
|
|
hdr: true, // HDR is required for bloom
|
|
..default()
|
|
},
|
|
tonemapping: Tonemapping::TonyMcMapface,
|
|
transform: Transform::from_xyz(0.0, 0.0, 8.0).looking_at(Vec3::ZERO, Vec3::Y),
|
|
..default()
|
|
},
|
|
BloomSettings {
|
|
composite_mode: BloomCompositeMode::EnergyConserving,
|
|
..default()
|
|
},
|
|
));
|
|
}
|
|
|
|
pub fn sync_camera_to_player(
|
|
settings: Res<settings::Settings>,
|
|
mut q_camera: Query<&mut Transform, With<Camera>>,
|
|
q_playercam: Query<(&actor::Actor, &Transform), (With<actor::PlayerCamera>, Without<Camera>)>,
|
|
) {
|
|
if q_camera.is_empty() || q_playercam.is_empty() {
|
|
return;
|
|
}
|
|
let mut camera_transform = q_camera.get_single_mut().unwrap();
|
|
let (actor, player_transform) = q_playercam.get_single().unwrap();
|
|
|
|
// Rotation
|
|
camera_transform.rotation = player_transform.rotation * Quat::from_array([0.0, -1.0, 0.0, 0.0]);
|
|
|
|
// Translation
|
|
if settings.third_person {
|
|
camera_transform.translation = player_transform.translation + camera_transform.rotation * (actor.camdistance * Vec3::new(0.0, 0.2, 1.0));
|
|
}
|
|
else {
|
|
camera_transform.translation = player_transform.translation;
|
|
}
|
|
}
|
|
|
|
pub fn update_fov(
|
|
q_player: Query<&actor::LifeForm, With<actor::Player>>,
|
|
mut q_camera: Query<&mut Projection, With<Camera>>,
|
|
) {
|
|
if let (Ok(lifeform), Ok(mut projection)) = (q_player.get_single(), q_camera.get_single_mut())
|
|
{
|
|
let fov = (lifeform.adrenaline.powf(3.0) * 45.0 + 45.0).to_radians();
|
|
*projection = Projection::Perspective(PerspectiveProjection { fov: fov, ..default() });
|
|
}
|
|
}
|
|
|
|
pub fn handle_input(
|
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
|
mut settings: ResMut<settings::Settings>,
|
|
) {
|
|
if keyboard_input.just_pressed(settings.key_camera) {
|
|
settings.third_person ^= true;
|
|
}
|
|
}
|
|
|
|
fn manage_player_actor(
|
|
settings: Res<settings::Settings>,
|
|
mut q_playercam: Query<&mut Visibility, With<actor::PlayerCamera>>,
|
|
mut q_hiddenplayer: Query<&mut Visibility, (With<actor::Player>, Without<actor::PlayerCamera>)>,
|
|
) {
|
|
for mut vis in &mut q_playercam {
|
|
if settings.third_person {
|
|
*vis = Visibility::Inherited;
|
|
}
|
|
else {
|
|
*vis = Visibility::Hidden;
|
|
}
|
|
}
|
|
for mut vis in &mut q_hiddenplayer {
|
|
*vis = Visibility::Hidden;
|
|
}
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn apply_input_to_player(
|
|
time: Res<Time>,
|
|
settings: Res<settings::Settings>,
|
|
mut windows: Query<&mut Window, With<PrimaryWindow>>,
|
|
mut mouse_events: EventReader<MouseMotion>,
|
|
key_input: Res<ButtonInput<KeyCode>>,
|
|
thruster_sound_controller: Query<&AudioSink, With<audio::ComponentThrusterSound>>,
|
|
rocket_sound_controller: Query<&AudioSink, With<audio::ComponentRocketSound>>,
|
|
ion_sound_controller: Query<&AudioSink, With<audio::ComponentIonSound>>,
|
|
mut q_playercam: Query<(
|
|
&mut Transform,
|
|
&mut actor::Engine,
|
|
&mut AngularVelocity,
|
|
&mut LinearVelocity,
|
|
), (With<actor::PlayerCamera>, Without<Camera>)>,
|
|
) {
|
|
let dt = time.delta_seconds();
|
|
let mut play_thruster_sound = false;
|
|
|
|
let window_result = windows.get_single_mut();
|
|
let mut focused = true;
|
|
if window_result.is_ok() {
|
|
focused = window_result.unwrap().focused;
|
|
}
|
|
|
|
if let Ok((mut player_transform, mut engine, mut angularvelocity, mut v)) = q_playercam.get_single_mut() {
|
|
|
|
if angularvelocity.length_squared() > 0.0001 {
|
|
angularvelocity.x *= 0.98;
|
|
angularvelocity.y *= 0.98;
|
|
angularvelocity.z *= 0.98;
|
|
}
|
|
else {
|
|
angularvelocity.0 = Vec3::splat(0.0);
|
|
}
|
|
|
|
// Handle key input
|
|
let mut axis_input = Vec3::ZERO;
|
|
if focused {
|
|
if key_input.pressed(settings.key_forward) {
|
|
axis_input.z += 1.2;
|
|
}
|
|
if key_input.pressed(settings.key_back) {
|
|
axis_input.z -= 1.2;
|
|
}
|
|
if key_input.pressed(settings.key_right) {
|
|
axis_input.x -= 1.2;
|
|
}
|
|
if key_input.pressed(settings.key_left) {
|
|
axis_input.x += 1.2;
|
|
}
|
|
if key_input.pressed(settings.key_up) {
|
|
axis_input.y += 1.2;
|
|
}
|
|
if key_input.pressed(settings.key_down) {
|
|
axis_input.y -= 1.2;
|
|
}
|
|
if key_input.pressed(settings.key_stop) {
|
|
let stop_direction = -v.normalize();
|
|
if stop_direction.length_squared() > 0.3 {
|
|
axis_input += 1.0 * (player_transform.rotation.inverse() * stop_direction);
|
|
}
|
|
}
|
|
}
|
|
// In typical games we would normalize the input vector so that diagonal movement is as
|
|
// fast as forward or sideways movement. But here, we merely clamp each direction to an
|
|
// absolute maximum of 1, since every thruster can be used separately. If the forward
|
|
// thrusters and the leftward thrusters are active at the same time, then of course the
|
|
// total diagonal acceleration is faster than the forward acceleration alone.
|
|
axis_input = axis_input.clamp(Vec3::splat(-1.0), Vec3::splat(1.0));
|
|
|
|
// Apply movement update
|
|
let forward_factor = engine.current_warmup * (if axis_input.z > 0.0 {
|
|
engine.thrust_forward
|
|
} else {
|
|
engine.thrust_back
|
|
});
|
|
let right_factor = engine.thrust_sideways * engine.current_warmup;
|
|
let up_factor = engine.thrust_sideways * engine.current_warmup;
|
|
let factor = Vec3::new(right_factor, up_factor, forward_factor);
|
|
|
|
if axis_input.length_squared() > 0.003 {
|
|
let acceleration_global = player_transform.rotation * (axis_input * factor);
|
|
let mut acceleration_total = actor::ENGINE_SPEED_FACTOR * dt * acceleration_global;
|
|
let threshold = 1e-5;
|
|
if key_input.pressed(settings.key_stop) {
|
|
// Decelerate
|
|
for i in 0..3 {
|
|
if v[i].abs() < threshold {
|
|
v[i] = 0.0;
|
|
}
|
|
else if v[i].signum() != (v[i] + acceleration_total[i]).signum() {
|
|
// Almost stopped, but we overshot v=0
|
|
v[i] = 0.0;
|
|
acceleration_total[i] = 0.0;
|
|
}
|
|
}
|
|
}
|
|
v.0 += acceleration_total;
|
|
engine.current_warmup = (engine.current_warmup + dt / engine.warmup_seconds).clamp(0.0, 1.0);
|
|
play_thruster_sound = !settings.mute_sfx;
|
|
}
|
|
else {
|
|
engine.current_warmup = (engine.current_warmup - dt / engine.warmup_seconds).clamp(0.0, 1.0);
|
|
}
|
|
|
|
// Handle mouse input and mouse-like key bindings
|
|
let mut mouse_delta = Vec2::ZERO;
|
|
let mut pitch_yaw_rot = Vec3::ZERO;
|
|
let mouseless_sensitivity = 8.0;
|
|
if key_input.pressed(settings.key_mouseup) {
|
|
pitch_yaw_rot[0] -= mouseless_sensitivity;
|
|
}
|
|
if key_input.pressed(settings.key_mousedown) {
|
|
pitch_yaw_rot[0] += mouseless_sensitivity;
|
|
}
|
|
if key_input.pressed(settings.key_mouseleft) {
|
|
pitch_yaw_rot[1] += mouseless_sensitivity;
|
|
}
|
|
if key_input.pressed(settings.key_mouseright) {
|
|
pitch_yaw_rot[1] -= mouseless_sensitivity;
|
|
}
|
|
if key_input.pressed(settings.key_rotateleft) {
|
|
pitch_yaw_rot[2] -= mouseless_sensitivity;
|
|
}
|
|
if key_input.pressed(settings.key_rotateright) {
|
|
pitch_yaw_rot[2] += mouseless_sensitivity;
|
|
}
|
|
for mouse_event in mouse_events.read() {
|
|
mouse_delta += mouse_event.delta;
|
|
}
|
|
if mouse_delta != Vec2::ZERO {
|
|
if key_input.pressed(settings.key_rotate) {
|
|
pitch_yaw_rot[2] += mouse_delta.x;
|
|
} else {
|
|
pitch_yaw_rot[0] += mouse_delta.y;
|
|
pitch_yaw_rot[1] -= mouse_delta.x;
|
|
}
|
|
}
|
|
if pitch_yaw_rot.length_squared() > 0.0001 {
|
|
pitch_yaw_rot *= RADIANS_PER_DOT * settings.mouse_sensitivity;
|
|
player_transform.rotation *= Quat::from_euler(EulerRot::ZYX,
|
|
pitch_yaw_rot[2], pitch_yaw_rot[1], pitch_yaw_rot[0]).normalize();
|
|
}
|
|
|
|
// Play sound effects
|
|
if let Ok(sink) = thruster_sound_controller.get_single() {
|
|
if play_thruster_sound && engine.engine_type == actor::EngineType::Monopropellant {
|
|
sink.play()
|
|
} else {
|
|
sink.pause()
|
|
}
|
|
}
|
|
if let Ok(sink) = rocket_sound_controller.get_single() {
|
|
if play_thruster_sound && engine.engine_type == actor::EngineType::Rocket {
|
|
sink.play()
|
|
} else {
|
|
sink.pause()
|
|
}
|
|
}
|
|
if let Ok(sink) = ion_sound_controller.get_single() {
|
|
if play_thruster_sound && engine.engine_type == actor::EngineType::Ion {
|
|
sink.play()
|
|
} else {
|
|
sink.pause()
|
|
}
|
|
}
|
|
}
|
|
}
|