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

Update to latest Bevy main 0.11 #23

Merged
merged 20 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ categories = ["game-engines"]
description = "Bevy plugin to help implement loading states"

[dependencies]
bevy_ecs = { version = "0.10" }
bevy_app = { version = "0.10" }
bevy_asset = { version = "0.10", optional = true }
bevy_utils = { version = "0.10", optional = true }
bevy_ecs = { git = "https://github.com/bevyengine/bevy" }
bevy_app = { git = "https://github.com/bevyengine/bevy" }
bevy_asset = { git = "https://github.com/bevyengine/bevy", optional = true }
bevy_utils = { git = "https://github.com/bevyengine/bevy", optional = true }

[features]
assets = ["bevy_asset", "bevy_utils"]

[dev-dependencies]
bevy = { version = "0.10" }
bevy = { git = "https://github.com/bevyengine/bevy" }

[[example]]
name = "full"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Bevy Compatibility:
| `0.5` | `bevy_loading = 0.1` |

---

This crate helps you in cases where you need to track when a bunch of
work has been completed, and perform a state transition.

Expand Down
15 changes: 6 additions & 9 deletions examples/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,24 @@ fn main() {
// Add plugin for our game loading screen
.add_plugin(ProgressPlugin::new(AppState::GameLoading).continue_to(AppState::InGame))
// Load our UI assets during our splash screen
.add_system(load_ui_assets.in_schedule(OnEnter(AppState::Splash)))
.add_systems(OnEnter(AppState::Splash), load_ui_assets)
// Our game loading screen
// systems that implement tasks to be tracked for completion:
.add_systems(
Update,
(
net_init_session.track_progress(),
world_generation.track_progress(),
internal_thing.track_progress(),
// we can also add regular untracked systems to our loading screen,
// like to draw our progress bar:
ui_progress_bar,
ui_progress_bar.after(ProgressSystemSet::CheckProgress),
)
.in_set(LoadingSystems),
.run_if(in_state(AppState::GameLoading)),
)
.configure_set(LoadingSystems.run_if(in_state(AppState::GameLoading)))
.run();
}

// Todo: remove after https://github.com/bevyengine/bevy/pull/7676
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, SystemSet)]
struct LoadingSystems;

#[derive(Resource)]
struct MyUiAssets {
ui_font: Handle<Font>,
Expand Down Expand Up @@ -104,7 +100,8 @@ fn world_generation(
}
}

