Skip to content

Commit

Permalink
Allow key bindings customization
Browse files Browse the repository at this point in the history
  • Loading branch information
tesfabpel committed Nov 19, 2021
1 parent b90f6fc commit 1a4b378
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 10 deletions.
57 changes: 57 additions & 0 deletions examples/key_bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Remove the line below if you are copying this to your own project
extern crate bevy_flycam;

use bevy::prelude::*;
use bevy_flycam::PlayerPlugin;
use bevy_flycam::{KeyBindings, MovementSettings};

//From bevy examples:
//https://github.com/bevyengine/bevy/blob/latest/examples/3d/3d_scene.rs

fn main() {
App::build()
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
.add_plugin(PlayerPlugin)
.insert_resource(MovementSettings {
sensitivity: 0.00015, // default: 0.00012
speed: 12.0, // default: 12.0
})
.insert_resource(KeyBindings {
move_forward: KeyCode::A,
move_backward: KeyCode::Z,
move_left: KeyCode::Q,
move_right: KeyCode::W,
move_ascend: KeyCode::C,
move_descend: KeyCode::V,
toggle_grab_cursor: KeyCode::F,
})
.add_startup_system(setup.system())
.run();
}

/// set up a simple 3D scene
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// plane
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..Default::default()
});
// cube
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
});
// light
commands.spawn_bundle(LightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..Default::default()
});
}
57 changes: 47 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ impl Default for MovementSettings {
}
}

/// Key configuration
pub struct KeyBindings {
pub move_forward: KeyCode,
pub move_backward: KeyCode,
pub move_left: KeyCode,
pub move_right: KeyCode,
pub move_ascend: KeyCode,
pub move_descend: KeyCode,
pub toggle_grab_cursor: KeyCode,
}

impl Default for KeyBindings {
fn default() -> Self {
Self {
move_forward: KeyCode::W,
move_backward: KeyCode::S,
move_left: KeyCode::A,
move_right: KeyCode::D,
move_ascend: KeyCode::Space,
move_descend: KeyCode::LShift,
toggle_grab_cursor: KeyCode::Escape,
}
}
}

/// Used in queries when you want flycams and not other cameras
pub struct FlyCam;

Expand Down Expand Up @@ -55,6 +80,7 @@ fn player_move(
time: Res<Time>,
windows: Res<Windows>,
settings: Res<MovementSettings>,
key_bindings: Res<KeyBindings>,
mut query: Query<(&FlyCam, &mut Transform)>,
) {
let window = windows.get_primary().unwrap();
Expand All @@ -66,14 +92,19 @@ fn player_move(

for key in keys.get_pressed() {
if window.cursor_locked() {
match key {
KeyCode::W => velocity += forward,
KeyCode::S => velocity -= forward,
KeyCode::A => velocity -= right,
KeyCode::D => velocity += right,
KeyCode::Space => velocity += Vec3::Y,
KeyCode::LShift => velocity -= Vec3::Y,
_ => (),
let key = *key;
if key == key_bindings.move_forward {
velocity += forward;
} else if key == key_bindings.move_backward {
velocity -= forward;
} else if key == key_bindings.move_left {
velocity -= right;
} else if key == key_bindings.move_right {
velocity += right;
} else if key == key_bindings.move_ascend {
velocity += Vec3::Y;
} else if key == key_bindings.move_descend {
velocity -= Vec3::Y;
}
}
}
Expand Down Expand Up @@ -114,9 +145,13 @@ fn player_look(
}
}

fn cursor_grab(keys: Res<Input<KeyCode>>, mut windows: ResMut<Windows>) {
fn cursor_grab(
keys: Res<Input<KeyCode>>,
key_bindings: Res<KeyBindings>,
mut windows: ResMut<Windows>,
) {
let window = windows.get_primary_mut().unwrap();
if keys.just_pressed(KeyCode::Escape) {
if keys.just_pressed(key_bindings.toggle_grab_cursor) {
toggle_grab_cursor(window);
}
}
Expand All @@ -127,6 +162,7 @@ impl Plugin for PlayerPlugin {
fn build(&self, app: &mut AppBuilder) {
app.init_resource::<InputState>()
.init_resource::<MovementSettings>()
.init_resource::<KeyBindings>()
.add_startup_system(setup_player.system())
.add_startup_system(initial_grab_cursor.system())
.add_system(player_move.system())
Expand All @@ -141,6 +177,7 @@ impl Plugin for NoCameraPlayerPlugin {
fn build(&self, app: &mut AppBuilder) {
app.init_resource::<InputState>()
.init_resource::<MovementSettings>()
.init_resource::<KeyBindings>()
.add_startup_system(initial_grab_cursor.system())
.add_system(player_move.system())
.add_system(player_look.system())
Expand Down

0 comments on commit 1a4b378

Please sign in to comment.