Files
voxel-game/src/client/handle_input.rs
2024-06-05 00:31:14 -04:00

96 lines
3.5 KiB
Rust

use std::{f32::consts::PI, time::Duration};
use nalgebra::Rotation3;
use winit::{dpi::LogicalPosition, keyboard::KeyCode as Key, window::CursorGrabMode};
use super::Client;
impl Client<'_> {
pub fn handle_input(&mut self, dt: &Duration) {
let dt = dt.as_secs_f32();
let Client { input, state, .. } = self;
if input.just_pressed(Key::Escape) {
if let Some(window) = &self.window {
self.grabbed_cursor = !self.grabbed_cursor;
let mode = if self.grabbed_cursor {
window.set_cursor_visible(false);
CursorGrabMode::Locked
} else {
window.set_cursor_visible(true);
CursorGrabMode::None
};
#[cfg(not(target_os = "windows"))]
window.set_cursor_grab(mode).expect("wah");
}
return;
}
if self.grabbed_cursor {
if let Some(window) = &self.window {
let size = window.inner_size();
window
.set_cursor_position(LogicalPosition::new(size.width / 2, size.height / 2))
.expect("wah");
}
let delta = input.mouse_delta;
if delta.x != 0.0 {
state.camera.orientation = Rotation3::from_axis_angle(
&state.camera.up(),
(delta.x * 0.003).clamp(-PI, PI),
) * state.camera.orientation;
}
if delta.y != 0.0 {
state.camera.orientation = Rotation3::from_axis_angle(
&state.camera.right(),
(delta.y * 0.003).clamp(-PI, PI),
) * state.camera.orientation;
}
if delta.x != 0.0 || delta.y != 0.0 {
state.camera.orientation.renormalize();
}
}
let rot_dist = 1.0 * dt;
if input.pressed(Key::KeyQ) {
state.camera.orientation =
Rotation3::from_axis_angle(&state.camera.forward(), rot_dist)
* state.camera.orientation;
}
if input.pressed(Key::KeyE) {
state.camera.orientation =
Rotation3::from_axis_angle(&state.camera.forward(), -rot_dist)
* state.camera.orientation;
}
if input.scroll_delta != 0.0 {
state.camera_scroll += input.scroll_delta;
state.camera.scale = (state.camera_scroll * 0.2).exp();
}
let move_dist = 10.0 * dt;
if input.pressed(Key::KeyW) {
state.camera.pos += *state.camera.forward() * move_dist;
}
if input.pressed(Key::KeyA) {
state.camera.pos += *state.camera.left() * move_dist;
}
if input.pressed(Key::KeyS) {
state.camera.pos += *state.camera.backward() * move_dist;
}
if input.pressed(Key::KeyD) {
state.camera.pos += *state.camera.right() * move_dist;
}
if input.pressed(Key::Space) {
state.camera.pos += *state.camera.up() * move_dist;
}
if input.pressed(Key::ShiftLeft) {
state.camera.pos += *state.camera.down() * move_dist;
}
if input.pressed(Key::KeyZ) {
state.camera_scroll += dt * 10.0;
state.camera.scale = (state.camera_scroll * 0.1).exp();
}
if input.pressed(Key::KeyX) {
state.camera_scroll -= dt * 10.0;
state.camera.scale = (state.camera_scroll * 0.1).exp();
}
}
}