Player animation
This commit is contained in:
parent
27c58b569e
commit
0deaffcd32
|
@ -34,24 +34,35 @@ fn spawn_player(mut commands: Commands, assets: Res<AssetLoader>) {
|
|||
));
|
||||
}
|
||||
|
||||
fn player_movement(mut query: Query<&mut Heading, With<Player>>, input: Res<Input<KeyCode>>) {
|
||||
let mut heading = query.single_mut();
|
||||
fn player_movement(
|
||||
mut query: Query<(&mut TextureAtlasSprite, &mut Heading), With<Player>>,
|
||||
input: Res<Input<KeyCode>>,
|
||||
) {
|
||||
let (mut sprite, mut heading) = query.single_mut();
|
||||
**heading = Vec2::ZERO;
|
||||
for key in input.get_pressed() {
|
||||
match key {
|
||||
KeyCode::W | KeyCode::Up => {
|
||||
**heading += Vec2::Y;
|
||||
}
|
||||
KeyCode::S | KeyCode::Down => {
|
||||
**heading -= Vec2::Y;
|
||||
}
|
||||
KeyCode::A | KeyCode::Left => {
|
||||
**heading -= Vec2::X;
|
||||
}
|
||||
KeyCode::D | KeyCode::Right => {
|
||||
**heading += Vec2::X;
|
||||
}
|
||||
KeyCode::W | KeyCode::Up => {
|
||||
**heading += Vec2::Y;
|
||||
}
|
||||
KeyCode::S | KeyCode::Down => {
|
||||
**heading -= Vec2::Y;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if heading.y < 0. {
|
||||
sprite.index = 0;
|
||||
} else if heading.y > 0. {
|
||||
sprite.index = 1;
|
||||
} else if heading.x != 0. {
|
||||
sprite.index = 2;
|
||||
sprite.flip_x = heading.x > 0.;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue