use bevy::utils::HashSet; use bevy::{asset::LoadState, prelude::*}; use bevy_mod_aseprite::{ Aseprite, AsepriteAnimation, AsepriteBundle, AsepritePlugin, AsepriteSystems, AsepriteTag, }; pub mod sprites { use bevy_mod_aseprite::aseprite; aseprite!(pub Player, "player.ase"); } pub fn main() { App::new() .add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) .add_plugins(AsepritePlugin) .init_resource::>() .init_resource::() .init_state::() .add_systems(OnEnter(AppState::Loading), load_assets) .add_systems(Update, check_assets.run_if(in_state(AppState::Loading))) .add_systems(OnExit(AppState::Loading), setup) .add_systems( Update, ( keyboard_input, transition_player.before(AsepriteSystems::Animate), update_player, ) .run_if(in_state(AppState::Ready)), ) .run(); } #[derive(States, Debug, Clone, Default, PartialEq, Eq, Hash)] enum AppState { #[default] Loading, Ready, } #[derive(Resource, Deref, DerefMut, Default)] struct AsepriteHandles(Vec>); fn load_assets(mut aseprite_handles: ResMut, asset_server: Res) { let player: Handle = asset_server.load(sprites::Player::PATH); aseprite_handles.push(player); } fn check_assets( aseprite_handles: ResMut, asset_server: Res, mut state: ResMut>, ) { aseprite_handles .iter() .all(|handle| { matches!( asset_server.get_load_state(handle.id()), Some(LoadState::Loaded) ) }) .then(|| state.set(AppState::Ready)); } fn setup( mut commands: Commands, aseprite_handles: Res, aseprites: Res>, ) { commands.spawn(Camera2dBundle::default()); let aseprite_handle = &aseprite_handles[0]; let aseprite = aseprites.get(aseprite_handle).unwrap(); let animation = AsepriteAnimation::new(aseprite.info(), sprites::Player::tags::STAND); commands .spawn(Player) .insert(PlayerState::Stand) .insert(Orientation::Right) .insert(AsepriteBundle { texture: aseprite.texture().clone_weak(), atlas: TextureAtlas { index: animation.current_frame(), layout: aseprite.layout().clone_weak(), }, aseprite: aseprite_handle.clone_weak(), animation, transform: Transform { scale: Vec3::splat(2.0), ..default() }, ..default() }); } fn update_player( time: Res