Add basic player movement system

This commit is contained in:
Daniel Lynn 2021-07-17 00:20:54 -05:00
parent 66854be921
commit 9e16c22eb5
Signed by: daniel
GPG Key ID: 28496A140E180A9D
2 changed files with 30 additions and 0 deletions

View File

@ -19,5 +19,6 @@ fn main() {
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
.add_startup_system(add_player.system())
.add_system(player_movement.system())
.run();
}

View File

@ -50,6 +50,35 @@ pub fn add_player(
});
}
pub fn player_movement(
keyboard_input: Res<Input<KeyCode>>,
mut query: Query<(&Player, &mut Transform)>,
time: Res<Time>,
) {
if let Ok((_, mut transform)) = query.single_mut() {
let mut direction = Vec2::ZERO;
if keyboard_input.pressed(KeyCode::A) {
direction.x -= 1.0;
}
if keyboard_input.pressed(KeyCode::D) {
direction.x += 1.0;
}
if keyboard_input.pressed(KeyCode::S) {
direction.y -= 1.0;
}
if keyboard_input.pressed(KeyCode::W) {
direction.y += 1.0;
}
let translation = &mut transform.translation;
let delta_seconds = time.delta_seconds();
translation.x += delta_seconds * direction.x * 100.0;
translation.y += delta_seconds * direction.y * 100.0;
}
}
pub fn greet_player(
query: Query<&Name, With<Player>>,
time: Res<Time>,