 b91945b54d
			
		
	
	
		b91945b54d
		
	
	
	
	
		
			
			This is an adoption of #3775 This merges `TextureAtlas` `from_grid_with_padding` into `from_grid` , adding optional padding and optional offset. Since the orignal PR, the offset had already been added to from_grid_with_padding through #4836 ## Changelog - Added `padding` and `offset` arguments to `TextureAtlas::from_grid` - Removed `TextureAtlas::from_grid_with_padding` ## Migration Guide `TextureAtlas::from_grid_with_padding` was merged into `from_grid` which takes two additional parameters for padding and an offset. ``` // 0.8 TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1); // 0.9 TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1, None, None) // 0.8 TextureAtlas::from_grid_with_padding(texture_handle, Vec2::new(24.0, 24.0), 7, 1, Vec2::new(4.0, 4.0)); // 0.9 TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1, Some(Vec2::new(4.0, 4.0)), None) ``` Co-authored-by: olefish <88390729+oledfish@users.noreply.github.com>
		
			
				
	
	
		
			55 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			55 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| //! Renders an animated sprite by loading all animation frames from a single image (a sprite sheet)
 | |
| //! into a texture atlas, and changing the displayed image periodically.
 | |
| 
 | |
| use bevy::prelude::*;
 | |
| 
 | |
| fn main() {
 | |
|     App::new()
 | |
|         .insert_resource(ImageSettings::default_nearest()) // prevents blurry sprites
 | |
|         .add_plugins(DefaultPlugins)
 | |
|         .add_startup_system(setup)
 | |
|         .add_system(animate_sprite)
 | |
|         .run();
 | |
| }
 | |
| 
 | |
| #[derive(Component, Deref, DerefMut)]
 | |
| struct AnimationTimer(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();
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| fn setup(
 | |
|     mut commands: Commands,
 | |
|     asset_server: Res<AssetServer>,
 | |
|     mut texture_atlases: ResMut<Assets<TextureAtlas>>,
 | |
| ) {
 | |
|     let texture_handle = asset_server.load("textures/rpg/chars/gabe/gabe-idle-run.png");
 | |
|     let texture_atlas =
 | |
|         TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1, None, None);
 | |
|     let texture_atlas_handle = texture_atlases.add(texture_atlas);
 | |
|     commands.spawn(Camera2dBundle::default());
 | |
|     commands.spawn((
 | |
|         SpriteSheetBundle {
 | |
|             texture_atlas: texture_atlas_handle,
 | |
|             transform: Transform::from_scale(Vec3::splat(6.0)),
 | |
|             ..default()
 | |
|         },
 | |
|         AnimationTimer(Timer::from_seconds(0.1, true)),
 | |
|     ));
 | |
| }
 |