oxygengine_composite_renderer/system/
tilemap_animation.rs

1use crate::component::{CompositeSurfaceCache, CompositeTilemap, CompositeTilemapAnimation};
2use core::{
3    app::AppLifeCycle,
4    ecs::{Comp, Universe, WorldRef},
5    Scalar,
6};
7
8pub type CompositeTilemapAnimationSystemResources<'a> = (
9    WorldRef,
10    &'a AppLifeCycle,
11    Comp<&'a mut CompositeTilemap>,
12    Comp<&'a mut CompositeTilemapAnimation>,
13    Comp<&'a mut CompositeSurfaceCache>,
14);
15
16pub fn composite_tilemap_animation_system(universe: &mut Universe) {
17    let (world, lifecycle, ..) =
18        universe.query_resources::<CompositeTilemapAnimationSystemResources>();
19
20    let dt = lifecycle.delta_time_seconds() as Scalar;
21    for (_, (tilemap, animation, cache)) in world
22        .query::<(
23            &mut CompositeTilemap,
24            &mut CompositeTilemapAnimation,
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                        tilemap.set_tileset(Some(anim.tileset.clone()));
35                        tilemap.set_grid(frame.clone());
36                        if let Some(cache) = cache {
37                            cache.rebuild();
38                        }
39                    }
40                }
41            }
42        }
43        animation.process(dt);
44    }
45}