bevy_mod_aseprite 0.15.0

A plugin for using Aseprite animations in Bevy
Documentation

Aseprite plugin for Bevy

latest doc

A plugin for using Aseprite animations in Bevy.

The Aseprite component requires Bevy's Sprite and contains two fields:

Example

See examples/aseprite.rs for a complete example, you can run it with:

cargo run --example aseprite

Usage

Basic usage is as follows:

use bevy_mod_aseprite::prelude::*;

#[derive(Resource, Deref, DerefMut, Default)]
struct AsepriteHandles(Vec<Handle<AsepriteAsset>>);

fn load_assets(asset_server: Res<AssetServer>, mut ase_handles: ResMut<AsepriteHandles>) {
    let player = asset_server.load("player.ase");
    ase_handles.push(player);
}

fn setup(
    mut commands: Commands,
    ase_handles: Res<AsepriteHandles>,
    ase_assets: Res<Assets<AsepriteAsset>>,
) {
    let ase_handle = &ase_handles[0];
    let ase_asset = ase_assets.get(ase_handle).unwrap();
    let anim = AsepriteAnimation::new(&ase_asset.info, "idle");
    commands.spawn((
        Player,
        Sprite {
            image: ase_asset.atlas_texture.clone(),
            texture_atlas: Some(TextureAtlas {
                index: anim.current_frame(),
                layout: ase_asset.atlas_layout.clone(),
            }),
            ..default()
        },
        Aseprite {
            anim,
            asset: ase_handle.clone(),
        },
    ));
}

The AsepriteAnimation struct also exposes methods to get information such as the current animation frame (within a tag or not), its duration, and the number of remaining frames. This can be useful to transition states at the end of an animation:

fn transition_player(
    time: Res<Time>,
    player_q: Query<(&PlayerState, &Aseprite), With<Player>>,
    aseprites: Res<Assets<AsepriteAsset>>,
    mut ev_player_changed: MessageWriter<PlayerChanged>,
) -> Result {
    let (&player_state, ase) = player_q.single()?;
    let ase_asset = aseprites.get(&ase.asset).unwrap();
    // Change the player state to idle at the end of the attack animation
    if let PlayerState::Attack = player_state {
        let remaining_frames = ase.anim.remaining_tag_frames(&ase_asset.info).unwrap();
        let frame_finished = ase.anim.frame_finished(time.delta());
        if remaining_frames == 0 && frame_finished {
            ev_player_changed.send(PlayerState::Stand.into());
        }
    }
    Ok(())
}

Bevy Compatibility

bevy bevy_mod_aseprite
0.19 0.15
0.18 0.14
0.17 0.11, 0.12, 0.13
0.16 0.10
0.15 0.9
0.14 0.8
0.13 0.7
0.12 0.6
0.11 0.5
0.10 0.4
0.9 0.2, 0.3
0.8 0.1

History

This crate started as a fork of mdenchev/bevy_aseprite.