bevy/examples/2d/sprite_sheet.rs
Marek Legris 474bb5403e
Transform Rewrite (#374)
Remove individual Translation / Rotation / Scale components in favor of a combined Transform component
2020-09-14 14:00:32 -07:00

46 lines
1.5 KiB
Rust

use bevy::prelude::*;
fn main() {
App::build()
.add_default_plugins()
.add_startup_system(setup.system())
.add_system(animate_sprite_system.system())
.run();
}
fn animate_sprite_system(
texture_atlases: Res<Assets<TextureAtlas>>,
mut query: Query<(&mut Timer, &mut TextureAtlasSprite, &Handle<TextureAtlas>)>,
) {
for (timer, mut sprite, texture_atlas_handle) in &mut query.iter() {
if timer.finished {
let texture_atlas = texture_atlases.get(&texture_atlas_handle).unwrap();
sprite.index = ((sprite.index as usize + 1) % texture_atlas.textures.len()) as u32;
}
}
}
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut textures: ResMut<Assets<Texture>>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
) {
let texture_handle = asset_server
.load_sync(
&mut textures,
"assets/textures/rpg/chars/gabe/gabe-idle-run.png",
)
.unwrap();
let texture = textures.get(&texture_handle).unwrap();
let texture_atlas = TextureAtlas::from_grid(texture_handle, texture.size, 7, 1);
let texture_atlas_handle = texture_atlases.add(texture_atlas);
commands
.spawn(Camera2dComponents::default())
.spawn(SpriteSheetComponents {
texture_atlas: texture_atlas_handle,
transform: Transform::from_scale(Vec3::one() * 6.0),
..Default::default()
})
.with(Timer::from_seconds(0.1, true));
}