WIP crisper camera controls (apply rotation)

This commit is contained in:
yuni 2024-11-16 19:55:42 +01:00
parent 8ea2d1fb21
commit e8e81e8e52
3 changed files with 78 additions and 1 deletions

View file

@ -38,6 +38,7 @@ impl Plugin for ActorPlugin {
update_physics_lifeforms.run_if(game_running),
update_power.run_if(game_running),
handle_gravity.run_if(game_running),
handle_wants_rotation.run_if(game_running),
handle_wants_maxrotation.run_if(game_running),
handle_wants_maxvelocity
.run_if(game_running)
@ -176,6 +177,8 @@ pub struct WantsMaxVelocity(pub f64);
#[derive(Component)]
pub struct WantsToLookAt(pub String);
#[derive(Component)]
pub struct WantsRotation(pub DQuat);
#[derive(Component)]
pub struct WantsMatchVelocityWith(pub String);
#[derive(Component)]
pub struct Identifier(pub String);
@ -703,6 +706,14 @@ fn handle_wants_maxvelocity(
}
}
fn handle_wants_rotation(
mut q_actor: Query<(&mut Rotation, &WantsRotation)>,
) {
for (mut rot, setrot) in &mut q_actor {
**rot = setrot.0;
}
}
fn handle_wants_lookat(
mut query: Query<
(

View file

@ -443,8 +443,73 @@ fn manage_player_actor(
}
}
#[allow(clippy::too_many_arguments)]
pub fn apply_input_to_player(
settings: Res<var::Settings>,
mut commands: Commands,
mut q_player: Query<
(Entity, &Rotation, Option<&mut actor::WantsRotation>),
With<actor::PlayerCamera>,
>,
mut mouse_events: EventReader<MouseMotion>,
key_input: Res<ButtonInput<KeyCode>>,
q_windows: Query<&Window, With<PrimaryWindow>>,
) {
let player = q_player.get_single_mut();
if player.is_err() {
return;
}
let (entity, rot, setrot) = player.unwrap();
let (win_res_x, win_res_y): (f32, f32);
if let Ok(window) = &q_windows.get_single() {
win_res_x = window.resolution.width();
win_res_y = window.resolution.height();
} else {
win_res_x = 1920.0;
win_res_y = 1050.0;
}
// Determine rotation delta
let mut pitch_yaw_rot = Vec3::ZERO;
let mut mouse_delta = Vec2::ZERO;
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] += 2.0 * mouse_delta.x / win_res_x;
} else {
pitch_yaw_rot[0] += 2.0 * mouse_delta.y / win_res_y;
pitch_yaw_rot[1] -= 2.0 * mouse_delta.x / win_res_x;
}
}
// Apply rotation to player
if pitch_yaw_rot != Vec3::ZERO {
let rot_offset = DQuat::from_euler(
EulerRot::XYZ,
pitch_yaw_rot[0] as f64,
pitch_yaw_rot[1] as f64,
pitch_yaw_rot[2] as f64,
);
let rot_current = if let Some(setrot) = &setrot {
setrot.0
} else {
**rot
};
let rot_target = rot_current * rot_offset;
if let Some(mut setrot) = setrot {
setrot.0 = rot_target;
} else {
commands
.entity(entity)
.try_insert(actor::WantsRotation(rot_target));
}
}
}
#[allow(clippy::too_many_arguments)]
pub fn apply_input_to_player_old(
time: Res<Time>,
mut commands: Commands,
settings: Res<var::Settings>,

View file

@ -1135,6 +1135,7 @@ fn spawn_entities(
actor.insert(actor::Player);
actor.insert(actor::PlayerCamera);
actor.insert(hud::AugmentedRealityOverlayBroadcaster);
//actor.insert(actor::WantsRotation(Quat::IDENTITY));
ew_updateavatar.send(hud::UpdateAvatarEvent);
}
if state.is_sun {