Guest User

Untitled

a guest
Jan 24th, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.39 KB | Source Code | 0 0
  1. use std::time::Duration;
  2.  
  3. use bevy::prelude::*;
  4.  
  5. pub struct AnimatePlugin;
  6.  
  7.  
  8. impl Plugin for AnimatePlugin {
  9.     fn build(&self, app: &mut App) {
  10.         app
  11.             .register_type::<AnimationTimer>()
  12.             .add_system(animate_sprite);
  13.     }
  14. }
  15.  
  16.  
  17. #[derive(Component, Deref,DerefMut,Reflect,Default)]
  18. #[reflect(Component)]
  19. pub struct AnimationTimer(pub Timer);
  20.  
  21. pub struct AnimationFrame {
  22.     pub atlas_handle: Handle<TextureAtlas>,
  23.     pub atlas_index: usize,
  24.     pub duration: Duration,
  25. }
  26.  
  27. pub struct Animation {
  28.     pub frames: Vec<AnimationFrame>,
  29. }
  30.  
  31. #[derive(Component)]
  32. pub struct Animations {
  33.     pub animations: Vec<Animation>,
  34. }
  35.  
  36. #[derive(Default, Component)]
  37. pub struct Animator {
  38.     pub current_animation: usize,
  39.     pub last_animation: usize,
  40.     pub current_frame: usize,
  41.     pub timer: Timer,
  42. }
  43.  
  44.  
  45. fn animate_sprite(
  46.     time: Res<Time>,
  47.     texture_atlases: Res<Assets<TextureAtlas>>,
  48.     mut query: Query<(
  49.         &mut AnimationTimer,
  50.         &mut TextureAtlasSprite,
  51.         &Handle<TextureAtlas>,
  52.     )>,
  53. ) {
  54.     for (mut timer, mut sprite, texture_atlas_handle) in &mut query {
  55.         timer.tick(time.delta());
  56.         if timer.just_finished() {
  57.             let texture_atlas = texture_atlases.get(texture_atlas_handle).unwrap();
  58.             sprite.index = (sprite.index + 1) % texture_atlas.textures.len();
  59.         }
  60.     }
  61. }
  62.  
  63.  
  64.  
Advertisement
Add Comment
Please, Sign In to add comment