Skip to content

Commit

Permalink
Start moving examples to the new API
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasEi committed Nov 24, 2023
1 parent 3d9f378 commit cb16617
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 81 deletions.
2 changes: 1 addition & 1 deletion bevy_asset_loader/examples/atlas_from_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
LoadingState::new(MyStates::AssetLoading).continue_to_state(MyStates::Next),
)
.configure_loading_state(
LoadingStateConfig::new(MyStates::AssetLoading).add_collection::<MyAssets>(),
LoadingStateConfig::new(MyStates::AssetLoading).load_collection::<MyAssets>(),
)
.add_systems(OnEnter(MyStates::Next), draw_atlas)
.add_systems(
Expand Down
4 changes: 2 additions & 2 deletions bevy_asset_loader/examples/configure_loading_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ impl Plugin for PlayerAndMusicPlugin {
// We can add all kinds of things to the loading state here. This method can be called from any plugin any number of times.
.configure_loading_state(
LoadingStateConfig::new(MyStates::AssetLoading)
.add_collection::<AudioAssets>()
.add_collection::<ImageAssets>()
.load_collection::<AudioAssets>()
.load_collection::<ImageAssets>()
.init_resource::<ExampleResource>(),
);
}
Expand Down
14 changes: 5 additions & 9 deletions bevy_asset_loader/examples/custom_dynamic_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ fn main() {
// We need to make sure that our dynamic asset collections can be loaded from the asset file
.add_state::<MyStates>()
.add_loading_state(
LoadingState::new(MyStates::AssetLoading).continue_to_state(MyStates::Next),
LoadingState::new(MyStates::AssetLoading)
.continue_to_state(MyStates::Next)
.load_collection::<MyAssets>()
.register_dynamic_asset_collection::<CustomDynamicAssetCollection>()
.with_dynamic_assets_file::<CustomDynamicAssetCollection>("custom.my-assets.ron"),
)
.register_dynamic_asset_collection::<_, CustomDynamicAssetCollection>(
MyStates::AssetLoading,
)
.add_dynamic_collection_to_loading_state::<_, CustomDynamicAssetCollection>(
MyStates::AssetLoading,
"custom.my-assets.ron",
)
.add_collection_to_loading_state::<_, MyAssets>(MyStates::AssetLoading)
.add_systems(OnEnter(MyStates::Next), render_stuff)
.run();
}
Expand Down
2 changes: 1 addition & 1 deletion bevy_asset_loader/examples/init_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
)
.configure_loading_state(
LoadingStateConfig::new(MyStates::AssetLoading)
.add_collection::<ImageAssets>()
.load_collection::<ImageAssets>()
.init_resource::<CombinedImage>(),
)
.add_systems(OnEnter(MyStates::Next), draw)
Expand Down
24 changes: 23 additions & 1 deletion bevy_asset_loader/src/dynamic_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,29 @@ impl<State: States> DynamicAssetCollections<State> {
self.register_file_by_type_id(loading_state, file, TypeId::of::<C>());
}

