use std::marker::PhantomData;
use crate::prelude::*;
use beet_core::prelude::*;
pub struct RunOnAssetReadyPlugin<A: Asset>(PhantomData<A>);
impl<A: Asset> Default for RunOnAssetReadyPlugin<A> {
fn default() -> Self { Self(PhantomData) }
}
impl<A: Asset> Plugin for RunOnAssetReadyPlugin<A> {
fn build(&self, app: &mut App) {
app.add_systems(Update, run_on_asset_ready::<A>);
}
}
#[derive(Debug, Component)]
pub struct RunOnAssetReady<A: Asset> {
pub handle: Handle<A>,
}
impl<A: Asset> RunOnAssetReady<A> {
pub fn new(handle: Handle<A>) -> Self { Self { handle } }
}
fn run_on_asset_ready<A: Asset>(
mut asset_events: MessageReader<AssetEvent<A>>,
mut commands: Commands,
query: Query<(Entity, &RunOnAssetReady<A>)>,
) {
for ev in asset_events.read() {
match ev {
AssetEvent::LoadedWithDependencies { id } => {
for (entity, run_on_ready) in query.iter() {
if run_on_ready.handle.id() == *id {
commands
.entity(entity)
.remove::<RunOnAssetReady<A>>()
.trigger_target(GetOutcome);
}
}
}
_ => {}
}
}
}