fn internal_thing(// ...
fn internal_thing(
// ...
) -> HiddenProgress {
// "hidden progress" allows us to report progress
// that is tracked separately, so it is counted for
Expand Down
65 changes: 32 additions & 33 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@
#![forbid(unsafe_code)]
#![warn(missing_docs)]

use bevy_ecs::schedule::SystemConfig;
use std::fmt::Debug;
use std::hash::Hash;
use std::ops::{Add, AddAssign};
use std::sync::atomic::AtomicU32;
use std::sync::atomic::Ordering as MemOrdering;

use bevy_app::prelude::*;
use bevy_ecs::prelude::*;
use bevy_ecs::{prelude::*, schedule::SystemConfigs};
#[cfg(feature = "assets")]
mod asset;

Expand All @@ -58,6 +57,7 @@ pub mod prelude {
pub use crate::ProgressCounter;
pub use crate::ProgressPlugin;
pub use crate::ProgressSystem;
pub use crate::ProgressSystemSet;
}

/// Progress reported by a system
Expand Down Expand Up @@ -141,7 +141,8 @@ pub struct HiddenProgress(pub Progress);
/// Add this plugin to your app, to use this crate for the specified state.
///
/// If you have multiple different states that need progress tracking,
/// you can add the plugin for each one.
/// you can add the plugin for each one. Tracking multiple state types
/// simultaneously is *not* currently supported (see issue #20).
///
/// If you want the optional assets tracking ("assets" cargo feature), enable
/// it with `.track_assets()`.
Expand Down Expand Up @@ -207,34 +208,31 @@ impl<S: States> ProgressPlugin<S> {

impl<S: States> Plugin for ProgressPlugin<S> {
fn build(&self, app: &mut App) {
app.add_system(loadstate_enter.in_schedule(OnEnter(self.state.clone())))
.add_system(
next_frame
.in_base_set(ProgressSystemSet::Preparation)
app.add_systems(OnEnter(self.state.clone()), loadstate_enter)
.add_systems(
Update,
(
next_frame
.before(TrackedProgressSet)
.in_set(ProgressSystemSet::Preparation),
check_progress::<S>(self.next_state.clone())
.after(TrackedProgressSet)
.in_set(ProgressSystemSet::CheckProgress),
)
.run_if(in_state(self.state.clone())),
)
.configure_set(
ProgressSystemSet::Preparation
.after(CoreSet::StateTransitions)
.before(TrackedProgressSet),
)
.add_system(
check_progress::<S>(self.next_state.clone())
.in_base_set(ProgressSystemSet::CheckProgress)
.run_if(in_state(self.state.clone())),
)
.configure_set(ProgressSystemSet::CheckProgress.after(TrackedProgressSet))
.add_system(loadstate_exit.in_schedule(OnExit(self.state.clone())));
.add_systems(OnExit(self.state.clone()), loadstate_exit);

#[cfg(feature = "assets")]
if self.track_assets {
app.init_resource::<asset::AssetsLoading>();
app.add_system(
app.add_systems(
Update,
asset::assets_progress
.track_progress()
.run_if(in_state(self.state.clone())),
);
app.add_system(asset::assets_loading_reset.in_schedule(OnExit(self.state.clone())));
app.add_systems(OnExit(self.state.clone()), asset::assets_loading_reset);
}

#[cfg(not(feature = "assets"))]
Expand All @@ -253,17 +251,17 @@ pub trait ProgressSystem<Params, T: ApplyProgress>: IntoSystem<(), T, Params> {
/// Call this to add your system returning [`Progress`] to your [`App`]
///
/// This adds the functionality for tracking the returned Progress.
fn track_progress(self) -> SystemConfig;
fn track_progress(self) -> SystemConfigs;
}

impl<S, T, Params> ProgressSystem<Params, T> for S
where
T: ApplyProgress + 'static,
S: IntoSystem<(), T, Params>,
{
fn track_progress(self) -> SystemConfig {
fn track_progress(self) -> SystemConfigs {
self.pipe(|In(progress): In<T>, counter: Res<ProgressCounter>| {
progress.apply_progress(&*counter);
progress.apply_progress(&counter);
})
.in_set(TrackedProgressSet)
}
Expand All @@ -279,14 +277,12 @@ fn check_progress<S: States>(next_state: Option<S>) -> impl FnMut(Res<ProgressCo
}
}

/// Base sets for running systems before and after progress tracking
/// Sets of the systems before and after progress tracking
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, SystemSet)]
#[system_set(base)]
pub enum ProgressSystemSet {
/// This base set is configured to run before [`TrackedProgressSet`]
/// and after [`CoreSet::StateTransitions`]
/// This set is configured to run before [`TrackedProgressSet`]
Preparation,
/// All systems reading progress should be part of this base set
/// All systems reading progress should be part of this set
///
/// This set is configured to run after [`TrackedProgressSet`]
CheckProgress,
Expand Down Expand Up @@ -353,9 +349,11 @@ impl ProgressCounter {
/// registered systems.
pub fn progress_complete(&self) -> Progress {
let total =
self.total.load(MemOrdering::Acquire) + self.total_hidden.load(MemOrdering::Acquire);
self.total.load(MemOrdering::Acquire) +
self.total_hidden.load(MemOrdering::Acquire);
let done =
self.done.load(MemOrdering::Acquire) + self.done_hidden.load(MemOrdering::Acquire);
self.done.load(MemOrdering::Acquire) +
self.done_hidden.load(MemOrdering::Acquire);

Progress { done, total }
}
Expand Down Expand Up @@ -469,8 +467,9 @@ pub fn dummy_system_wait_frames<const N: u32>(mut count: Local<u32>) -> HiddenPr
pub fn dummy_system_wait_millis<const MILLIS: u64>(
mut state: Local<Option<std::time::Instant>>,
) -> HiddenProgress {
let end = state
.unwrap_or_else(|| std::time::Instant::now() + std::time::Duration::from_millis(MILLIS));
let end = state.unwrap_or_else(
|| std::time::Instant::now() + std::time::Duration::from_millis(MILLIS)
);
*state = Some(end);
HiddenProgress((std::time::Instant::now() > end).into())
}