use bevy::prelude::*;
use bevy_asset_loader::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.init_collection::<ImageAssets>()
.add_systems(Startup, draw)
.add_systems(
Update,
((load_audio, play_audio).chain(), animate_sprite_system),
)
.run();
}
fn load_audio(world: &mut World) {
let mouse_input = world.get_resource::<ButtonInput<MouseButton>>().unwrap();
if mouse_input.just_pressed(MouseButton::Left) {
world.init_collection::<AudioAssets>();
}
}
fn play_audio(audio_assets: Option<Res<AudioAssets>>, mut commands: Commands) {
if let Some(audio_assets) = audio_assets {
if audio_assets.is_added() {
commands.spawn(AudioPlayer(audio_assets.background.clone()));
}
}
}
#[derive(AssetCollection, Resource)]
struct ImageAssets {
#[asset(path = "images/female_adventurer_sheet.png")]
female_adventurer: Handle<Image>,
#[asset(texture_atlas_layout(tile_size_x = 96, tile_size_y = 99, columns = 8, rows = 1))]
female_adventurer_layout: Handle<TextureAtlasLayout>,
#[asset(path = "images/tree.png")]
tree: Handle<Image>,
}
#[derive(AssetCollection, Resource)]
struct AudioAssets {
#[asset(path = "audio/background.ogg")]
background: Handle<AudioSource>,
}
fn draw(mut commands: Commands, image_assets: Res<ImageAssets>) {
commands.spawn(Camera2d);
commands
.spawn((
Sprite::from_atlas_image(
image_assets.female_adventurer.clone(),
TextureAtlas::from(image_assets.female_adventurer_layout.clone()),
),
Transform::from_translation(Vec3::new(-150., 0., 1.)),
))
.insert(AnimationTimer(Timer::from_seconds(
0.1,
TimerMode::Repeating,
)));
commands.spawn((
Sprite::from_image(image_assets.tree.clone()),
Transform::from_translation(Vec3::new(150., 0., 1.)),
));
}
#[derive(Component)]
struct AnimationTimer(Timer);
fn animate_sprite_system(time: Res<Time>, mut query: Query<(&mut AnimationTimer, &mut Sprite)>) {
for (mut timer, mut sprite) in &mut query {
timer.0.tick(time.delta());
if timer.0.is_finished() {
if let Some(atlas) = &mut sprite.texture_atlas {
atlas.index = (atlas.index + 1) % 8;
}
}
}
}