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-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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 02:23:11 +00:00
|
|
|
fn setup(
|
|
|
|
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-27 17:18:27 +00:00
|
|
|
command_buffer: &mut CommandBuffer,
|
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();
|
|
|
|
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);
|
2020-06-02 02:23:11 +00:00
|
|
|
command_buffer
|
|
|
|
.build()
|
2020-06-25 18:21:56 +00:00
|
|
|
.entity_with(OrthographicCameraComponents::default())
|
|
|
|
.entity_with(SpriteSheetComponents {
|
2020-06-06 07:12:38 +00:00
|
|
|
texture_atlas: texture_atlas_handle,
|
2020-06-22 19:35:33 +00:00
|
|
|
scale: Scale(6.0),
|
2020-06-02 02:23:11 +00:00
|
|
|
..Default::default()
|
2020-06-04 02:53:41 +00:00
|
|
|
})
|
2020-06-25 18:21:56 +00:00
|
|
|
.with(Timer::from_seconds(0.1));
|
2020-06-02 02:23:11 +00:00
|
|
|
}
|