1use std::time::Duration;
2
3use bevy::prelude::*;
4
5#[derive(Component)]
6pub struct SpriteFrameUpdate {
7 pub index: usize,
8 pub total: usize,
9 pub timer: Timer,
10}
11
12impl SpriteFrameUpdate {
13 pub fn next_index(&mut self, duration: Duration) -> usize {
14 self.timer.tick(duration);
15 if self.timer.just_finished() {
16 self.index += 1;
17 }
18 self.index % self.total
19 }
20}
21
22pub fn sprite_frame_update_system(
23 time: Res<Time>,
24 mut query: Query<(&mut TextureAtlas, &mut SpriteFrameUpdate)>,
25) {
26 for (mut atlas, mut frame) in query.iter_mut() {
27 atlas.index = frame.next_index(time.delta());
28 }
29}