simplify keyboard_input example
This commit is contained in:
parent
ed561d7f70
commit
aa2928739c
@ -6,74 +6,21 @@ use bevy::{
|
|||||||
fn main() {
|
fn main() {
|
||||||
App::build()
|
App::build()
|
||||||
.add_default_plugins()
|
.add_default_plugins()
|
||||||
.add_startup_system(setup.system())
|
.add_system(keyboard_input_system.system())
|
||||||
.add_system(move_on_input.system())
|
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This system moves our cube left when the "left" key is pressed and moves it right when the "right" key is pressed
|
/// This system prints 'A' key state
|
||||||
fn move_on_input(
|
fn keyboard_input_system(keyboard_input: Res<Input<KeyCode>>) {
|
||||||
world: &mut SubWorld,
|
if keyboard_input.pressed(KeyCode::A) {
|
||||||
time: Res<Time>,
|
println!("'A' currently pressed");
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if keyboard_input.just_released(KeyCode::Left) {
|
if keyboard_input.just_pressed(KeyCode::A) {
|
||||||
println!("left just released");
|
println!("'A' just pressed");
|
||||||
}
|
}
|
||||||
|
|
||||||
let speed = 3.0;
|
if keyboard_input.just_released(KeyCode::A) {
|
||||||
for (mut translation, _) in query.iter_mut(world) {
|
println!("'A' just released");
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user