Skip to content

Commit

Permalink
simplify keyboard_input example
Browse files Browse the repository at this point in the history
  • Loading branch information
cart committed Jun 5, 2020
1 parent ed561d7 commit aa29287
Showing 1 changed file with 9 additions and 62 deletions.
71 changes: 9 additions & 62 deletions examples/input/keyboard_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,21 @@ use bevy::{
fn main() {
App::build()
.add_default_plugins()
.add_startup_system(setup.system())
.add_system(move_on_input.system())
.add_system(keyboard_input_system.system())
.run();
}

/// This system moves our cube left when the "left" key is pressed and moves it right when the "right" key is pressed
fn move_on_input(
world: &mut SubWorld,
time: Res<Time>,
keyboard_input: Res<Input<KeyCode>>,
query: &mut Query<(Write<Translation>, Read<Handle<Mesh>>)>,
) {
let moving_left = keyboard_input.pressed(KeyCode::Left);
let moving_right = keyboard_input.pressed(KeyCode::Right);

if keyboard_input.just_pressed(KeyCode::Left) {
println!("left just pressed");
/// This system prints 'A' key state
fn keyboard_input_system(keyboard_input: Res<Input<KeyCode>>) {
if keyboard_input.pressed(KeyCode::A) {
println!("'A' currently pressed");
}

if keyboard_input.just_released(KeyCode::Left) {
println!("left just released");
if keyboard_input.just_pressed(KeyCode::A) {
println!("'A' just pressed");
}

let speed = 3.0;
for (mut translation, _) in query.iter_mut(world) {
if moving_left {
translation.0 += math::vec3(speed, 0.0, 0.0) * time.delta_seconds;
}

if moving_right {
translation.0 += math::vec3(-speed, 0.0, 0.0) * time.delta_seconds;
}
if keyboard_input.just_released(KeyCode::A) {
println!("'A' just released");
}
}

/// Creates a simple scene containing the cube we will be controlling
fn setup(
command_buffer: &mut CommandBuffer,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 1.0 }));
let cube_material_handle = materials.add(StandardMaterial {
albedo: Color::rgb(0.5, 0.4, 0.3),
..Default::default()
});

command_buffer
.build()
// cube
.add_entity(MeshEntity {
mesh: cube_handle,
material: cube_material_handle,
translation: Translation::new(0.0, 0.0, 1.0),
..Default::default()
})
// light
.add_entity(LightEntity {
translation: Translation::new(4.0, -4.0, 5.0),
..Default::default()
})
// camera
.add_entity(PerspectiveCameraEntity {
local_to_world: LocalToWorld::new_sync_disabled(Mat4::look_at_rh(
Vec3::new(0.0, 8.0, 5.0),
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0),
)),
..Default::default()
});
}

0 comments on commit aa29287

Please sign in to comment.