bevy/examples/2d/sprite_sheet.rs

47 lines
1.4 KiB
Rust
Raw Normal View History

2020-06-04 02:53:41 +00:00
use bevy::prelude::*;
fn main() {
App::build()
.add_default_plugins()
.add_startup_system(setup.system())
2020-06-04 02:00:19 +00:00
.add_system(animate_sprite_system.system())
.run();
}
2020-06-04 02:00:19 +00:00
fn animate_sprite_system(
2020-06-06 07:12:38 +00:00
texture_atlases: Res<Assets<TextureAtlas>>,
2020-07-10 04:18:35 +00:00
mut query: Query<(&mut Timer, &mut TextureAtlasSprite, &Handle<TextureAtlas>)>,
2020-06-04 02:00:19 +00:00
) {
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;
}
2020-06-04 02:00:19 +00:00
}
}
fn setup(
2020-07-10 04:18:35 +00:00
mut commands: Commands,
asset_server: Res<AssetServer>,
2020-06-04 02:00:19 +00:00
mut textures: ResMut<Assets<Texture>>,
2020-06-06 07:12:38 +00:00
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
) {
2020-06-04 02:00:19 +00:00
let texture_handle = asset_server
2020-06-15 19:47:35 +00:00
.load_sync(
&mut textures,
"assets/textures/rpg/chars/gabe/gabe-idle-run.png",
)
2020-06-04 02:00:19 +00:00
.unwrap();
let texture_atlas = TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1);
2020-06-06 07:12:38 +00:00
let texture_atlas_handle = texture_atlases.add(texture_atlas);
2020-07-10 04:18:35 +00:00
commands
.spawn(Camera2dComponents::default())
2020-07-10 04:18:35 +00:00
.spawn(SpriteSheetComponents {
2020-06-06 07:12:38 +00:00
texture_atlas: texture_atlas_handle,
transform: Transform::from_scale(Vec3::splat(6.0)),
..Default::default()
2020-06-04 02:53:41 +00:00
})
.with(Timer::from_seconds(0.1, true));
}