use bevy::prelude::*; use bevy::render::camera::ScalingMode; use micro_banimate::definitions::{AnimationSet, AnimationStatus, DirectionalAnimationBundle}; use micro_banimate::directionality::{Directionality, Horizontal}; fn main() { App::new() .add_plugins(DefaultPlugins) .add_plugins(micro_banimate::BanimatePluginGroup) .add_systems(Startup, load_assets) .add_systems(PostStartup, spawn_assets) .add_systems(Update, process_input) .run(); } #[derive(Default, Resource)] struct ExampleAssets { atlas: Handle, sprites: Handle, animations: Handle, } fn load_assets( mut commands: Commands, assets: Res, mut atlas: ResMut>, ) { let sprites = assets.load("character.png"); let atlas = atlas.add(TextureAtlasLayout::from_grid( UVec2::new(48, 48), 10, 6, None, None, )); let anim_handle = assets.load("character.anim.json"); commands.insert_resource(ExampleAssets { sprites, atlas, animations: anim_handle, }); } fn spawn_assets(mut commands: Commands, assets: Res) { const WIDTH: f32 = 480.0; const HEIGHT: f32 = 320.0; commands.spawn(( TextureAtlas { layout: assets.atlas.clone_weak(), index: 0, }, SpriteBundle { texture: assets.sprites.clone_weak(), ..Default::default() }, DirectionalAnimationBundle::with_direction( "idle", assets.animations.clone_weak(), Directionality::Right, ), )); commands.spawn(Camera2dBundle { projection: OrthographicProjection { area: Rect::new(-(WIDTH / 2.0), -(HEIGHT / 2.0), WIDTH / 2.0, HEIGHT / 2.0), scaling_mode: ScalingMode::AutoMin { min_height: HEIGHT, min_width: WIDTH, }, far: 1000., near: -1000., ..Default::default() }, ..Default::default() }); } fn process_input( input: Res>, mut status_query: Query<(&mut AnimationStatus, &mut Directionality)>, ) { for (mut status, mut direction) in &mut status_query { if input.just_released(KeyCode::ArrowLeft) { direction.with_horizontal(Horizontal::Left); } if input.just_released(KeyCode::ArrowRight) { direction.with_horizontal(Horizontal::Right); } if input.just_released(KeyCode::Digit1) { status.start_or_continue("idle"); } if input.just_released(KeyCode::Digit2) { status.start_or_continue("run"); } } }