From 1a4b378da96810725db0da3c6b3f341f2c8f65a9 Mon Sep 17 00:00:00 2001 From: Fabrizio Pelosi Date: Wed, 10 Nov 2021 21:58:30 +0100 Subject: [PATCH 1/4] Allow key bindings customization --- examples/key_bindings.rs | 57 ++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 57 +++++++++++++++++++++++++++++++++------- 2 files changed, 104 insertions(+), 10 deletions(-) create mode 100644 examples/key_bindings.rs diff --git a/examples/key_bindings.rs b/examples/key_bindings.rs new file mode 100644 index 0000000..6e28a17 --- /dev/null +++ b/examples/key_bindings.rs @@ -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>, + mut materials: ResMut>, +) { + // 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() + }); +} diff --git a/src/lib.rs b/src/lib.rs index 45b44c3..5501492 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -55,6 +80,7 @@ fn player_move( time: Res