Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::time::Duration;
- use bevy::prelude::*;
- pub struct AnimatePlugin;
- impl Plugin for AnimatePlugin {
- fn build(&self, app: &mut App) {
- app
- .register_type::<AnimationTimer>()
- .add_system(animate_sprite);
- }
- }
- #[derive(Component, Deref,DerefMut,Reflect,Default)]
- #[reflect(Component)]
- pub struct AnimationTimer(pub Timer);
- pub struct AnimationFrame {
- pub atlas_handle: Handle<TextureAtlas>,
- pub atlas_index: usize,
- pub duration: Duration,
- }
- pub struct Animation {
- pub frames: Vec<AnimationFrame>,
- }
- #[derive(Component)]
- pub struct Animations {
- pub animations: Vec<Animation>,
- }
- #[derive(Default, Component)]
- pub struct Animator {
- pub current_animation: usize,
- pub last_animation: usize,
- pub current_frame: usize,
- pub timer: Timer,
- }
- fn animate_sprite(
- time: Res<Time>,
- texture_atlases: Res<Assets<TextureAtlas>>,
- mut query: Query<(
- &mut AnimationTimer,
- &mut TextureAtlasSprite,
- &Handle<TextureAtlas>,
- )>,
- ) {
- for (mut timer, mut sprite, texture_atlas_handle) in &mut query {
- timer.tick(time.delta());
- if timer.just_finished() {
- let texture_atlas = texture_atlases.get(texture_atlas_handle).unwrap();
- sprite.index = (sprite.index + 1) % texture_atlas.textures.len();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment