Skip to content

Commit

Permalink
configure_loading_state init_resource
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasEi committed Nov 23, 2023
1 parent 03a4620 commit 8b227b4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 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).with_collection::<MyAssets>(),
LoadingStateConfig::new(MyStates::AssetLoading).add_collection::<MyAssets>(),
)
.add_systems(OnEnter(MyStates::Next), draw_atlas)
.add_systems(
Expand Down
8 changes: 6 additions & 2 deletions bevy_asset_loader/examples/init_resource.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy::prelude::*;
use bevy_asset_loader::loading_state::LoadingStateConfig;
use bevy_asset_loader::prelude::*;

/// This example demonstrates how you can use [`App::init_resource_after_loading_state`] to initialize
Expand All @@ -13,8 +14,11 @@ fn main() {
.add_loading_state(
LoadingState::new(MyStates::AssetLoading).continue_to_state(MyStates::Next),
)
.add_collection_to_loading_state::<_, ImageAssets>(MyStates::AssetLoading)
.init_resource_after_loading_state::<_, CombinedImage>(MyStates::AssetLoading)
.configure_loading_state(
LoadingStateConfig::new(MyStates::AssetLoading)
.add_collection::<ImageAssets>()
.init_resource::<CombinedImage>(),
)
.add_systems(OnEnter(MyStates::Next), draw)
.run();
}
Expand Down
43 changes: 34 additions & 9 deletions bevy_asset_loader/src/loading_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,10 @@ pub trait LoadingStateAppExt {
/// # pub tree: Handle<Image>,
/// # }
/// ```
#[deprecated(
since = "0.19.0",
note = "Use configure_loading_state and [`LoadingStateConfig::add_collection`] instead."
)]
fn add_collection_to_loading_state<S: States, A: AssetCollection>(
&mut self,
loading_state: S,
Expand Down Expand Up @@ -660,6 +664,10 @@ pub trait LoadingStateAppExt {
/// # pub array: Handle<Image>,
/// # }
/// ```
#[deprecated(
since = "0.19.0",
note = "Use configure_loading_state and [`LoadingStateConfig::init_resource`] instead."
)]
fn init_resource_after_loading_state<S: States, A: Resource + FromWorld>(
&mut self,
loading_state: S,
Expand All @@ -673,23 +681,25 @@ pub trait LoadingStateAppExt {

pub struct LoadingStateConfig<S: States> {
state: S,
start_loading: Vec<SystemConfigs>,
check_loading: Vec<SystemConfigs>,
on_enter_loading_assets: Vec<SystemConfigs>,
on_update: Vec<SystemConfigs>,
on_enter_finalize: Vec<SystemConfigs>,
}

impl<S: States> LoadingStateConfig<S> {
pub fn new(state: S) -> Self {
Self {
state,
start_loading: vec![],
check_loading: vec![],
on_enter_loading_assets: vec![],
on_update: vec![],
on_enter_finalize: vec![],
}
}

pub fn with_collection<A: AssetCollection>(mut self) -> Self {
self.start_loading
pub fn add_collection<A: AssetCollection>(mut self) -> Self {
self.on_enter_loading_assets
.push(start_loading_collection::<S, A>.into_configs());
self.check_loading.push(
self.on_update.push(
check_loading_collection::<S, A>
.in_set(InternalLoadingStateSet::CheckAssets)
.into_configs(),
Expand All @@ -698,8 +708,17 @@ impl<S: States> LoadingStateConfig<S> {
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.
pub fn init_resource<R: Resource + FromWorld>(mut self) -> Self {
self.on_enter_finalize
.push(init_resource::<R>.into_configs());

self
}

fn build(self, app: &mut App) {
for config in self.start_loading {
for config in self.on_enter_loading_assets {
app.add_systems(
OnEnterInternalLoadingState(
self.state.clone(),
Expand All @@ -708,9 +727,15 @@ impl<S: States> LoadingStateConfig<S> {
config,
);
}
for config in self.check_loading {
for config in self.on_update {
app.add_systems(LoadingStateSchedule(self.state.clone()), config);
}
for config in self.on_enter_finalize {
self.add_systems(
OnEnterInternalLoadingState(self.state.clone(), InternalLoadingState::Finalize),
config,
);
}
}
}

Expand Down

0 comments on commit 8b227b4

Please sign in to comment.