use bevy::prelude::*;
use benimator::FrameRate;
#[derive(Component, Deref)]
struct Animation(benimator::Animation);
#[derive(Default, Component, Deref, DerefMut)]
struct AnimationState(benimator::State);
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
.add_startup_system(spawn)
.add_system(animate)
.run();
}
fn spawn(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut textures: ResMut<Assets<TextureAtlas>>,
) {
commands.spawn(Camera2dBundle::default());
let animation = Animation(benimator::Animation::from_indices(
0..=4,
FrameRate::from_fps(12.0),
));
commands
.spawn(SpriteSheetBundle {
texture_atlas: textures.add(TextureAtlas::from_grid(
asset_server.load("coin.png"),
Vec2::new(16.0, 16.0),
5,
1,
None,
None,
)),
transform: Transform::from_scale(Vec3::splat(10.0)),
..Default::default()
})
.insert(animation)
.insert(AnimationState::default());
}
fn animate(
time: Res<Time>,
mut query: Query<(&mut AnimationState, &mut TextureAtlasSprite, &Animation)>,
) {
for (mut player, mut texture, animation) in query.iter_mut() {
player.update(animation, time.delta());
texture.index = player.frame_index();
}
}