Skip to content

Commit

Permalink
Merge pull request #73 from arewerage/main
Browse files Browse the repository at this point in the history
Fixed the `stageless_progress` example. Added a little more clarity to the examples: `stageless_progress` and `progress_tracking`
  • Loading branch information
NiklasEi authored Jul 29, 2022
2 parents c3a60ee + 764cd5f commit 7926034
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 22 deletions.
42 changes: 32 additions & 10 deletions bevy_asset_loader/examples/progress_tracking.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use bevy::app::AppExit;
use bevy::asset::LoadState;
use bevy::diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin};
use bevy::prelude::*;
use bevy_asset_loader::prelude::*;
use iyes_progress::{ProgressCounter, ProgressPlugin};

/// This example shows how to track the loading progress of your collections using `iyes_progress`
///
/// Running it will print the current progress for every frame. The five assets from
/// the two collections will be loaded rather quickly (one or two frames). The final task
/// the two collections will be loaded rather quickly (one/a few frames). The final task
/// completes after one second. At that point, `iyes_progress` will continue to the next state
/// and the app will terminate.
fn main() {
Expand All @@ -19,17 +20,24 @@ fn main() {
)
.add_state(MyStates::AssetLoading)
.add_plugins(DefaultPlugins)
.add_plugin(FrameTimeDiagnosticsPlugin::default())
// track progress during `MyStates::AssetLoading` and continue to `MyStates::Next` when progress is completed
.add_plugin(ProgressPlugin::new(MyStates::AssetLoading).continue_to(MyStates::Next))
// gracefully quit the app when `MyStates::Next` is reached
.add_system_set(SystemSet::on_enter(MyStates::Next).with_system(expect))
.add_system_set(
SystemSet::on_update(MyStates::AssetLoading).with_system(track_fake_long_task),
SystemSet::on_update(MyStates::AssetLoading)
.with_system(track_fake_long_task.before(print_progress)),
)
.add_system_to_stage(CoreStage::PostUpdate, print_progress)
.add_system(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 = 2.0;

#[derive(AssetCollection)]
struct AudioAssets {
#[asset(path = "audio/background.ogg")]
Expand All @@ -50,8 +58,8 @@ struct TextureAssets {
}

fn track_fake_long_task(time: Res<Time>, progress: Res<ProgressCounter>) {
if time.seconds_since_startup() > 1. {
info!("done");
if time.seconds_since_startup() > DURATION_LONG_TASK_IN_SECS {
info!("Long task is completed");
progress.manually_track(true.into());
} else {
progress.manually_track(false.into());
Expand Down Expand Up @@ -88,14 +96,28 @@ fn expect(
asset_server.get_load_state(texture_assets.tree.clone()),
LoadState::Loaded
);
println!("Everything looks good!");
println!("Quitting the application...");
info!("Everything looks good!");
info!("Quitting the application...");
quit.send(AppExit);
}

fn print_progress(progress: Option<Res<ProgressCounter>>) {
if let Some(progress) = progress {
info!("Current progress: {:?}", progress.progress());
fn print_progress(
progress: Option<Res<ProgressCounter>>,
diagnostics: Res<Diagnostics>,
mut last_done: Local<u32>,
) {
if let Some(progress) = progress.map(|counter| counter.progress()) {
if progress.done > *last_done {
*last_done = progress.done;
info!(
"[Frame {}] Changed progress: {:?}",
diagnostics
.get(FrameTimeDiagnosticsPlugin::FRAME_COUNT)
.map(|diagnostic| diagnostic.sum())
.unwrap_or(0.),
progress
);
}
}
}

Expand Down
51 changes: 39 additions & 12 deletions bevy_asset_loader/examples/stageless_progress.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy::app::AppExit;
use bevy::asset::LoadState;
use bevy::diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin};
use bevy::prelude::*;
use bevy_asset_loader::prelude::*;
use iyes_loopless::prelude::*;
Expand All @@ -8,28 +9,40 @@ use iyes_progress::{ProgressCounter, ProgressPlugin};
/// This example shows how to track the loading progress of your collections using `iyes_progress`
///
/// Running it will print the current progress for every frame. The five assets from
/// the two collections will be loaded rather quickly (one or two frames). The final task
/// the two collections will be loaded rather quickly (one/a few frames). The final task
/// completes after one second. At that point, `iyes_progress` will continue to the next state
/// and the app will terminate.
///
/// NOTE: If you do not track any `iyes_progress` tasks manually, you need to let the `LoadingState`
/// handle the state transition! See [#54](https://github.com/NiklasEi/bevy_asset_loader/issues/54) for more info.
fn main() {
App::new()
.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)
.add_plugin(FrameTimeDiagnosticsPlugin::default())
// 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)
.add_system(
track_fake_long_task
.run_in_state(MyStates::AssetLoading)
.before(print_progress),
)
.add_system(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 = 2.0;

#[derive(AssetCollection)]
struct AudioAssets {
#[asset(path = "audio/background.ogg")]
Expand All @@ -50,8 +63,8 @@ struct TextureAssets {
}

fn track_fake_long_task(time: Res<Time>, progress: Res<ProgressCounter>) {
if time.seconds_since_startup() > 1. {
info!("done");
if time.seconds_since_startup() > DURATION_LONG_TASK_IN_SECS {
info!("Long task is completed");
progress.manually_track(true.into());
} else {
progress.manually_track(false.into());
Expand Down Expand Up @@ -88,14 +101,28 @@ fn expect(
asset_server.get_load_state(texture_assets.tree.clone()),
LoadState::Loaded
);
println!("Everything looks good!");
println!("Quitting the application...");
info!("Everything looks good!");
info!("Quitting the application...");
quit.send(AppExit);
}

fn print_progress(progress: Option<Res<ProgressCounter>>) {
if let Some(progress) = progress {
info!("Current progress: {:?}", progress.progress());
fn print_progress(
progress: Option<Res<ProgressCounter>>,
diagnostics: Res<Diagnostics>,
mut last_done: Local<u32>,
) {
if let Some(progress) = progress.map(|counter| counter.progress()) {
if progress.done > *last_done {
*last_done = progress.done;
info!(
"[Frame {}] Changed progress: {:?}",
diagnostics
.get(FrameTimeDiagnosticsPlugin::FRAME_COUNT)
.map(|diagnostic| diagnostic.sum())
.unwrap_or(0.),
progress
);
}
}
}

Expand Down

0 comments on commit 7926034

Please sign in to comment.