-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove
audio
+ assets
modules in favor of raw Bevy APIs and thin …
…LoadResource API (#264) Co-authored-by: Ben Frankel <[email protected]> Co-authored-by: Jan Hohenheim <[email protected]>
- Loading branch information
1 parent
149e7ea
commit d7d8c10
Showing
16 changed files
with
300 additions
and
365 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//! A simple, high-level way to load collections of asset handles as resources | ||
|
||
use bevy::prelude::*; | ||
|
||
pub(super) fn plugin(app: &mut App) { | ||
app.init_resource::<ResourceHandles>() | ||
.add_systems(PreUpdate, load_resource_assets); | ||
} | ||
|
||
pub trait LoadResource { | ||
/// This will load the [`Resource`] as an [`Asset`]. When all of its asset dependencies | ||
/// have been loaded, it will be inserted as a resource. This ensures that the resource only | ||
/// exists when the assets are ready. | ||
fn load_resource<T: Resource + Asset + Clone + FromWorld>(&mut self) -> &mut Self; | ||
} | ||
|
||
impl LoadResource for App { | ||
fn load_resource<T: Resource + Asset + Clone + FromWorld>(&mut self) -> &mut Self { | ||
self.init_asset::<T>(); | ||
let world = self.world_mut(); | ||
let value = T::from_world(world); | ||
let assets = world.resource::<AssetServer>(); | ||
let handle = assets.add(value); | ||
let mut handles = world.resource_mut::<ResourceHandles>(); | ||
handles.waiting.push((handle.untyped(), |world, handle| { | ||
let assets = world.resource::<Assets<T>>(); | ||
if let Some(value) = assets.get(handle.id().typed::<T>()) { | ||
world.insert_resource(value.clone()); | ||
} | ||
})); | ||
self | ||
} | ||
} | ||
|
||
/// A function that inserts a loaded resource. | ||
type InsertLoadedResource = fn(&mut World, &UntypedHandle); | ||
|
||
#[derive(Resource, Default)] | ||
struct ResourceHandles { | ||
waiting: Vec<(UntypedHandle, InsertLoadedResource)>, | ||
#[allow(unused)] | ||
finished: Vec<UntypedHandle>, | ||
} | ||
|
||
fn load_resource_assets(world: &mut World) { | ||
world.resource_scope(|world, mut resource_handles: Mut<ResourceHandles>| { | ||
world.resource_scope(|world, assets: Mut<AssetServer>| { | ||
for _ in 0..resource_handles.waiting.len() { | ||
let (handle, insert_fn) = resource_handles.waiting.pop().unwrap(); | ||
if assets.is_loaded_with_dependencies(&handle) { | ||
insert_fn(world, &handle); | ||
resource_handles.finished.push(handle); | ||
} else { | ||
resource_handles.waiting.push((handle, insert_fn)); | ||
} | ||
} | ||
}); | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use bevy::prelude::*; | ||
|
||
/// An organizational marker component that should be added to a spawned [`AudioBundle`] if it is in the | ||
/// general "music" category (ex: global background music, soundtrack, etc). | ||
/// | ||
/// This can then be used to query for and operate on sounds in that category. For example: | ||
/// | ||
/// ``` | ||
/// use bevy::prelude::*; | ||
/// use bevy_quickstart::audio::Music; | ||
/// | ||
/// fn set_music_volume(sink_query: Query<&AudioSink, With<Music>>) { | ||
/// for sink in &sink_query { | ||
/// sink.set_volume(0.5); | ||
/// } | ||
/// } | ||
/// ``` | ||
#[derive(Component, Default)] | ||
pub struct Music; | ||
|
||
/// An organizational marker component that should be added to a spawned [`AudioBundle`] if it is in the | ||
/// general "sound effect" category (ex: footsteps, the sound of a magic spell, a door opening). | ||
/// | ||
/// This can then be used to query for and operate on sounds in that category. For example: | ||
/// | ||
/// ``` | ||
/// use bevy::prelude::*; | ||
/// use bevy_quickstart::audio::SoundEffect; | ||
/// | ||
/// fn set_sound_effect_volume(sink_query: Query<&AudioSink, With<SoundEffect>>) { | ||
/// for sink in &sink_query { | ||
/// sink.set_volume(0.5); | ||
/// } | ||
/// } | ||
/// ``` | ||
#[derive(Component, Default)] | ||
pub struct SoundEffect; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.