From 27c58b569efa318c798e3b676d5713b16350fcb2 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Fri, 22 Dec 2023 21:52:33 -0600 Subject: [PATCH] Sprite frame cycling --- src/main.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9dc64ad..ac2c242 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,14 +36,22 @@ fn main() { } fn keyboard_input_system( - mut query: Query<&mut TextureAtlasSprite, With>, + mut query: Query<(&Handle, &mut TextureAtlasSprite), With>, keyboard_input: Res>, mut app_exit_events: ResMut>, + texture_atlases: Res>, ) { if keyboard_input.pressed(KeyCode::Escape) { app_exit_events.send(bevy::app::AppExit); - } else if keyboard_input.pressed(KeyCode::N) { - let mut sprite = query.single_mut(); - sprite.index += 1; + } else if keyboard_input.just_pressed(KeyCode::N) { + 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; + } else { + sprite.index = 0; + } } }