Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the stageless_progress example. Added a little more clarity to the examples: stageless_progress and progress_tracking #73

Merged
merged 4 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions bevy_asset_loader/examples/progress_tracking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ fn main() {
.run();
}

// Time in seconds to complete a custom long-running task.
// If assets are loaded earlier, the current state will not
// be changed until the 'fake long task' is completed (thanks to 'iyes_progress')
const DURATION_LONG_TASK_IN_SECS: f64 = 3.0;

#[derive(AssetCollection)]
struct AudioAssets {
#[asset(path = "audio/background.ogg")]
Expand All @@ -49,12 +54,14 @@ struct TextureAssets {
female_adventurer: Handle<TextureAtlas>,
}

fn track_fake_long_task(time: Res<Time>, progress: Res<ProgressCounter>) {
if time.seconds_since_startup() > 1. {
info!("done");
progress.manually_track(true.into());
} else {
progress.manually_track(false.into());
fn track_fake_long_task(time: Res<Time>, progress: Res<ProgressCounter>, mut printed: Local<bool>) {
NiklasEi marked this conversation as resolved.
Show resolved Hide resolved
let ended = time.seconds_since_startup() > DURATION_LONG_TASK_IN_SECS;

progress.manually_track((ended).into());

if ended && !*printed {
info!("The fake long task is completed!");
*printed = true;
}
}

Expand Down Expand Up @@ -95,7 +102,10 @@ fn expect(

fn print_progress(progress: Option<Res<ProgressCounter>>) {
if let Some(progress) = progress {
info!("Current progress: {:?}", progress.progress());
// The condition to remove hell in the console
NiklasEi marked this conversation as resolved.
Show resolved Hide resolved
if progress.is_changed() {
info!("Current progress: {:?}", progress.progress());
NiklasEi marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

Expand Down
27 changes: 18 additions & 9 deletions bevy_asset_loader/examples/stageless_progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@ fn main() {
.add_loopless_state(MyStates::AssetLoading)
.add_loading_state(
LoadingState::new(MyStates::AssetLoading)
.continue_to_state(MyStates::Next)
.with_collection::<TextureAssets>()
.with_collection::<AudioAssets>(),
)
.add_plugins(DefaultPlugins)
// track progress during `MyStates::AssetLoading` and continue to `MyStates::Next` when progress is completed
.add_plugin(ProgressPlugin::new(MyStates::AssetLoading))
.add_plugin(ProgressPlugin::new(MyStates::AssetLoading).continue_to(MyStates::Next))
// gracefully quit the app when `MyStates::Next` is reached
.add_enter_system(MyStates::Next, expect)
.add_system(track_fake_long_task.run_in_state(MyStates::AssetLoading))
.add_system_to_stage(CoreStage::PostUpdate, print_progress)
.run();
}

// Time in seconds to complete a custom long-running task.
// If assets are loaded earlier, the current state will not
// be changed until the 'fake long task' is completed (thanks to 'iyes_progress')
const DURATION_LONG_TASK_IN_SECS: f64 = 3.0;

#[derive(AssetCollection)]
struct AudioAssets {
#[asset(path = "audio/background.ogg")]
Expand All @@ -49,12 +53,14 @@ struct TextureAssets {
female_adventurer: Handle<TextureAtlas>,
}

fn track_fake_long_task(time: Res<Time>, progress: Res<ProgressCounter>) {
if time.seconds_since_startup() > 1. {
info!("done");
progress.manually_track(true.into());
} else {
progress.manually_track(false.into());
fn track_fake_long_task(time: Res<Time>, progress: Res<ProgressCounter>, mut printed: Local<bool>) {
let ended = time.seconds_since_startup() > DURATION_LONG_TASK_IN_SECS;

progress.manually_track((ended).into());

if ended && !*printed {
info!("The fake long task is completed!");
*printed = true;
}
}

Expand Down Expand Up @@ -95,7 +101,10 @@ fn expect(

fn print_progress(progress: Option<Res<ProgressCounter>>) {
if let Some(progress) = progress {
info!("Current progress: {:?}", progress.progress());
// The condition to remove hell in the console
if progress.is_changed() {
info!("Current progress: {:?}", progress.progress());
}
}
}

Expand Down