#assets #bevy #bevy-plugin #gamedev

covey_asset_loader

Bevy plugin for asset loading

1 unstable release

0.1.1 Apr 17, 2023
0.1.0 Apr 17, 2023

#2396 in Game dev

MIT/Apache

2MB
207 lines

Covey Asset Loader

This Bevy plugin tries to tackle the problem of asset dependencies for states. For example, you have

enum AppState {
    Splash,
    ..
}

struct SplashAssets { .. }

and you want to make sure SplashAssets is available before Splash is entered and also get cleaned up SplashAssets when Splash is exited.

Usage

See the basic example for a complete usage. But in general, it is like

Requirements on your State

use covey_asset_loader::prelude::*; 

// implement AssetState
#[derive(AssetState, States, ..)]
enum AppState {     
    #[default]                 
    Boot,
    #[assets(SplashAssets)]
    Splash,
    #[assets(MainMenuAssets)]
    MainMenu,
    // no it doesn't support multiple assets
    // #[assets(InGameAssets1, InGameAssets2)]
    InGame,
}

Requirements on your Assets

// implement AssetCollection , Resource and Reflect
#[derive(AssetCollection, Resource, Reflect, ..)]
struct SplashAssets {
    #[asset(path = "fonts/FiraSans-Bold.ttf")]
    font1: Handle<Font>,
    #[asset(path = "audio/breakout_collision.ogg")]
    audio1: Handle<AudioSource>,
    #[asset(path = "images/icon.png")]
    image1: Handle<Image>,
}

Available methods

impl Plugin for SplashPlugin { 
    fn build(&self, app: &mut App) {
        app
            // Required, load SplashAssets for AppState
            .state_asset_loader::<SplashAssets, AppState>()
            // Optional, release SplashAssets when it exits AppState::Splash after `GlobalAssetCleanUpTimer` has finished, default is 5 seconds.
            .cleanup_assets_on_exit::<SplashAssets>(AppState::Splash)
            // Optional, if specified, will override `GlobalAssetCleanUpTimer` for this specific Resrouce, in this case, SplashAssets will be released after 2 seconds.
            .insert_resource(AssetCleanUpTimer::<SplashAssets>(
                Timer::from_seconds(2.0, TimerMode::Once),
                PhantomData,
            ))
    }
}

Changing state

Unfortunately to make it work, you have to use ScheduleNextState instead of NextState to change state. It is not a feature but a workaround of the limitation of Bevy

fn system(
    mut schedule_state: ResMut<ScheduleNextState<AppState>>,
) {
    schedule_state.set(AppState::MainMenu);
}

Dependencies

~20–56MB
~1M SLoC