oxygengine_composite_renderer/system/
sprite_animation.rs

1use crate::component::{CompositeSprite, CompositeSpriteAnimation, CompositeSurfaceCache};
2use core::{
3    app::AppLifeCycle,
4    ecs::{Comp, Universe, WorldRef},
5    Scalar,
6};
7
8pub type CompositeSpriteAnimationSystemResources<'a> = (
9    WorldRef,
10    &'a AppLifeCycle,
11    Comp<&'a mut CompositeSprite>,
12    Comp<&'a mut CompositeSpriteAnimation>,
13    Comp<&'a mut CompositeSurfaceCache>,
14);
15
16pub fn composite_sprite_animation_system(universe: &mut Universe) {
17    let (world, lifecycle, ..) =
18        universe.query_resources::<CompositeSpriteAnimationSystemResources>();
19
20    let dt = lifecycle.delta_time_seconds() as Scalar;
21    for (_, (sprite, animation, cache)) in world
22        .query::<(
23            &mut CompositeSprite,
24            &mut CompositeSpriteAnimation,
25            Option<&mut CompositeSurfaceCache>,
26        )>()
27        .iter()
28    {
29        if animation.dirty {
30            animation.dirty = false;
31            if let Some((name, phase, _, _)) = &animation.current {
32                if let Some(anim) = animation.animations.get(name) {
33                    if let Some(frame) = anim.frames.get(*phase as usize) {
34                        sprite.set_sheet_frame(Some((anim.sheet.clone(), frame.clone())));
35                        if let Some(cache) = cache {
36                            cache.rebuild();
37                        }
38                    }
39                }
40            }
41        }
42        animation.process(dt);
43    }
44}