2020-06-04 02:53:41 +00:00
|
|
|
use bevy::prelude::*;
|
2020-06-02 02:23:11 +00:00
|
|
|
|
|
|
|
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())
|
2020-06-02 02:23:11 +00:00
|
|
|
.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
|
|
|
) {
|
2020-08-21 21:57:25 +00:00
|
|
|
for (timer, mut sprite, texture_atlas_handle) in &mut query.iter() {
|
2020-06-27 19:06:12 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 02:23:11 +00:00
|
|
|
fn setup(
|
2020-07-10 04:18:35 +00:00
|
|
|
mut commands: Commands,
|
2020-06-02 02:23:11 +00:00
|
|
|
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-02 02:23:11 +00:00
|
|
|
) {
|
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();
|
2020-10-15 03:49:07 +00:00
|
|
|
|
|
|
|
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
|
2020-07-20 00:00:08 +00:00
|
|
|
.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,
|
2020-10-18 20:03:16 +00:00
|
|
|
transform: Transform::from_scale(Vec3::splat(6.0)),
|
2020-06-02 02:23:11 +00:00
|
|
|
..Default::default()
|
2020-06-04 02:53:41 +00:00
|
|
|
})
|
2020-08-21 21:57:25 +00:00
|
|
|
.with(Timer::from_seconds(0.1, true));
|
2020-06-02 02:23:11 +00:00
|
|
|
}
|