fn register_file_by_type_id(&mut self, loading_state: State, file: &str, type_id: TypeId) {
pub(crate) fn register_files_by_type_id(
&mut self,
loading_state: State,
mut files: Vec<String>,
type_id: TypeId,
) {
let mut dynamic_collections_for_state =
self.files.remove(&loading_state).unwrap_or_default();
let mut dynamic_files = dynamic_collections_for_state
.remove(&type_id)
.unwrap_or_default();
dynamic_files.append(&mut files);
dynamic_collections_for_state.insert(type_id, dynamic_files);
self.files
.insert(loading_state, dynamic_collections_for_state);
}

pub(crate) fn register_file_by_type_id(
&mut self,
loading_state: State,
file: &str,
type_id: TypeId,
) {
let mut dynamic_collections_for_state =
self.files.remove(&loading_state).unwrap_or_default();
let mut dynamic_files = dynamic_collections_for_state
Expand Down
98 changes: 38 additions & 60 deletions bevy_asset_loader/src/loading_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,8 @@ where
}

impl<S: States> ConfigureLoadingState for LoadingState<S> {
fn add_collection<A: AssetCollection>(mut self) -> Self {
self.config = self.config.add_collection::<A>();
fn load_collection<A: AssetCollection>(mut self) -> Self {
self.config = self.config.load_collection::<A>();

self
}
Expand All @@ -466,7 +466,7 @@ impl<S: States> ConfigureLoadingState for LoadingState<S> {
self
}

fn with_dynamic_assets<C: DynamicAssetCollection + Asset>(mut self, file: &str) -> Self {
fn with_dynamic_assets_file<C: DynamicAssetCollection + Asset>(mut self, file: &str) -> Self {
self.config
.with_dynamic_assets_type_id(file, TypeId::of::<C>());

Expand Down Expand Up @@ -625,7 +625,7 @@ pub trait LoadingStateAppExt {
/// ```
#[deprecated(
since = "0.19.0",
note = "Use configure_loading_state and [`LoadingStateConfig::add_collection`] instead."
note = "Use load_collection on LoadingState or LoadingStateConfig instead."
)]
fn add_collection_to_loading_state<S: States, A: AssetCollection>(
&mut self,
Expand Down Expand Up @@ -735,7 +735,7 @@ pub trait LoadingStateAppExt {
/// LoadingState::new(GameState::Loading)
/// .continue_to_state(GameState::Menu)
/// )
/// .configure_loading_state(LoadingStateConfig::new(GameState::Loading).add_collection::<AudioAssets>())
/// .configure_loading_state(LoadingStateConfig::new(GameState::Loading).load_collection::<AudioAssets>())
/// # .set_runner(|mut app| app.update())
/// .run();
/// # }
Expand Down Expand Up @@ -766,65 +766,34 @@ pub struct LoadingStateConfig<S: States> {
}

pub trait ConfigureLoadingState {
fn add_collection<A: AssetCollection>(self) -> Self;
/// Add the given collection to the loading state.
///
/// Its loading progress will be tracked. Only when all included handles are fully loaded, the
/// collection will be inserted to the ECS as a resource.
///
/// See the `two_collections` example
#[must_use = "The configuration will only be applied when passed to App::configure_loading_state"]
fn load_collection<A: AssetCollection>(self) -> Self;

/// The resource will be initialized at the end of the loading state using its [`FromWorld`] implementation.
/// All asset collections will be available at that point and fully loaded.
/// ```edition2021
/// # use bevy_asset_loader::prelude::*;
/// # use bevy::prelude::*;
/// # use bevy::asset::AssetPlugin;
/// # fn main() {
/// App::new()
/// # /*
/// .add_plugins(DefaultPlugins)
/// # */
/// # .add_plugins((MinimalPlugins, AssetPlugin::default()))
/// .add_state::<GameState>()
/// # .init_resource::<iyes_progress::ProgressCounter>()
/// .add_loading_state(
/// LoadingState::new(GameState::Loading)
/// .continue_to_state(GameState::Menu)
/// )
/// .configure_loading_state(
/// LoadingStateConfig::new(GameState::Loading)
/// .add_collection::<TextureForAtlas>()
/// .init_resource::<TextureAtlasFromWorld>(),
/// )
/// # .set_runner(|mut app| app.update())
/// # .run();
/// # }
/// # #[derive(Clone, Eq, PartialEq, Debug, Hash, Default, States)]
/// # enum GameState {
/// # #[default]
/// # Loading,
/// # Menu
/// # }
/// # #[derive(Resource)]
/// # struct TextureAtlasFromWorld {
/// # atlas: Handle<TextureAtlas>
/// # }
/// # impl FromWorld for TextureAtlasFromWorld {
/// # fn from_world(world: &mut World) -> Self {
/// # let cell = world.cell();
/// # let assets = cell.get_resource::<TextureForAtlas>().expect("TextureForAtlas not loaded");
/// # let mut atlases = cell.get_resource_mut::<Assets<TextureAtlas>>().expect("TextureAtlases missing");
/// # TextureAtlasFromWorld {
/// # atlas: atlases.add(TextureAtlas::from_grid(assets.array.clone(), Vec2::new(250., 250.), 1, 4, None, None))
/// # }
/// # }
/// # }
/// # #[derive(AssetCollection, Resource)]
/// # pub struct TextureForAtlas {
/// # #[asset(path = "images/female_adventurer.ogg")]
/// # pub array: Handle<Image>,
/// # }
/// ```
///
/// See the `init_resource` example
#[must_use = "The configuration will only be applied when passed to App::configure_loading_state"]
fn init_resource<R: Resource + FromWorld>(self) -> Self;

/// Register a custom dynamic asset collection type
///
/// See the `custom_dynamic_assets` example
#[must_use = "The configuration will only be applied when passed to App::configure_loading_state"]
fn register_dynamic_asset_collection<C: DynamicAssetCollection + Asset>(self) -> Self;

fn with_dynamic_assets<C: DynamicAssetCollection + Asset>(self, file: &str) -> Self;
/// Add a file containing dynamic assets to the loading state. Keys contained in the file, will
/// be available for asset collections.
///
/// See the `dynamic_asset` example
#[must_use = "The configuration will only be applied when passed to App::configure_loading_state"]
fn with_dynamic_assets_file<C: DynamicAssetCollection + Asset>(self, file: &str) -> Self;
}

impl<S: States> LoadingStateConfig<S> {
Expand All @@ -845,7 +814,7 @@ impl<S: States> LoadingStateConfig<S> {
self.dynamic_assets.insert(type_id, dynamic_files);
}

fn build(self, app: &mut App) {
fn build(mut self, app: &mut App) {
for config in self.on_enter_loading_assets {
app.add_systems(
OnEnterInternalLoadingState(
Expand Down Expand Up @@ -873,11 +842,20 @@ impl<S: States> LoadingStateConfig<S> {
config,
);
}
let mut dynamic_assets = app
.world
.get_resource_mut::<DynamicAssetCollections<S>>()
.unwrap_or_else(|| {
panic!("Failed to get the DynamicAssetCollections resource for the loading state.")
});
for (id, files) in self.dynamic_assets.drain() {
dynamic_assets.register_files_by_type_id(self.state.clone(), files, id);
}
}
}

impl<S: States> ConfigureLoadingState for LoadingStateConfig<S> {
fn add_collection<A: AssetCollection>(mut self) -> Self {
fn load_collection<A: AssetCollection>(mut self) -> Self {
self.on_enter_loading_assets
.push(start_loading_collection::<S, A>.into_configs());
self.on_update.push(
Expand Down Expand Up @@ -907,7 +885,7 @@ impl<S: States> ConfigureLoadingState for LoadingStateConfig<S> {
self
}

fn with_dynamic_assets<C: DynamicAssetCollection + Asset>(mut self, file: &str) -> Self {
fn with_dynamic_assets_file<C: DynamicAssetCollection + Asset>(mut self, file: &str) -> Self {
self.with_dynamic_assets_type_id(file, TypeId::of::<C>());

self
Expand Down
7 changes: 0 additions & 7 deletions ideas.md

This file was deleted.

0 comments on commit cb16617

Please sign in to comment.