From 60309f84a0b61fd7720054dc8baa3949505db858 Mon Sep 17 00:00:00 2001 From: hut Date: Tue, 7 May 2024 22:04:47 +0200 Subject: [PATCH] update mapcam.center fast enough to avoid flicker on moving target --- src/camera.rs | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index 14f710d..d658bdc 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -40,6 +40,9 @@ impl Plugin for CameraPlugin { .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)); app.add_systems(Update, update_map_camera); app.add_systems(Update, update_fov); app.add_systems(PreUpdate, apply_input_to_player); @@ -74,6 +77,7 @@ pub struct MapCam { pub offset_x: f64, pub offset_z: f64, pub center: DVec3, + pub center_on_entity: Option, } impl Default for MapCam { fn default() -> Self { @@ -86,6 +90,7 @@ impl Default for MapCam { offset_x: 0.0, offset_z: 0.0, center: DVec3::new(0.0, 0.0, 0.0), + center_on_entity: None, } } } @@ -161,8 +166,8 @@ pub fn update_map_camera( settings: Res, mut mapcam: ResMut, mut q_camera: Query<&mut Transform, (With, Without)>, - q_playercam: Query<(&Transform, &Position), (With, Without)>, - q_target: Query<(&Transform, &Position), (With, Without, Without)>, + q_playercam: Query<(Entity, &Transform, &Position), (With, Without)>, + q_target: Query<(Entity, &Transform, &Position), (With, Without, Without)>, q_target_changed: Query<(), Changed>, mut mouse_events: EventReader, mut er_mousewheel: EventReader, @@ -172,12 +177,13 @@ pub fn update_map_camera( return; } let mut camera_transform = q_camera.get_single_mut().unwrap(); - let (player_trans, player_pos) = q_playercam.get_single().unwrap(); - let (target_trans, target_pos) = if let Ok(target) = q_target.get_single() { + let (player_entity, player_trans, player_pos) = q_playercam.get_single().unwrap(); + let (target_entity, target_trans, target_pos) = if let Ok(target) = q_target.get_single() { target } else { - (player_trans, player_pos) + (player_entity, player_trans, player_pos) }; + mapcam.center_on_entity = Some(target_entity); // Get mouse movement let mut mouse_delta = Vec2::ZERO; @@ -242,7 +248,6 @@ pub fn update_map_camera( // Update point of view let pov_rotation = DQuat::from_euler(EulerRot::XYZ, 0.0, mapcam.yaw as f64, mapcam.pitch as f64); - let offset = DVec3::new(mapcam.offset_x, 0.0, mapcam.offset_z); let point_of_view = pov_rotation * (mapcam.zoom_level as f64 * DVec3::new(1.0, 0.0, 0.0)); // Update movement offset @@ -255,11 +260,26 @@ pub fn update_map_camera( mapcam.offset_z += 0.01 * (direction.z * mapcam.zoom_level); // Apply updates to camera - mapcam.center = **target_pos + offset; camera_transform.translation = point_of_view.as_vec3(); camera_transform.look_at(Vec3::ZERO, Vec3::Y); } +pub fn update_mapcam_center( + mut mapcam: ResMut, + settings: Res, + 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; + } + } +} + pub fn update_fov( q_player: Query<&actor::ExperiencesGForce, With>, mouse_input: Res>,