update mapcam.center fast enough to avoid flicker on moving target

This commit is contained in:
yuni 2024-05-07 22:04:47 +02:00
parent 875e27ccf1
commit 60309f84a0

View file

@ -40,6 +40,9 @@ impl Plugin for CameraPlugin {
.after(PhysicsSet::Sync) .after(PhysicsSet::Sync)
.after(apply_input_to_player) .after(apply_input_to_player)
.before(TransformSystem::TransformPropagate)); .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_map_camera);
app.add_systems(Update, update_fov); app.add_systems(Update, update_fov);
app.add_systems(PreUpdate, apply_input_to_player); app.add_systems(PreUpdate, apply_input_to_player);
@ -74,6 +77,7 @@ pub struct MapCam {
pub offset_x: f64, pub offset_x: f64,
pub offset_z: f64, pub offset_z: f64,
pub center: DVec3, pub center: DVec3,
pub center_on_entity: Option<Entity>,
} }
impl Default for MapCam { impl Default for MapCam {
fn default() -> Self { fn default() -> Self {
@ -86,6 +90,7 @@ impl Default for MapCam {
offset_x: 0.0, offset_x: 0.0,
offset_z: 0.0, offset_z: 0.0,
center: DVec3::new(0.0, 0.0, 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<var::Settings>, settings: Res<var::Settings>,
mut mapcam: ResMut<MapCam>, mut mapcam: ResMut<MapCam>,
mut q_camera: Query<&mut Transform, (With<Camera>, Without<actor::PlayerCamera>)>, mut q_camera: Query<&mut Transform, (With<Camera>, Without<actor::PlayerCamera>)>,
q_playercam: Query<(&Transform, &Position), (With<actor::PlayerCamera>, Without<Camera>)>, q_playercam: Query<(Entity, &Transform, &Position), (With<actor::PlayerCamera>, Without<Camera>)>,
q_target: Query<(&Transform, &Position), (With<hud::IsTargeted>, Without<Camera>, Without<actor::PlayerCamera>)>, q_target: Query<(Entity, &Transform, &Position), (With<hud::IsTargeted>, Without<Camera>, Without<actor::PlayerCamera>)>,
q_target_changed: Query<(), Changed<hud::IsTargeted>>, q_target_changed: Query<(), Changed<hud::IsTargeted>>,
mut mouse_events: EventReader<MouseMotion>, mut mouse_events: EventReader<MouseMotion>,
mut er_mousewheel: EventReader<MouseWheel>, mut er_mousewheel: EventReader<MouseWheel>,
@ -172,12 +177,13 @@ pub fn update_map_camera(
return; return;
} }
let mut camera_transform = q_camera.get_single_mut().unwrap(); let mut camera_transform = q_camera.get_single_mut().unwrap();
let (player_trans, player_pos) = q_playercam.get_single().unwrap(); let (player_entity, player_trans, player_pos) = q_playercam.get_single().unwrap();
let (target_trans, target_pos) = if let Ok(target) = q_target.get_single() { let (target_entity, target_trans, target_pos) = if let Ok(target) = q_target.get_single() {
target target
} else { } else {
(player_trans, player_pos) (player_entity, player_trans, player_pos)
}; };
mapcam.center_on_entity = Some(target_entity);
// Get mouse movement // Get mouse movement
let mut mouse_delta = Vec2::ZERO; let mut mouse_delta = Vec2::ZERO;
@ -242,7 +248,6 @@ pub fn update_map_camera(
// Update point of view // Update point of view
let pov_rotation = DQuat::from_euler(EulerRot::XYZ, 0.0, mapcam.yaw as f64, mapcam.pitch as f64); 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)); let point_of_view = pov_rotation * (mapcam.zoom_level as f64 * DVec3::new(1.0, 0.0, 0.0));
// Update movement offset // Update movement offset
@ -255,11 +260,26 @@ pub fn update_map_camera(
mapcam.offset_z += 0.01 * (direction.z * mapcam.zoom_level); mapcam.offset_z += 0.01 * (direction.z * mapcam.zoom_level);
// Apply updates to camera // Apply updates to camera
mapcam.center = **target_pos + offset;
camera_transform.translation = point_of_view.as_vec3(); camera_transform.translation = point_of_view.as_vec3();
camera_transform.look_at(Vec3::ZERO, Vec3::Y); camera_transform.look_at(Vec3::ZERO, Vec3::Y);
} }
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;
}
}
}
pub fn update_fov( pub fn update_fov(
q_player: Query<&actor::ExperiencesGForce, With<actor::Player>>, q_player: Query<&actor::ExperiencesGForce, With<actor::Player>>,
mouse_input: Res<ButtonInput<MouseButton>>, mouse_input: Res<ButtonInput<MouseButton>>,