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

Allow key bindings customization #11

Merged
merged 2 commits into from
Apr 3, 2023
Merged
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
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()
});
}
79 changes: 70 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ 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
/// A marker component used in queries when you want flycams and not other cameras
#[derive(Component)]
pub struct FlyCam;
Expand Down Expand Up @@ -70,6 +96,31 @@ 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();
for (_camera, mut transform) in query.iter_mut() {
let mut velocity = Vec3::ZERO;
let local_z = transform.local_z();
let forward = -Vec3::new(local_z.x, 0., local_z.z);
let right = Vec3::new(local_z.z, 0., -local_z.x);

for key in keys.get_pressed() {
if window.cursor_locked() {
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;
mut query: Query<&mut Transform, With<FlyCam>>,
) {
if let Some(window) = windows.get_primary() {
Expand Down Expand Up @@ -134,6 +185,14 @@ fn player_look(
}
}

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(key_bindings.toggle_grab_cursor) {
toggle_grab_cursor(window);
fn cursor_grab(keys: Res<Input<KeyCode>>, mut windows: ResMut<Windows>) {
if let Some(window) = windows.get_primary_mut() {
if keys.just_pressed(KeyCode::Escape) {
Expand All @@ -150,11 +209,12 @@ impl Plugin for PlayerPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<InputState>()
.init_resource::<MovementSettings>()
.add_startup_system(setup_player)
.add_startup_system(initial_grab_cursor)
.add_system(player_move)
.add_system(player_look)
.add_system(cursor_grab);
.init_resource::<KeyBindings>()
.add_startup_system(setup_player.system())
.add_startup_system(initial_grab_cursor.system())
.add_system(player_move.system())
.add_system(player_look.system())
.add_system(cursor_grab.system());
}
}

Expand All @@ -164,9 +224,10 @@ impl Plugin for NoCameraPlayerPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<InputState>()
.init_resource::<MovementSettings>()
.add_startup_system(initial_grab_cursor)
.add_system(player_move)
.add_system(player_look)
.add_system(cursor_grab);
.init_resource::<KeyBindings>()
.add_startup_system(initial_grab_cursor.system())
.add_system(player_move.system())
.add_system(player_look.system())
.add_system(cursor_grab.system());
}
}