diff options
Diffstat (limited to 'stockton-contrib/src/flycam.rs')
-rw-r--r-- | stockton-contrib/src/flycam.rs | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/stockton-contrib/src/flycam.rs b/stockton-contrib/src/flycam.rs index 7b82c4f..093f75a 100644 --- a/stockton-contrib/src/flycam.rs +++ b/stockton-contrib/src/flycam.rs @@ -15,7 +15,9 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -use stockton_input::{Axis, InputManager}; +use std::f32::consts::PI; + +use stockton_input::{Axis, InputManager, Mouse}; use stockton_types::components::Transform; use stockton_types::Vector3; @@ -29,27 +31,38 @@ pub trait FlycamInput { pub struct FlycamControlled { pub speed: f32, + pub sensitivity: f32, +} + +impl FlycamControlled { + pub fn new(speed: f32, pixels_per_360: f32) -> Self { + FlycamControlled { + speed, + sensitivity: (2.0 * PI) / pixels_per_360, + } + } } #[system(for_each)] pub fn flycam_move<T>( #[resource] manager: &T, #[resource] timing: &Timing, + #[resource] mouse: &Mouse, transform: &mut Transform, flycam: &FlycamControlled, ) where T: 'static + InputManager, T::Inputs: FlycamInput, { - // TODO: Deal with looking around - let inputs = manager.get_inputs(); - let speed = flycam.speed; - let impulse = Vector3::new( - **inputs.get_x_axis() as f32 * speed * timing.delta_time, - **inputs.get_y_axis() as f32 * speed * timing.delta_time, - **inputs.get_z_axis() as f32 * speed * timing.delta_time, + let delta = Vector3::new( + **inputs.get_x_axis() as f32 * flycam.speed * timing.delta_time, + **inputs.get_y_axis() as f32 * flycam.speed * timing.delta_time, + **inputs.get_z_axis() as f32 * flycam.speed * timing.delta_time, ); - transform.position += impulse; + transform.translate(delta); + + let rotation = mouse.delta * flycam.sensitivity; + transform.rotate(Vector3::new(-rotation.y, rotation.x, 0.0)); } |