Sprite frame cycling

This commit is contained in:
Daniel Flanagan 2023-12-22 21:52:33 -06:00
parent d851eb7676
commit 27c58b569e
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4

View file

@ -36,14 +36,22 @@ fn main() {
} }
fn keyboard_input_system( fn keyboard_input_system(
mut query: Query<&mut TextureAtlasSprite, With<player::Player>>, mut query: Query<(&Handle<TextureAtlas>, &mut TextureAtlasSprite), With<player::Player>>,
keyboard_input: Res<Input<KeyCode>>, keyboard_input: Res<Input<KeyCode>>,
mut app_exit_events: ResMut<Events<bevy::app::AppExit>>, mut app_exit_events: ResMut<Events<bevy::app::AppExit>>,
texture_atlases: Res<Assets<TextureAtlas>>,
) { ) {
if keyboard_input.pressed(KeyCode::Escape) { if keyboard_input.pressed(KeyCode::Escape) {
app_exit_events.send(bevy::app::AppExit); app_exit_events.send(bevy::app::AppExit);
} else if keyboard_input.pressed(KeyCode::N) { } else if keyboard_input.just_pressed(KeyCode::N) {
let mut sprite = query.single_mut(); let (texture, mut sprite) = query.single_mut();
let t = texture_atlases
.get(texture)
.expect("could not load player texture");
if sprite.index < t.textures.len() - 1 {
sprite.index += 1; sprite.index += 1;
} else {
sprite.index = 0;
}
} }
} }