|
|
|
|
@ -33,20 +33,24 @@ impl Default for CameraFreeLook { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn camera_free_look( |
|
|
|
|
window: Single<(&Window, &mut CursorOptions)>, |
|
|
|
|
accumulated_mouse_motion: Res<AccumulatedMouseMotion>, |
|
|
|
|
mouse_button_input: Res<ButtonInput<MouseButton>>, |
|
|
|
|
key_input: Res<ButtonInput<KeyCode>>, |
|
|
|
|
window: Single<(&Window, &mut CursorOptions)>, |
|
|
|
|
camera: Single<(&mut Transform, &mut CameraFreeLook)>, |
|
|
|
|
crosshair: Single<&mut Visibility, With<Crosshair>>, |
|
|
|
|
) { |
|
|
|
|
let (window, mut cursor) = window.into_inner(); |
|
|
|
|
let (mut transform, mut look) = camera.into_inner(); |
|
|
|
|
let mut crosshair_visibility = crosshair.into_inner(); |
|
|
|
|
|
|
|
|
|
if !window.focused || key_input.just_pressed(KeyCode::Escape) { |
|
|
|
|
*crosshair_visibility = Visibility::Hidden; |
|
|
|
|
cursor.grab_mode = CursorGrabMode::None; |
|
|
|
|
cursor.visible = true; |
|
|
|
|
} |
|
|
|
|
if mouse_button_input.any_just_pressed([MouseButton::Left, MouseButton::Right]) { |
|
|
|
|
*crosshair_visibility = Visibility::Inherited; |
|
|
|
|
cursor.grab_mode = CursorGrabMode::Locked; |
|
|
|
|
cursor.visible = false; |
|
|
|
|
} |
|
|
|
|
@ -139,3 +143,32 @@ pub fn noclip_controller( |
|
|
|
|
transform.translation += movement.z * dt * forward; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Component)] |
|
|
|
|
pub struct Crosshair; |
|
|
|
|
|
|
|
|
|
pub fn setup_crosshair(mut commands: Commands, asset_server: Res<AssetServer>) { |
|
|
|
|
commands.spawn(( |
|
|
|
|
Node { |
|
|
|
|
width: percent(100), |
|
|
|
|
height: percent(100), |
|
|
|
|
align_items: AlignItems::Center, |
|
|
|
|
justify_content: JustifyContent::Center, |
|
|
|
|
..default() |
|
|
|
|
}, |
|
|
|
|
children![( |
|
|
|
|
Crosshair, |
|
|
|
|
Node { |
|
|
|
|
width: px(64), |
|
|
|
|
height: px(64), |
|
|
|
|
..default() |
|
|
|
|
}, |
|
|
|
|
ImageNode { |
|
|
|
|
image: asset_server.load("crosshair.png"), |
|
|
|
|
..default() |
|
|
|
|
}, |
|
|
|
|
// Hidden by default, because cursor shouldn't be grabbed at startup either.
|
|
|
|
|
Visibility::Hidden, |
|
|
|
|
)], |
|
|
|
|
)); |
|
|
|
|
} |
|
|
|
|
|