Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support enabling/disabling mouse/keyboard movement #3

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy = "0.1.3"
bevy = "0.2.0"
83 changes: 68 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
//!
//! There's also a basic piece of example code included in `/examples/basic.rs`
use bevy::{
input::mouse::MouseMotion,
input::mouse::{MouseButton, MouseMotion},
prelude::*,
};

Expand Down Expand Up @@ -69,6 +69,12 @@ pub struct FlyCamera {
pub key_up: KeyCode,
/// Key used to move forward. Defaults to `LShift`
pub key_down: KeyCode,
/// Whether activate movement is a toggle or has to be held down. Defaults to `true`
pub activate_is_toggle: bool,
/// Mouse button used to activate movement. Defaults to `None`
pub mouse_button_activate: Option<MouseButton>,
/// Key used to activate movement. Defaults to `None`
pub key_activate: Option<KeyCode>,
}
impl Default for FlyCamera {
fn default() -> Self {
Expand All @@ -86,23 +92,52 @@ impl Default for FlyCamera {
key_right: KeyCode::D,
key_up: KeyCode::Space,
key_down: KeyCode::LShift,
activate_is_toggle: true,
mouse_button_activate: None,
key_activate: None,
}
}
}

fn forward_vector(rotation: &Rotation) -> Vec3 {
fn activate_motion_system(
mut state: ResMut<State>,
mouse_button_input: Res<Input<MouseButton>>,
keyboard_input: Res<Input<KeyCode>>,
fly_camera: &FlyCamera,
) {
if let Some(key_activate) = fly_camera.key_activate {
if fly_camera.activate_is_toggle {
if keyboard_input.just_pressed(key_activate) {
state.activated = !state.activated;
}
} else {
state.activated = keyboard_input.pressed(key_activate);
}
}
if let Some(mouse_button_activate) = fly_camera.mouse_button_activate {
if fly_camera.activate_is_toggle {
if mouse_button_input.just_pressed(mouse_button_activate) {
state.activated = !state.activated;
}
} else {
state.activated = mouse_button_input.pressed(mouse_button_activate);
}
}
}

fn forward_vector(rotation: &Quat) -> Vec3 {
rotation.mul_vec3(Vec3::unit_z()).normalize()
}

fn forward_walk_vector(rotation: &Rotation) -> Vec3 {
fn forward_walk_vector(rotation: &Quat) -> Vec3 {
let f = forward_vector(rotation);
let f_flattened = Vec3::new(f.x(), 0.0, f.z()).normalize();
f_flattened
}

fn strafe_vector(rotation: &Rotation) -> Vec3 {
fn strafe_vector(rotation: &Quat) -> Vec3 {
// Rotate it 90 degrees to get the strafe direction
Rotation::from_rotation_y(90.0f32.to_radians())
Quat::from_rotation_y(90.0f32.to_radians())
.mul_vec3(forward_walk_vector(rotation))
.normalize()
}
Expand All @@ -124,10 +159,14 @@ fn movement_axis(

fn camera_movement_system(
time: Res<Time>,
state: Res<State>,
keyboard_input: Res<Input<KeyCode>>,
mut query: Query<(&mut FlyCamera, &mut Translation, &Rotation)>,
mut query: Query<(&mut FlyCamera, &mut Transform)>,
) {
for (mut options, mut translation, rotation) in &mut query.iter() {
if !state.activated {
return;
}
for (mut options, mut transform) in &mut query.iter() {
let axis_h =
movement_axis(&keyboard_input, options.key_right, options.key_left);
let axis_v =
Expand All @@ -138,8 +177,9 @@ fn camera_movement_system(

let any_button_down = axis_h != 0.0 || axis_v != 0.0 || axis_float != 0.0;

let accel: Vec3 = ((strafe_vector(rotation) * axis_h)
+ (forward_walk_vector(rotation) * axis_v)
let rotation = transform.rotation();
let accel: Vec3 = ((strafe_vector(&rotation) * axis_h)
+ (forward_walk_vector(&rotation) * axis_v)
+ (Vec3::unit_y() * axis_float))
* options.speed;

Expand Down Expand Up @@ -167,21 +207,33 @@ fn camera_movement_system(
options.velocity + delta_friction
};

translation.0 += options.velocity;
transform.translate(options.velocity);
}
}

#[derive(Default)]
struct State {
mouse_motion_event_reader: EventReader<MouseMotion>,
activated: bool,
}

impl Default for State {
fn default() -> Self {
State {
mouse_motion_event_reader: EventReader::default(),
activated: true,
}
}
}

fn mouse_motion_system(
time: Res<Time>,
mut state: ResMut<State>,
mouse_motion_events: Res<Events<MouseMotion>>,
mut query: Query<(&mut FlyCamera, &mut Rotation)>,
mut query: Query<(&mut FlyCamera, &mut Transform)>,
) {
if !state.activated {
return;
}
let mut delta: Vec2 = Vec2::zero();
for event in state.mouse_motion_event_reader.iter(&mouse_motion_events) {
delta += event.delta;
Expand All @@ -190,7 +242,7 @@ fn mouse_motion_system(
return;
}

for (mut options, mut rotation) in &mut query.iter() {
for (mut options, mut transform) in &mut query.iter() {
options.yaw -= delta.x() * options.sensitivity * time.delta_seconds;
options.pitch += delta.y() * options.sensitivity * time.delta_seconds;

Expand All @@ -205,8 +257,8 @@ fn mouse_motion_system(
let yaw_radians = options.yaw.to_radians();
let pitch_radians = options.pitch.to_radians();

rotation.0 = Quat::from_axis_angle(Vec3::unit_y(), yaw_radians)
* Quat::from_axis_angle(-Vec3::unit_x(), pitch_radians);
transform.set_rotation(Quat::from_axis_angle(Vec3::unit_y(), yaw_radians)
* Quat::from_axis_angle(-Vec3::unit_x(), pitch_radians));
}
}

Expand All @@ -227,6 +279,7 @@ impl Plugin for FlyCameraPlugin {
fn build(&self, app: &mut AppBuilder) {
app
.init_resource::<State>()
.add_system(activate_motion_system.system())
.add_system(camera_movement_system.system())
.add_system(mouse_motion_system.system());
}
Expand Down