2024-04-21 16:23:40 +00:00
|
|
|
// ▄████████▄ + ███ + ▄█████████ ███ +
|
|
|
|
// ███▀ ▀███ + + ███ ███▀ + ███ + +
|
|
|
|
// ███ + ███ ███ ███ █████████ ███ ███ ███ ███
|
2024-04-21 17:34:00 +00:00
|
|
|
// ███ +███ ███ ███ ███ ███▐██████ ███ ███ ███
|
2024-04-21 16:23:40 +00:00
|
|
|
// ███ + ███ ███+ ███ +███ ███ + ███ ███ + ███
|
|
|
|
// ███▄ ▄███ ███▄ ███ ███ + ███ + ███ ███▄ ███
|
|
|
|
// ▀████████▀ + ▀███████ ███▄ ███▄ ▀████ ▀███████
|
|
|
|
// + + + ███
|
|
|
|
// + ▀████████████████████████████████████████████████████▀
|
2024-04-23 15:33:36 +00:00
|
|
|
//
|
2024-04-23 15:39:07 +00:00
|
|
|
// This module manages the game's viewport, handles camera- and
|
|
|
|
// movement-related keyboard input, and provides some camera-
|
|
|
|
// related computation functions.
|
2024-04-21 16:23:40 +00:00
|
|
|
|
2024-05-22 02:57:20 +00:00
|
|
|
use crate::prelude::*;
|
2024-03-29 18:41:46 +00:00
|
|
|
use bevy::core_pipeline::bloom::{BloomCompositeMode, BloomSettings};
|
2024-03-30 14:50:36 +00:00
|
|
|
use bevy::core_pipeline::tonemapping::Tonemapping;
|
2024-05-22 02:57:20 +00:00
|
|
|
use bevy::input::mouse::{MouseMotion, MouseWheel};
|
2024-04-24 17:54:37 +00:00
|
|
|
use bevy::pbr::{CascadeShadowConfigBuilder, DirectionalLightShadowMap};
|
2024-05-22 02:57:20 +00:00
|
|
|
use bevy::prelude::*;
|
2024-03-30 17:50:53 +00:00
|
|
|
use bevy::transform::TransformSystem;
|
2024-05-22 02:57:20 +00:00
|
|
|
use bevy::window::PrimaryWindow;
|
2024-05-01 19:50:59 +00:00
|
|
|
use bevy_xpbd_3d::plugins::sync;
|
2024-05-22 02:57:20 +00:00
|
|
|
use bevy_xpbd_3d::prelude::*;
|
2024-05-13 02:20:18 +00:00
|
|
|
use std::collections::HashMap;
|
2024-03-16 20:00:40 +00:00
|
|
|
|
2024-04-25 02:15:57 +00:00
|
|
|
pub const INITIAL_ZOOM_LEVEL: f64 = 10.0;
|
2024-07-13 12:30:02 +00:00
|
|
|
pub const MAX_DIST_FOR_MATCH_VELOCITY: f64 = 10000.0;
|
2024-04-25 02:15:57 +00:00
|
|
|
|
2024-03-31 20:08:26 +00:00
|
|
|
pub struct CameraPlugin;
|
2024-03-16 20:00:40 +00:00
|
|
|
|
2024-03-31 20:08:26 +00:00
|
|
|
impl Plugin for CameraPlugin {
|
2024-03-16 20:00:40 +00:00
|
|
|
fn build(&self, app: &mut App) {
|
2024-03-29 18:41:46 +00:00
|
|
|
app.add_systems(Startup, setup_camera);
|
2024-05-13 18:21:56 +00:00
|
|
|
app.add_systems(Update, handle_input.run_if(in_control));
|
2024-05-13 02:47:47 +00:00
|
|
|
app.add_systems(Update, update_map_only_object_visibility.run_if(alive));
|
2024-07-14 19:07:00 +00:00
|
|
|
app.add_systems(
|
|
|
|
PostUpdate,
|
2024-07-14 19:16:19 +00:00
|
|
|
manage_player_actor.in_set(sync::SyncSet::PositionToTransform),
|
2024-07-14 19:07:00 +00:00
|
|
|
);
|
2024-05-22 02:57:20 +00:00
|
|
|
app.add_systems(
|
|
|
|
PostUpdate,
|
|
|
|
sync_camera_to_player
|
|
|
|
.after(PhysicsSet::Sync)
|
|
|
|
.after(apply_input_to_player)
|
|
|
|
.before(TransformSystem::TransformPropagate),
|
|
|
|
);
|
|
|
|
app.add_systems(
|
|
|
|
PostUpdate,
|
|
|
|
update_mapcam_center
|
|
|
|
.before(sync::position_to_transform)
|
|
|
|
.in_set(sync::SyncSet::PositionToTransform),
|
|
|
|
);
|
2024-05-13 19:43:02 +00:00
|
|
|
app.add_systems(Update, update_map_camera.run_if(in_control));
|
2024-05-13 02:47:47 +00:00
|
|
|
app.add_systems(Update, update_fov.run_if(alive));
|
2024-05-07 19:44:23 +00:00
|
|
|
app.add_systems(PreUpdate, apply_input_to_player);
|
2024-04-18 19:01:03 +00:00
|
|
|
app.insert_resource(MapCam::default());
|
2024-05-01 19:50:59 +00:00
|
|
|
|
2024-05-07 19:32:34 +00:00
|
|
|
// To center the renderer origin on the player camera,
|
|
|
|
// 1. Disable bevy_xpbd's position->transform sync function
|
|
|
|
app.insert_resource(sync::SyncConfig {
|
|
|
|
position_to_transform: true,
|
|
|
|
transform_to_position: false,
|
|
|
|
});
|
|
|
|
// 2. Add own position->transform sync function
|
2024-05-22 02:57:20 +00:00
|
|
|
app.add_systems(
|
|
|
|
PostUpdate,
|
|
|
|
position_to_transform
|
|
|
|
.after(sync::position_to_transform)
|
|
|
|
.in_set(sync::SyncSet::PositionToTransform),
|
|
|
|
);
|
2024-04-18 19:01:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-20 00:09:04 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct ShowOnlyInMap {
|
|
|
|
pub min_distance: f64,
|
|
|
|
pub distance_to_id: String,
|
|
|
|
}
|
|
|
|
|
2024-04-18 19:01:03 +00:00
|
|
|
#[derive(Resource)]
|
|
|
|
pub struct MapCam {
|
2024-04-19 02:41:07 +00:00
|
|
|
pub initialized: bool,
|
2024-04-20 02:39:48 +00:00
|
|
|
pub zoom_level: f64,
|
|
|
|
pub target_zoom_level: f64,
|
|
|
|
pub pitch: f64,
|
|
|
|
pub yaw: f64,
|
2024-04-20 02:19:30 +00:00
|
|
|
pub offset_x: f64,
|
|
|
|
pub offset_z: f64,
|
2024-04-20 02:39:48 +00:00
|
|
|
pub center: DVec3,
|
2024-05-07 20:04:47 +00:00
|
|
|
pub center_on_entity: Option<Entity>,
|
2024-04-18 19:01:03 +00:00
|
|
|
}
|
|
|
|
impl Default for MapCam {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2024-04-19 02:41:07 +00:00
|
|
|
initialized: false,
|
|
|
|
zoom_level: 2.0,
|
2024-04-25 02:15:57 +00:00
|
|
|
target_zoom_level: INITIAL_ZOOM_LEVEL,
|
2024-05-12 22:52:34 +00:00
|
|
|
pitch: PI * 0.3,
|
2024-04-18 19:01:03 +00:00
|
|
|
yaw: 0.0,
|
2024-04-20 02:19:30 +00:00
|
|
|
offset_x: 0.0,
|
|
|
|
offset_z: 0.0,
|
2024-04-20 02:39:48 +00:00
|
|
|
center: DVec3::new(0.0, 0.0, 0.0),
|
2024-05-07 20:04:47 +00:00
|
|
|
center_on_entity: None,
|
2024-04-18 19:01:03 +00:00
|
|
|
}
|
2024-03-16 20:00:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-22 02:57:20 +00:00
|
|
|
pub fn setup_camera(mut commands: Commands, settings: Res<var::Settings>) {
|
2024-03-29 18:41:46 +00:00
|
|
|
// Add player
|
|
|
|
commands.spawn((
|
|
|
|
Camera3dBundle {
|
|
|
|
camera: Camera {
|
|
|
|
hdr: true, // HDR is required for bloom
|
2024-03-31 20:08:26 +00:00
|
|
|
clear_color: ClearColorConfig::Custom(Color::BLACK),
|
2024-03-29 18:41:46 +00:00
|
|
|
..default()
|
|
|
|
},
|
2024-03-30 14:50:36 +00:00
|
|
|
tonemapping: Tonemapping::TonyMcMapface,
|
2024-03-29 18:41:46 +00:00
|
|
|
transform: Transform::from_xyz(0.0, 0.0, 8.0).looking_at(Vec3::ZERO, Vec3::Y),
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
BloomSettings {
|
|
|
|
composite_mode: BloomCompositeMode::EnergyConserving,
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
));
|
2024-04-01 16:06:41 +00:00
|
|
|
|
|
|
|
// Add Light from the Sun
|
|
|
|
commands.spawn(DirectionalLightBundle {
|
|
|
|
directional_light: DirectionalLight {
|
2024-04-30 22:44:47 +00:00
|
|
|
illuminance: 2000.0,
|
2024-04-24 17:54:37 +00:00
|
|
|
shadows_enabled: settings.shadows_sun,
|
2024-04-01 16:06:41 +00:00
|
|
|
..default()
|
|
|
|
},
|
2024-05-22 02:57:20 +00:00
|
|
|
transform: Transform::from_rotation(Quat::from_rotation_y(PI32 / 2.0)),
|
2024-04-01 16:06:41 +00:00
|
|
|
cascade_shadow_config: CascadeShadowConfigBuilder {
|
2024-04-24 17:54:37 +00:00
|
|
|
num_cascades: 4,
|
2024-04-24 18:17:37 +00:00
|
|
|
minimum_distance: 0.1,
|
|
|
|
maximum_distance: 5000.0,
|
2024-04-01 16:06:41 +00:00
|
|
|
..default()
|
2024-05-22 02:57:20 +00:00
|
|
|
}
|
|
|
|
.into(),
|
2024-04-01 16:06:41 +00:00
|
|
|
..default()
|
|
|
|
});
|
2024-04-24 17:59:14 +00:00
|
|
|
|
|
|
|
commands.insert_resource(DirectionalLightShadowMap {
|
|
|
|
size: settings.shadowmap_resolution,
|
|
|
|
});
|
2024-03-29 18:41:46 +00:00
|
|
|
}
|
|
|
|
|
2024-03-30 15:18:49 +00:00
|
|
|
pub fn sync_camera_to_player(
|
2024-04-14 12:55:00 +00:00
|
|
|
settings: Res<var::Settings>,
|
2024-04-05 00:58:02 +00:00
|
|
|
mut q_camera: Query<&mut Transform, (With<Camera>, Without<actor::PlayerCamera>)>,
|
2024-03-30 16:05:32 +00:00
|
|
|
q_playercam: Query<(&actor::Actor, &Transform), (With<actor::PlayerCamera>, Without<Camera>)>,
|
2024-03-30 15:18:49 +00:00
|
|
|
) {
|
2024-04-18 19:01:03 +00:00
|
|
|
if settings.map_active || q_camera.is_empty() || q_playercam.is_empty() {
|
2024-03-30 15:18:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let mut camera_transform = q_camera.get_single_mut().unwrap();
|
|
|
|
let (actor, player_transform) = q_playercam.get_single().unwrap();
|
|
|
|
|
|
|
|
// Rotation
|
2024-05-08 16:40:01 +00:00
|
|
|
let rotation = player_transform.rotation * Quat::from_array([0.0, -1.0, 0.0, 0.0]);
|
2024-03-30 15:18:49 +00:00
|
|
|
|
|
|
|
// Translation
|
|
|
|
if settings.third_person {
|
2024-05-22 02:57:20 +00:00
|
|
|
camera_transform.translation = player_transform.translation
|
|
|
|
+ rotation * (actor.camdistance * Vec3::new(0.0, 0.2, 1.0));
|
2024-05-08 16:40:01 +00:00
|
|
|
camera_transform.rotation = rotation * Quat::from_euler(EulerRot::XYZ, -0.02, 0.0, 0.0);
|
2024-05-22 02:57:20 +00:00
|
|
|
} else {
|
2024-03-30 15:18:49 +00:00
|
|
|
camera_transform.translation = player_transform.translation;
|
2024-05-08 16:40:01 +00:00
|
|
|
camera_transform.rotation = rotation;
|
2024-03-30 15:18:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-18 19:01:03 +00:00
|
|
|
pub fn update_map_camera(
|
|
|
|
settings: Res<var::Settings>,
|
|
|
|
mut mapcam: ResMut<MapCam>,
|
|
|
|
mut q_camera: Query<&mut Transform, (With<Camera>, Without<actor::PlayerCamera>)>,
|
2024-05-07 23:00:58 +00:00
|
|
|
q_playercam: Query<(Entity, &Transform), (With<actor::PlayerCamera>, Without<Camera>)>,
|
2024-05-22 02:57:20 +00:00
|
|
|
q_target: Query<
|
|
|
|
(Entity, &Transform),
|
|
|
|
(
|
|
|
|
With<hud::IsTargeted>,
|
|
|
|
Without<Camera>,
|
|
|
|
Without<actor::PlayerCamera>,
|
|
|
|
),
|
|
|
|
>,
|
2024-04-20 02:19:30 +00:00
|
|
|
q_target_changed: Query<(), Changed<hud::IsTargeted>>,
|
2024-04-18 19:01:03 +00:00
|
|
|
mut mouse_events: EventReader<MouseMotion>,
|
2024-04-19 20:54:27 +00:00
|
|
|
mut er_mousewheel: EventReader<MouseWheel>,
|
2024-04-18 19:01:03 +00:00
|
|
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
|
|
|
) {
|
|
|
|
if !settings.map_active || q_camera.is_empty() || q_playercam.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let mut camera_transform = q_camera.get_single_mut().unwrap();
|
2024-05-07 23:00:58 +00:00
|
|
|
let (player_entity, player_trans) = q_playercam.get_single().unwrap();
|
|
|
|
let (target_entity, target_trans) = if let Ok(target) = q_target.get_single() {
|
2024-04-19 01:54:17 +00:00
|
|
|
target
|
|
|
|
} else {
|
2024-05-07 23:00:58 +00:00
|
|
|
(player_entity, player_trans)
|
2024-04-19 01:54:17 +00:00
|
|
|
};
|
2024-05-07 20:04:47 +00:00
|
|
|
mapcam.center_on_entity = Some(target_entity);
|
2024-04-18 19:01:03 +00:00
|
|
|
|
|
|
|
// Get mouse movement
|
|
|
|
let mut mouse_delta = Vec2::ZERO;
|
|
|
|
for mouse_event in mouse_events.read() {
|
|
|
|
mouse_delta += mouse_event.delta;
|
|
|
|
}
|
|
|
|
// NOTE: we need to subtract a bit from PI/2, otherwise the "up"
|
|
|
|
// direction parameter for the Transform.look_at function is ambiguous
|
|
|
|
// at the extreme values and the orientation will flicker back/forth.
|
2024-05-01 20:14:35 +00:00
|
|
|
let min_zoom: f64 = target_trans.scale.x as f64 * 2.0;
|
2024-04-20 02:39:48 +00:00
|
|
|
let max_zoom: f64 = 17e18; // at this point, camera starts glitching
|
2024-05-22 02:57:20 +00:00
|
|
|
mapcam.pitch = (mapcam.pitch
|
|
|
|
+ mouse_delta.y as f64 / 180.0 * settings.mouse_sensitivity as f64)
|
|
|
|
.clamp(-PI / 2.0 + EPSILON, PI / 2.0 - EPSILON);
|
2024-04-20 02:39:48 +00:00
|
|
|
mapcam.yaw += mouse_delta.x as f64 / 180.0 * settings.mouse_sensitivity as f64;
|
2024-04-18 19:01:03 +00:00
|
|
|
|
2024-04-20 02:19:30 +00:00
|
|
|
// Reset movement offset if target changes
|
|
|
|
if !q_target_changed.is_empty() {
|
|
|
|
mapcam.offset_x = 0.0;
|
|
|
|
mapcam.offset_z = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get keyboard movement
|
2024-04-20 02:39:48 +00:00
|
|
|
let mut offset_x: f64 = 0.0;
|
|
|
|
let mut offset_z: f64 = 0.0;
|
2024-04-20 02:19:30 +00:00
|
|
|
if keyboard_input.pressed(settings.key_forward) {
|
|
|
|
offset_x -= 1.0;
|
|
|
|
}
|
|
|
|
if keyboard_input.pressed(settings.key_back) {
|
|
|
|
offset_x += 1.0;
|
|
|
|
}
|
|
|
|
if keyboard_input.pressed(settings.key_right) {
|
|
|
|
offset_z -= 1.0;
|
|
|
|
}
|
|
|
|
if keyboard_input.pressed(settings.key_left) {
|
|
|
|
offset_z += 1.0;
|
|
|
|
}
|
2024-04-21 22:07:45 +00:00
|
|
|
if keyboard_input.pressed(settings.key_stop) {
|
|
|
|
mapcam.offset_x = 0.0;
|
|
|
|
mapcam.offset_z = 0.0;
|
|
|
|
}
|
2024-04-20 02:19:30 +00:00
|
|
|
|
2024-04-18 19:01:03 +00:00
|
|
|
// Update zoom level
|
2024-04-19 02:41:07 +00:00
|
|
|
if !mapcam.initialized {
|
2024-05-22 02:57:20 +00:00
|
|
|
let factor: f64 = if target_trans == player_trans {
|
|
|
|
7.0
|
|
|
|
} else {
|
|
|
|
1.0
|
|
|
|
};
|
2024-05-01 20:14:35 +00:00
|
|
|
mapcam.target_zoom_level *= target_trans.scale.x as f64 * factor;
|
|
|
|
mapcam.zoom_level *= target_trans.scale.x as f64 * factor;
|
2024-04-19 02:41:07 +00:00
|
|
|
mapcam.initialized = true;
|
|
|
|
}
|
2024-04-20 02:39:48 +00:00
|
|
|
let mut change_zoom: f64 = 0.0;
|
2024-04-18 19:01:03 +00:00
|
|
|
if keyboard_input.pressed(settings.key_map_zoom_out) {
|
2024-04-20 00:33:37 +00:00
|
|
|
change_zoom += 0.5;
|
2024-04-18 19:01:03 +00:00
|
|
|
}
|
|
|
|
if keyboard_input.pressed(settings.key_map_zoom_in) {
|
2024-04-20 00:33:37 +00:00
|
|
|
change_zoom -= 0.5;
|
2024-04-18 19:01:03 +00:00
|
|
|
}
|
2024-04-19 20:54:27 +00:00
|
|
|
for wheel_event in er_mousewheel.read() {
|
2024-04-20 02:39:48 +00:00
|
|
|
change_zoom -= wheel_event.y as f64 * 3.0;
|
2024-04-19 20:54:27 +00:00
|
|
|
}
|
2024-05-22 02:57:20 +00:00
|
|
|
mapcam.target_zoom_level =
|
|
|
|
(mapcam.target_zoom_level * 1.1f64.powf(change_zoom)).clamp(min_zoom, max_zoom);
|
2024-04-18 20:48:21 +00:00
|
|
|
let zoom_speed = 0.05; // should be between 0.0001 (slow) and 1.0 (instant)
|
2024-05-22 02:57:20 +00:00
|
|
|
mapcam.zoom_level = (zoom_speed * mapcam.target_zoom_level
|
|
|
|
+ (1.0 - zoom_speed) * mapcam.zoom_level)
|
|
|
|
.clamp(min_zoom, max_zoom);
|
2024-04-18 19:01:03 +00:00
|
|
|
|
|
|
|
// Update point of view
|
2024-05-22 02:57:20 +00:00
|
|
|
let pov_rotation =
|
|
|
|
DQuat::from_euler(EulerRot::XYZ, 0.0, mapcam.yaw as f64, mapcam.pitch as f64);
|
2024-05-01 20:14:35 +00:00
|
|
|
let point_of_view = pov_rotation * (mapcam.zoom_level as f64 * DVec3::new(1.0, 0.0, 0.0));
|
2024-04-18 19:01:03 +00:00
|
|
|
|
2024-04-20 02:39:48 +00:00
|
|
|
// Update movement offset
|
|
|
|
let mut direction = pov_rotation * DVec3::new(offset_x, 0.0, offset_z);
|
|
|
|
let speed = direction.length();
|
|
|
|
direction.y = 0.0;
|
|
|
|
let direction = speed * direction.normalize_or_zero();
|
|
|
|
|
|
|
|
mapcam.offset_x += 0.01 * (direction.x * mapcam.zoom_level);
|
|
|
|
mapcam.offset_z += 0.01 * (direction.z * mapcam.zoom_level);
|
|
|
|
|
2024-04-18 19:01:03 +00:00
|
|
|
// Apply updates to camera
|
2024-05-01 20:14:35 +00:00
|
|
|
camera_transform.translation = point_of_view.as_vec3();
|
|
|
|
camera_transform.look_at(Vec3::ZERO, Vec3::Y);
|
2024-04-18 19:01:03 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 20:04:47 +00:00
|
|
|
pub fn update_mapcam_center(
|
|
|
|
mut mapcam: ResMut<MapCam>,
|
|
|
|
settings: Res<var::Settings>,
|
|
|
|
q_pos: Query<&Position>,
|
|
|
|
) {
|
|
|
|
if !settings.map_active {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if let Some(entity) = mapcam.center_on_entity {
|
|
|
|
if let Ok(pos) = q_pos.get(entity) {
|
|
|
|
let offset = DVec3::new(mapcam.offset_x, 0.0, mapcam.offset_z);
|
|
|
|
mapcam.center = **pos + offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-30 15:41:22 +00:00
|
|
|
pub fn update_fov(
|
2024-04-08 00:36:47 +00:00
|
|
|
q_player: Query<&actor::ExperiencesGForce, With<actor::Player>>,
|
2024-04-05 21:38:20 +00:00
|
|
|
mouse_input: Res<ButtonInput<MouseButton>>,
|
2024-04-14 12:55:00 +00:00
|
|
|
mut settings: ResMut<var::Settings>,
|
2024-03-30 15:41:22 +00:00
|
|
|
mut q_camera: Query<&mut Projection, With<Camera>>,
|
|
|
|
) {
|
2024-05-22 02:57:20 +00:00
|
|
|
if let (Ok(gforce), Ok(mut projection)) = (q_player.get_single(), q_camera.get_single_mut()) {
|
2024-04-05 21:38:20 +00:00
|
|
|
let fov: f32;
|
2024-05-22 02:11:39 +00:00
|
|
|
if mouse_input.pressed(settings.key_zoom) {
|
2024-04-08 01:15:45 +00:00
|
|
|
fov = settings.zoom_fov.to_radians();
|
2024-04-05 21:38:20 +00:00
|
|
|
if !settings.is_zooming {
|
|
|
|
settings.is_zooming = true;
|
|
|
|
}
|
|
|
|
} else {
|
2024-05-22 02:57:20 +00:00
|
|
|
fov = (gforce.visual_effect.clamp(0.0, 1.0) * settings.fov_highspeed + settings.fov)
|
|
|
|
.to_radians();
|
2024-04-05 21:38:20 +00:00
|
|
|
if settings.is_zooming {
|
|
|
|
settings.is_zooming = false;
|
|
|
|
}
|
|
|
|
};
|
2024-05-22 02:57:20 +00:00
|
|
|
*projection = Projection::Perspective(PerspectiveProjection {
|
|
|
|
fov: fov,
|
|
|
|
..default()
|
|
|
|
});
|
2024-03-30 15:41:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-29 18:41:46 +00:00
|
|
|
pub fn handle_input(
|
|
|
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
2024-05-13 19:11:27 +00:00
|
|
|
settings: Res<var::Settings>,
|
2024-04-11 19:06:21 +00:00
|
|
|
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
|
2024-05-13 19:11:27 +00:00
|
|
|
mut ew_game: EventWriter<GameEvent>,
|
2024-03-29 18:41:46 +00:00
|
|
|
) {
|
|
|
|
if keyboard_input.just_pressed(settings.key_camera) {
|
2024-05-13 19:11:27 +00:00
|
|
|
ew_game.send(GameEvent::SetThirdPerson(Toggle));
|
2024-03-29 18:41:46 +00:00
|
|
|
}
|
2024-04-18 19:01:03 +00:00
|
|
|
if keyboard_input.just_pressed(settings.key_map) {
|
2024-05-13 19:11:27 +00:00
|
|
|
ew_game.send(GameEvent::SetMap(Toggle));
|
2024-04-18 19:01:03 +00:00
|
|
|
}
|
2024-04-11 19:06:21 +00:00
|
|
|
if keyboard_input.just_pressed(settings.key_rotation_stabilizer) {
|
2024-05-13 19:11:27 +00:00
|
|
|
ew_game.send(GameEvent::SetRotationStabilizer(Toggle));
|
2024-04-25 01:52:32 +00:00
|
|
|
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Switch));
|
2024-04-11 19:06:21 +00:00
|
|
|
}
|
2024-03-29 18:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn manage_player_actor(
|
2024-04-17 11:55:39 +00:00
|
|
|
mut commands: Commands,
|
2024-04-14 12:55:00 +00:00
|
|
|
settings: Res<var::Settings>,
|
2024-03-30 14:37:51 +00:00
|
|
|
mut q_playercam: Query<&mut Visibility, With<actor::PlayerCamera>>,
|
2024-05-22 02:57:20 +00:00
|
|
|
mut q_hiddenplayer: Query<
|
|
|
|
(
|
|
|
|
Entity,
|
|
|
|
&mut Visibility,
|
|
|
|
&mut Position,
|
|
|
|
&mut Rotation,
|
|
|
|
&mut LinearVelocity,
|
|
|
|
&mut AngularVelocity,
|
|
|
|
Option<&mut actor::ExperiencesGForce>,
|
|
|
|
Option<&actor::JustNowEnteredVehicle>,
|
|
|
|
),
|
|
|
|
(With<actor::Player>, Without<actor::PlayerCamera>),
|
|
|
|
>,
|
|
|
|
q_ride: Query<
|
|
|
|
(
|
|
|
|
&Transform,
|
|
|
|
&Position,
|
|
|
|
&Rotation,
|
|
|
|
&LinearVelocity,
|
|
|
|
&AngularVelocity,
|
|
|
|
),
|
|
|
|
(With<actor::PlayerDrivesThis>, Without<actor::Player>),
|
|
|
|
>,
|
2024-03-29 18:41:46 +00:00
|
|
|
) {
|
|
|
|
for mut vis in &mut q_playercam {
|
2024-04-18 19:01:03 +00:00
|
|
|
if settings.third_person || settings.map_active {
|
2024-03-29 18:41:46 +00:00
|
|
|
*vis = Visibility::Inherited;
|
2024-05-22 02:57:20 +00:00
|
|
|
} else {
|
2024-03-29 18:41:46 +00:00
|
|
|
*vis = Visibility::Hidden;
|
|
|
|
}
|
|
|
|
}
|
2024-05-22 02:57:20 +00:00
|
|
|
for (entity, mut vis, mut pos, mut rot, mut v, mut angv, mut gforce, entering) in
|
|
|
|
&mut q_hiddenplayer
|
|
|
|
{
|
2024-04-03 11:53:49 +00:00
|
|
|
// If we are riding a vehicle, place the player at the position where
|
|
|
|
// it would be after exiting the vehicle.
|
|
|
|
// I would rather place it in the center of the vehicle, but at the time
|
|
|
|
// of writing, I couldn't set the position/rotation of the player *during*
|
|
|
|
// exiting the vehicle, so I'm doing it here instead, as a workaround.
|
2024-03-30 17:48:19 +00:00
|
|
|
*vis = Visibility::Hidden;
|
2024-04-03 10:26:56 +00:00
|
|
|
if let Ok((ride_trans, ride_pos, ride_rot, ride_v, ride_angv)) = q_ride.get_single() {
|
2024-05-22 02:57:20 +00:00
|
|
|
pos.0 = ride_pos.0
|
|
|
|
+ DVec3::from(ride_trans.rotation * Vec3::new(0.0, 0.0, ride_trans.scale.z * 2.0));
|
2024-04-03 10:26:56 +00:00
|
|
|
rot.0 = ride_rot.0 * DQuat::from_array([-1.0, 0.0, 0.0, 0.0]);
|
2024-03-30 21:31:07 +00:00
|
|
|
*v = ride_v.clone();
|
|
|
|
*angv = ride_angv.clone();
|
2024-04-17 11:55:39 +00:00
|
|
|
|
|
|
|
// I really don't want people to die from the g-forces of entering
|
|
|
|
// vehicles at high relative speed, even though they probably should.
|
|
|
|
if let (Some(gforce), Some(_)) = (&mut gforce, entering) {
|
|
|
|
gforce.last_linear_velocity = v.0;
|
2024-05-22 02:57:20 +00:00
|
|
|
commands
|
|
|
|
.entity(entity)
|
|
|
|
.remove::<actor::JustNowEnteredVehicle>();
|
2024-04-17 11:55:39 +00:00
|
|
|
}
|
2024-03-30 21:31:07 +00:00
|
|
|
}
|
2024-03-30 17:48:19 +00:00
|
|
|
}
|
2024-03-29 18:41:46 +00:00
|
|
|
}
|
|
|
|
|
2024-03-16 20:00:40 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2024-04-05 20:16:01 +00:00
|
|
|
pub fn apply_input_to_player(
|
2024-03-16 20:00:40 +00:00
|
|
|
time: Res<Time>,
|
2024-05-22 21:29:54 +00:00
|
|
|
mut commands: Commands,
|
2024-04-14 12:55:00 +00:00
|
|
|
settings: Res<var::Settings>,
|
2024-06-10 23:04:23 +00:00
|
|
|
jupiter_pos: Res<game::JupiterPos>,
|
2024-04-10 20:51:11 +00:00
|
|
|
windows: Query<&Window, With<PrimaryWindow>>,
|
2024-03-16 20:00:40 +00:00
|
|
|
mut mouse_events: EventReader<MouseMotion>,
|
|
|
|
key_input: Res<ButtonInput<KeyCode>>,
|
2024-05-13 02:20:18 +00:00
|
|
|
q_audiosinks: Query<(&audio::Sfx, &AudioSink)>,
|
2024-04-05 18:57:21 +00:00
|
|
|
q_target: Query<&LinearVelocity, (With<hud::IsTargeted>, Without<actor::PlayerCamera>)>,
|
2024-07-14 19:08:41 +00:00
|
|
|
q_closest: Query<
|
|
|
|
(&Position, &LinearVelocity),
|
2024-07-14 19:16:32 +00:00
|
|
|
(
|
|
|
|
Without<hud::IsTargeted>,
|
|
|
|
Without<actor::PlayerCamera>,
|
|
|
|
Without<actor::Player>,
|
|
|
|
Without<actor::PlayerDrivesThis>,
|
|
|
|
),
|
2024-07-14 19:08:41 +00:00
|
|
|
>,
|
2024-05-22 02:57:20 +00:00
|
|
|
mut q_playercam: Query<
|
|
|
|
(
|
2024-05-22 21:29:54 +00:00
|
|
|
Entity,
|
2024-03-31 01:09:14 +00:00
|
|
|
&Transform,
|
2024-03-29 15:33:12 +00:00
|
|
|
&mut actor::Engine,
|
2024-06-10 23:04:23 +00:00
|
|
|
&Position,
|
2024-03-29 15:33:12 +00:00
|
|
|
&mut LinearVelocity,
|
2024-03-31 01:09:14 +00:00
|
|
|
&mut ExternalTorque,
|
2024-04-11 19:42:42 +00:00
|
|
|
Option<&actor::PlayerDrivesThis>,
|
2024-05-22 02:57:20 +00:00
|
|
|
),
|
|
|
|
(With<actor::PlayerCamera>, Without<Camera>),
|
|
|
|
>,
|
2024-03-16 20:00:40 +00:00
|
|
|
) {
|
2024-05-13 18:21:56 +00:00
|
|
|
if settings.map_active || !settings.in_control() {
|
2024-04-18 19:01:03 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-03-16 20:00:40 +00:00
|
|
|
let dt = time.delta_seconds();
|
2024-03-16 23:41:06 +00:00
|
|
|
let mut play_thruster_sound = false;
|
2024-04-01 14:29:14 +00:00
|
|
|
let mut axis_input: DVec3 = DVec3::ZERO;
|
2024-03-16 20:00:40 +00:00
|
|
|
|
2024-04-10 20:51:11 +00:00
|
|
|
let (win_res_x, win_res_y): (f32, f32);
|
2024-03-16 23:24:47 +00:00
|
|
|
let mut focused = true;
|
2024-04-10 20:51:11 +00:00
|
|
|
if let Ok(window) = &windows.get_single() {
|
|
|
|
focused = window.focused;
|
|
|
|
win_res_x = window.resolution.width();
|
|
|
|
win_res_y = window.resolution.height();
|
2024-05-22 02:57:20 +00:00
|
|
|
} else {
|
2024-04-10 20:51:11 +00:00
|
|
|
win_res_x = 1920.0;
|
|
|
|
win_res_y = 1050.0;
|
2024-03-16 23:24:47 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 23:04:23 +00:00
|
|
|
if let Ok((player_entity, player_transform, mut engine, pos, mut v, mut torque, bike)) =
|
2024-05-22 02:57:20 +00:00
|
|
|
q_playercam.get_single_mut()
|
|
|
|
{
|
2024-06-10 23:04:23 +00:00
|
|
|
let target_v: DVec3 = if let Ok(target) = q_target.get_single() {
|
|
|
|
target.0
|
|
|
|
} else {
|
2024-07-13 12:30:02 +00:00
|
|
|
let mut closest_distance = MAX_DIST_FOR_MATCH_VELOCITY;
|
|
|
|
let mut closest_velocity = None;
|
|
|
|
for (testpos, v) in q_closest.iter() {
|
|
|
|
let distance = (pos.0 - testpos.0).length();
|
|
|
|
if distance < closest_distance {
|
|
|
|
closest_velocity = Some(v);
|
|
|
|
closest_distance = distance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if closest_velocity.is_some() {
|
|
|
|
closest_velocity.unwrap().0
|
|
|
|
} else {
|
|
|
|
let relative_pos = pos.0 - jupiter_pos.0;
|
|
|
|
nature::orbital_velocity(relative_pos, nature::JUPITER_MASS)
|
|
|
|
}
|
2024-06-10 23:04:23 +00:00
|
|
|
};
|
2024-03-16 20:00:40 +00:00
|
|
|
// Handle key input
|
2024-03-16 23:24:47 +00:00
|
|
|
if focused {
|
2024-05-15 02:58:55 +00:00
|
|
|
if key_input.pressed(settings.key_forward) || settings.cruise_control_active {
|
2024-03-29 18:41:46 +00:00
|
|
|
axis_input.z += 1.2;
|
2024-03-16 23:24:47 +00:00
|
|
|
}
|
2024-03-18 03:39:26 +00:00
|
|
|
if key_input.pressed(settings.key_back) {
|
2024-03-29 18:41:46 +00:00
|
|
|
axis_input.z -= 1.2;
|
2024-03-16 23:24:47 +00:00
|
|
|
}
|
2024-03-18 03:39:26 +00:00
|
|
|
if key_input.pressed(settings.key_right) {
|
2024-03-29 18:41:46 +00:00
|
|
|
axis_input.x -= 1.2;
|
2024-03-16 23:24:47 +00:00
|
|
|
}
|
2024-03-18 03:39:26 +00:00
|
|
|
if key_input.pressed(settings.key_left) {
|
2024-03-29 18:41:46 +00:00
|
|
|
axis_input.x += 1.2;
|
2024-03-16 23:24:47 +00:00
|
|
|
}
|
2024-03-18 03:39:26 +00:00
|
|
|
if key_input.pressed(settings.key_up) {
|
2024-03-29 01:49:16 +00:00
|
|
|
axis_input.y += 1.2;
|
2024-03-16 23:24:47 +00:00
|
|
|
}
|
2024-03-18 03:39:26 +00:00
|
|
|
if key_input.pressed(settings.key_down) {
|
2024-03-29 01:49:16 +00:00
|
|
|
axis_input.y -= 1.2;
|
2024-03-16 23:24:47 +00:00
|
|
|
}
|
2024-03-29 01:40:55 +00:00
|
|
|
if key_input.pressed(settings.key_stop) {
|
2024-04-05 18:57:21 +00:00
|
|
|
let stop_direction = (target_v - v.0).normalize();
|
2024-03-29 01:40:55 +00:00
|
|
|
if stop_direction.length_squared() > 0.3 {
|
2024-05-22 02:57:20 +00:00
|
|
|
axis_input += 1.0
|
|
|
|
* DVec3::from(
|
|
|
|
player_transform.rotation.inverse() * stop_direction.as_vec3(),
|
|
|
|
);
|
2024-03-29 01:40:55 +00:00
|
|
|
}
|
2024-03-21 17:45:43 +00:00
|
|
|
}
|
2024-05-15 13:07:07 +00:00
|
|
|
} else {
|
|
|
|
if settings.cruise_control_active {
|
|
|
|
axis_input.z += 1.2;
|
|
|
|
}
|
2024-03-21 17:45:43 +00:00
|
|
|
}
|
2024-03-29 01:41:05 +00:00
|
|
|
// 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.
|
2024-04-01 14:29:14 +00:00
|
|
|
axis_input = axis_input.clamp(DVec3::splat(-1.0), DVec3::splat(1.0));
|
2024-03-17 13:39:42 +00:00
|
|
|
|
2024-03-16 20:00:40 +00:00
|
|
|
// Apply movement update
|
2024-05-22 02:57:20 +00:00
|
|
|
let forward_factor = engine.current_warmup
|
|
|
|
* (if axis_input.z > 0.0 {
|
|
|
|
engine.thrust_forward
|
|
|
|
} else {
|
|
|
|
engine.thrust_back
|
|
|
|
});
|
2024-03-29 00:40:58 +00:00
|
|
|
let right_factor = engine.thrust_sideways * engine.current_warmup;
|
|
|
|
let up_factor = engine.thrust_sideways * engine.current_warmup;
|
2024-04-01 14:29:14 +00:00
|
|
|
let factor = DVec3::new(right_factor as f64, up_factor as f64, forward_factor as f64);
|
2024-03-29 00:40:58 +00:00
|
|
|
|
2024-03-29 01:40:55 +00:00
|
|
|
if axis_input.length_squared() > 0.003 {
|
2024-05-22 02:57:20 +00:00
|
|
|
let acceleration_global: DVec3 =
|
|
|
|
DVec3::from(player_transform.rotation * (axis_input * factor).as_vec3());
|
|
|
|
let mut acceleration_total: DVec3 =
|
|
|
|
(actor::ENGINE_SPEED_FACTOR * dt) as f64 * acceleration_global;
|
2024-03-29 01:56:48 +00:00
|
|
|
let threshold = 1e-5;
|
|
|
|
if key_input.pressed(settings.key_stop) {
|
2024-04-05 21:11:10 +00:00
|
|
|
// Decelerate (or match velocity to target_v)
|
2024-04-05 21:10:40 +00:00
|
|
|
let dv = v.0 - target_v;
|
2024-03-29 01:56:48 +00:00
|
|
|
for i in 0..3 {
|
2024-04-05 18:57:21 +00:00
|
|
|
if dv[i].abs() < threshold {
|
|
|
|
v[i] = target_v[i];
|
2024-05-22 02:57:20 +00:00
|
|
|
} else if dv[i].signum() != (dv[i] + acceleration_total[i]).signum() {
|
2024-03-30 18:36:43 +00:00
|
|
|
// Almost stopped, but we overshot v=0
|
2024-04-05 18:57:21 +00:00
|
|
|
v[i] = target_v[i];
|
2024-03-29 01:56:48 +00:00
|
|
|
acceleration_total[i] = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-16 17:44:05 +00:00
|
|
|
// TODO: handle mass
|
2024-03-29 15:33:12 +00:00
|
|
|
v.0 += acceleration_total;
|
2024-05-22 02:57:20 +00:00
|
|
|
engine.current_warmup =
|
|
|
|
(engine.current_warmup + dt / engine.warmup_seconds).clamp(0.0, 1.0);
|
2024-04-03 12:27:44 +00:00
|
|
|
play_thruster_sound = true;
|
2024-05-22 02:57:20 +00:00
|
|
|
} else {
|
|
|
|
engine.current_warmup =
|
|
|
|
(engine.current_warmup - dt / engine.warmup_seconds).clamp(0.0, 1.0);
|
2024-03-29 01:21:28 +00:00
|
|
|
}
|
2024-03-21 17:45:43 +00:00
|
|
|
|
2024-03-30 18:36:43 +00:00
|
|
|
// Handle mouse input and mouse-like key bindings
|
2024-03-31 00:07:35 +00:00
|
|
|
let mut play_reactionwheel_sound = false;
|
2024-03-16 20:00:40 +00:00
|
|
|
let mut mouse_delta = Vec2::ZERO;
|
2024-03-30 18:36:43 +00:00
|
|
|
let mut pitch_yaw_rot = Vec3::ZERO;
|
2024-05-22 02:57:20 +00:00
|
|
|
let sensitivity_factor = if settings.is_zooming {
|
|
|
|
settings.zoom_sensitivity_factor
|
|
|
|
} else {
|
|
|
|
1.0
|
|
|
|
};
|
2024-04-15 11:17:10 +00:00
|
|
|
let mouseless_sensitivity = 8.0 * sensitivity_factor;
|
|
|
|
let mouseless_rotation_sensitivity = 40.0 * sensitivity_factor;
|
2024-03-30 18:36:43 +00:00
|
|
|
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) {
|
2024-05-22 02:57:20 +00:00
|
|
|
pitch_yaw_rot[1] += mouseless_sensitivity;
|
|
|
|
}
|
2024-03-30 18:36:43 +00:00
|
|
|
if key_input.pressed(settings.key_mouseright) {
|
|
|
|
pitch_yaw_rot[1] -= mouseless_sensitivity;
|
|
|
|
}
|
|
|
|
if key_input.pressed(settings.key_rotateleft) {
|
2024-04-15 11:17:10 +00:00
|
|
|
pitch_yaw_rot[2] -= mouseless_rotation_sensitivity;
|
2024-03-30 18:36:43 +00:00
|
|
|
}
|
|
|
|
if key_input.pressed(settings.key_rotateright) {
|
2024-04-15 11:17:10 +00:00
|
|
|
pitch_yaw_rot[2] += mouseless_rotation_sensitivity;
|
2024-03-30 18:36:43 +00:00
|
|
|
}
|
2024-03-18 03:39:26 +00:00
|
|
|
for mouse_event in mouse_events.read() {
|
|
|
|
mouse_delta += mouse_event.delta;
|
2024-03-16 20:00:40 +00:00
|
|
|
}
|
|
|
|
if mouse_delta != Vec2::ZERO {
|
2024-03-30 18:14:59 +00:00
|
|
|
if key_input.pressed(settings.key_rotate) {
|
2024-04-10 20:51:11 +00:00
|
|
|
pitch_yaw_rot[2] += 1000.0 * mouse_delta.x / win_res_x;
|
2024-03-30 18:14:59 +00:00
|
|
|
} else {
|
2024-04-10 20:51:11 +00:00
|
|
|
pitch_yaw_rot[0] += 1000.0 * mouse_delta.y / win_res_y;
|
|
|
|
pitch_yaw_rot[1] -= 1000.0 * mouse_delta.x / win_res_x;
|
2024-03-30 18:14:59 +00:00
|
|
|
}
|
2024-03-30 18:36:43 +00:00
|
|
|
}
|
2024-03-31 01:19:15 +00:00
|
|
|
|
2024-05-22 21:50:28 +00:00
|
|
|
let mouse_speed = pitch_yaw_rot.length();
|
|
|
|
let mouse_moving = mouse_speed > EPSILON32;
|
|
|
|
|
|
|
|
if mouse_moving {
|
2024-03-31 00:07:35 +00:00
|
|
|
play_reactionwheel_sound = true;
|
2024-05-22 02:57:20 +00:00
|
|
|
pitch_yaw_rot *=
|
|
|
|
settings.mouse_sensitivity * sensitivity_factor * engine.reaction_wheels;
|
2024-04-01 14:29:14 +00:00
|
|
|
torque.apply_torque(DVec3::from(
|
2024-05-22 02:57:20 +00:00
|
|
|
player_transform.rotation
|
|
|
|
* Vec3::new(pitch_yaw_rot[0], pitch_yaw_rot[1], pitch_yaw_rot[2]),
|
|
|
|
));
|
2024-05-22 21:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if settings.rotation_stabilizer_active || key_input.pressed(settings.key_stop) {
|
2024-05-22 21:31:57 +00:00
|
|
|
commands
|
|
|
|
.entity(player_entity)
|
2024-05-22 22:40:47 +00:00
|
|
|
.try_insert(actor::WantsMaxRotation(mouse_speed as f64 * 0.1));
|
2024-05-22 21:31:57 +00:00
|
|
|
} else {
|
2024-05-22 21:29:54 +00:00
|
|
|
commands
|
|
|
|
.entity(player_entity)
|
|
|
|
.remove::<actor::WantsMaxRotation>();
|
2024-03-16 20:00:40 +00:00
|
|
|
}
|
2024-03-16 23:41:06 +00:00
|
|
|
|
2024-05-13 02:20:18 +00:00
|
|
|
let mut sinks: HashMap<audio::Sfx, &AudioSink> = HashMap::new();
|
|
|
|
for (sfx, sink) in &q_audiosinks {
|
|
|
|
sinks.insert(*sfx, sink);
|
|
|
|
}
|
|
|
|
|
2024-03-30 18:36:43 +00:00
|
|
|
// Play sound effects
|
2024-05-13 02:20:18 +00:00
|
|
|
if let Some(sink) = sinks.get(&audio::Sfx::ElectricMotor) {
|
2024-09-15 03:11:57 +00:00
|
|
|
let reactionwheel_volume = 1.5;
|
2024-03-31 00:07:35 +00:00
|
|
|
let volume = sink.volume();
|
2024-03-31 03:13:13 +00:00
|
|
|
let speed = sink.speed();
|
|
|
|
let action = pitch_yaw_rot.length_squared().powf(0.2) * 0.0005;
|
2024-04-11 19:42:42 +00:00
|
|
|
if play_reactionwheel_sound && !settings.mute_sfx && bike.is_some() {
|
2024-09-15 03:11:57 +00:00
|
|
|
sink.set_volume(reactionwheel_volume * (volume + action).clamp(0.0, 1.0));
|
2024-03-31 03:13:13 +00:00
|
|
|
sink.set_speed((speed + action * 0.2).clamp(0.2, 0.5));
|
2024-03-31 00:07:35 +00:00
|
|
|
sink.play()
|
|
|
|
} else {
|
|
|
|
if volume <= 0.01 {
|
|
|
|
sink.pause()
|
|
|
|
} else {
|
2024-03-31 03:13:13 +00:00
|
|
|
sink.set_volume((volume - 0.01).clamp(0.0, 1.0));
|
|
|
|
sink.set_speed((speed - 0.03).clamp(0.2, 0.5));
|
2024-03-31 00:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-15 19:55:46 +00:00
|
|
|
let sinks = vec![
|
2024-05-22 02:57:20 +00:00
|
|
|
(
|
2024-09-15 03:11:57 +00:00
|
|
|
1.0,
|
2024-05-22 02:57:20 +00:00
|
|
|
actor::EngineType::Monopropellant,
|
|
|
|
sinks.get(&audio::Sfx::Thruster),
|
|
|
|
),
|
2024-09-15 03:11:57 +00:00
|
|
|
(1.0, actor::EngineType::Ion, sinks.get(&audio::Sfx::Ion)),
|
2024-04-15 19:55:46 +00:00
|
|
|
];
|
|
|
|
let seconds_to_max_vol = 0.05;
|
|
|
|
let seconds_to_min_vol = 0.05;
|
|
|
|
for sink_data in sinks {
|
2024-05-13 02:20:18 +00:00
|
|
|
if let (vol_boost, engine_type, Some(sink)) = sink_data {
|
2024-04-15 19:55:46 +00:00
|
|
|
if settings.mute_sfx {
|
|
|
|
sink.pause();
|
2024-05-22 02:57:20 +00:00
|
|
|
} else {
|
2024-04-15 19:55:46 +00:00
|
|
|
let volume = sink.volume();
|
|
|
|
if engine.engine_type == engine_type {
|
|
|
|
if play_thruster_sound {
|
|
|
|
sink.play();
|
|
|
|
if volume < 1.0 {
|
|
|
|
let maxvol = vol_boost;
|
|
|
|
//let maxvol = engine.current_warmup * vol_boost;
|
2024-05-22 02:57:20 +00:00
|
|
|
sink.set_volume(
|
|
|
|
(volume + dt / seconds_to_max_vol).clamp(0.0, maxvol),
|
|
|
|
);
|
2024-04-15 19:55:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sink.set_volume((volume - dt / seconds_to_min_vol).clamp(0.0, 1.0));
|
|
|
|
}
|
2024-05-22 02:57:20 +00:00
|
|
|
} else if volume > 0.0 {
|
2024-04-15 19:55:46 +00:00
|
|
|
sink.set_volume((volume - dt / seconds_to_min_vol).clamp(0.0, 1.0));
|
|
|
|
}
|
|
|
|
if volume < 0.0001 {
|
|
|
|
sink.pause();
|
|
|
|
}
|
|
|
|
}
|
2024-03-29 03:36:20 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-16 20:00:40 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-05 17:11:34 +00:00
|
|
|
|
2024-04-20 00:09:04 +00:00
|
|
|
pub fn update_map_only_object_visibility(
|
|
|
|
settings: Res<var::Settings>,
|
|
|
|
q_camera: Query<&Transform, With<Camera>>,
|
|
|
|
q_player: Query<&Position, With<actor::PlayerCamera>>,
|
|
|
|
mut q_onlyinmap: Query<(&mut Visibility, &ShowOnlyInMap), Without<Camera>>,
|
2024-05-12 21:57:21 +00:00
|
|
|
id2pos: Res<game::Id2Pos>,
|
2024-04-20 00:09:04 +00:00
|
|
|
) {
|
|
|
|
if q_camera.is_empty() || q_player.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let cam: &Transform = q_camera.get_single().unwrap();
|
|
|
|
let player_pos: &Position = q_player.get_single().unwrap();
|
|
|
|
let cam_pos: Vec3 = cam.translation + player_pos.as_vec3();
|
|
|
|
for (mut vis, onlyinmap) in &mut q_onlyinmap {
|
2024-04-20 00:37:00 +00:00
|
|
|
if settings.map_active && settings.hud_active {
|
|
|
|
if let Some(pos) = id2pos.0.get(&onlyinmap.distance_to_id) {
|
2024-04-20 00:09:04 +00:00
|
|
|
let dist = cam_pos.distance(pos.as_vec3());
|
|
|
|
if dist >= onlyinmap.min_distance as f32 {
|
|
|
|
*vis = Visibility::Inherited;
|
2024-05-22 02:57:20 +00:00
|
|
|
} else {
|
2024-04-20 00:09:04 +00:00
|
|
|
*vis = Visibility::Hidden;
|
|
|
|
}
|
2024-04-20 00:37:00 +00:00
|
|
|
} else {
|
2024-05-22 02:57:20 +00:00
|
|
|
error!(
|
|
|
|
"Failed get position of actor ID '{}'",
|
|
|
|
&onlyinmap.distance_to_id
|
|
|
|
);
|
2024-04-20 00:09:04 +00:00
|
|
|
*vis = Visibility::Hidden;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*vis = Visibility::Hidden;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 17:11:34 +00:00
|
|
|
// Find the closest world object that the player is looking at
|
2024-04-05 20:06:58 +00:00
|
|
|
#[inline]
|
2024-04-05 17:34:01 +00:00
|
|
|
pub fn find_closest_target<TargetSpecifier>(
|
|
|
|
objects: Vec<(TargetSpecifier, &Transform)>,
|
|
|
|
camera_transform: &Transform,
|
|
|
|
) -> (Option<TargetSpecifier>, f32)
|
2024-05-22 02:57:20 +00:00
|
|
|
where
|
|
|
|
TargetSpecifier: Clone,
|
2024-04-05 17:11:34 +00:00
|
|
|
{
|
2024-04-05 17:34:01 +00:00
|
|
|
let mut closest_entity: Option<TargetSpecifier> = None;
|
2024-04-05 17:11:34 +00:00
|
|
|
let mut closest_distance: f32 = f32::MAX;
|
2024-05-22 02:57:20 +00:00
|
|
|
let target_vector: Vec3 =
|
|
|
|
(camera_transform.rotation * Vec3::new(0.0, 0.0, -1.0)).normalize_or_zero();
|
2024-04-05 17:34:01 +00:00
|
|
|
for (entity, trans) in objects {
|
|
|
|
// Use Transform instead of Position because we're basing this
|
|
|
|
// not on the player mesh but on the camera, which doesn't have a position.
|
2024-05-22 02:57:20 +00:00
|
|
|
let (angular_diameter, angle, distance) =
|
|
|
|
calc_angular_diameter_known_target_vector(trans, camera_transform, &target_vector);
|
2024-05-12 22:52:34 +00:00
|
|
|
if angle <= angular_diameter.clamp(0.01, PI32) {
|
2024-04-05 17:34:01 +00:00
|
|
|
// It's in the field of view!
|
|
|
|
//commands.entity(entity).insert(IsTargeted);
|
2024-05-22 02:57:20 +00:00
|
|
|
let distance_to_surface = distance - trans.scale.x;
|
2024-04-15 23:54:34 +00:00
|
|
|
if distance_to_surface < closest_distance {
|
|
|
|
closest_distance = distance_to_surface;
|
2024-04-05 17:59:53 +00:00
|
|
|
closest_entity = Some(entity);
|
2024-04-05 17:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (closest_entity, closest_distance);
|
|
|
|
}
|
2024-04-05 20:06:58 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn calc_angular_diameter_known_target_vector(
|
|
|
|
target: &Transform,
|
|
|
|
camera: &Transform,
|
|
|
|
target_vector: &Vec3,
|
|
|
|
) -> (f32, f32, f32) {
|
2024-05-22 02:57:20 +00:00
|
|
|
let pos_vector: Vec3 = (target.translation - camera.translation).normalize_or_zero();
|
2024-04-05 20:06:58 +00:00
|
|
|
let cosine_of_angle: f32 = target_vector.dot(pos_vector);
|
|
|
|
let angle: f32 = cosine_of_angle.acos();
|
|
|
|
let distance: f32 = target.translation.distance(camera.translation);
|
|
|
|
let leeway: f32 = 1.3;
|
|
|
|
let angular_diameter: f32 = if distance > 0.0 {
|
|
|
|
// Angular Diameter
|
|
|
|
leeway * (target.scale[0] / distance).asin()
|
2024-05-22 02:57:20 +00:00
|
|
|
} else {
|
2024-04-05 20:06:58 +00:00
|
|
|
0.0
|
|
|
|
};
|
|
|
|
return (angular_diameter, angle, distance);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2024-05-22 02:57:20 +00:00
|
|
|
pub fn calc_angular_diameter(target: &Transform, camera: &Transform) -> (f32, f32, f32) {
|
|
|
|
let target_vector: Vec3 = (camera.rotation * Vec3::new(0.0, 0.0, -1.0)).normalize_or_zero();
|
2024-04-05 20:06:58 +00:00
|
|
|
return calc_angular_diameter_known_target_vector(target, camera, &target_vector);
|
|
|
|
}
|
2024-05-01 19:50:59 +00:00
|
|
|
|
|
|
|
// An extension of bevy_xpbd_3d::plugins::position_to_transform that adjusts
|
|
|
|
// the rendering position to center entities at the player camera.
|
|
|
|
// This avoids rendering glitches when very far away from the origin.
|
|
|
|
pub fn position_to_transform(
|
2024-05-01 20:14:35 +00:00
|
|
|
mapcam: Res<MapCam>,
|
|
|
|
settings: Res<var::Settings>,
|
2024-05-01 19:50:59 +00:00
|
|
|
q_player: Query<&Position, With<actor::PlayerCamera>>,
|
2024-05-22 02:57:20 +00:00
|
|
|
mut q_trans: Query<
|
|
|
|
(&'static mut Transform, &'static Position, &'static Rotation),
|
|
|
|
Without<Parent>,
|
|
|
|
>,
|
2024-05-01 19:50:59 +00:00
|
|
|
) {
|
2024-05-01 20:14:35 +00:00
|
|
|
let center: DVec3 = if settings.map_active {
|
|
|
|
mapcam.center
|
2024-05-12 20:26:40 +00:00
|
|
|
} else if let Ok(player_pos) = q_player.get_single() {
|
2024-05-01 20:14:35 +00:00
|
|
|
**player_pos
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (mut transform, pos, rot) in &mut q_trans {
|
|
|
|
transform.translation = Vec3::new(
|
|
|
|
(pos.x - center.x) as f32,
|
|
|
|
(pos.y - center.y) as f32,
|
|
|
|
(pos.z - center.z) as f32,
|
|
|
|
);
|
|
|
|
transform.rotation = rot.as_quat();
|
2024-05-01 19:50:59 +00:00
|
|
|
}
|
|
|
|
}
|