bevy/examples/2d/sprite_sheet.rs

49 lines
1.5 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-06-04 02:53:41 +00:00
mut timer: ComMut<Timer>,
2020-06-06 07:12:38 +00:00
mut sprite: ComMut<TextureAtlasSprite>,
texture_atlas_handle: Com<Handle<TextureAtlas>>,
2020-06-04 02:00:19 +00:00
) {
2020-06-04 02:53:41 +00:00
if timer.finished {
2020-06-06 07:12:38 +00:00
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:53:41 +00:00
timer.reset();
2020-06-04 02:00:19 +00:00
}
}
fn setup(
command_buffer: &mut CommandBuffer,
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 = textures.get(&texture_handle).unwrap();
2020-06-06 07:12:38 +00:00
let texture_atlas = TextureAtlas::from_grid(texture_handle, texture.size, 7, 1);
let texture_atlas_handle = texture_atlases.add(texture_atlas);
command_buffer
.build()
.entity_with(OrthographicCameraComponents::default())
.entity_with(SpriteSheetComponents {
2020-06-06 07:12:38 +00:00
texture_atlas: texture_atlas_handle,
scale: Scale(6.0),
..Default::default()
2020-06-04 02:53:41 +00:00
})
.with(Timer::from_seconds(0.1));
}