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

Remove audio + assets modules in favor of raw Bevy APIs and thin LoadResource API #264

Merged
merged 12 commits into from
Aug 15, 2024
57 changes: 57 additions & 0 deletions src/asset_tracking.rs
cart marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use bevy::prelude::*;
use std::collections::VecDeque;

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_front((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
}
}

#[derive(Resource, Default)]
struct ResourceHandles {
waiting: VecDeque<(UntypedHandle, fn(&mut World, &UntypedHandle))>,
cart marked this conversation as resolved.
Show resolved Hide resolved
#[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() {
cart marked this conversation as resolved.
Show resolved Hide resolved
let (handle, insert_fn) = resource_handles.waiting.pop_back().unwrap();
if assets.is_loaded_with_dependencies(&handle) {
insert_fn(world, &handle);
resource_handles.finished.push(handle);
} else {
resource_handles.waiting.push_front((handle, insert_fn));
}
}
});
});
}
114 changes: 0 additions & 114 deletions src/assets.rs

This file was deleted.

7 changes: 7 additions & 0 deletions src/audio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use bevy::prelude::*;

#[derive(Component, Default)]
pub struct BackgroundMusic;

#[derive(Component, Default)]
pub struct SoundEffect;
cart marked this conversation as resolved.
Show resolved Hide resolved
78 changes: 0 additions & 78 deletions src/audio/bgm.rs

This file was deleted.

10 changes: 0 additions & 10 deletions src/audio/mod.rs

This file was deleted.

89 changes: 0 additions & 89 deletions src/audio/sfx.rs

This file was deleted.

Loading