use std::time::{Duration, Instant};
use bevy::{
prelude::{Commands, EventReader, EventWriter, IntoSystemDescriptor},
MinimalPlugins,
};
use bevy_app::{App, AppExit};
use bevy_background_compute::{
BackgroundComputeCheck, BackgroundComputeComplete, BackgroundComputePlugin,
ComputeInBackgroundCommandExt,
};
#[derive(Debug)]
struct FunnyNumber(i32);
fn main() {
App::new()
.add_plugins(MinimalPlugins)
.add_plugin(BackgroundComputePlugin::<FunnyNumber>::default())
.add_startup_system(start_background_compute)
.add_system(
background_compute_callback
.after(BackgroundComputeCheck::<FunnyNumber>::new()),
)
.run();
}
fn start_background_compute(mut commands: Commands) {
println!("Starting background compute");
commands.compute_in_background(async move {
let start_time = Instant::now();
let duration = Duration::from_secs(5);
while start_time.elapsed() < duration {
}
FunnyNumber(69)
});
println!("Returning from starting compute");
}
fn background_compute_callback(
mut events: EventReader<BackgroundComputeComplete<FunnyNumber>>,
mut exit: EventWriter<AppExit>,
) {
if events.len() > 0 {
println!(
"Funny number found: {:?}",
events.iter().next().unwrap().0 .0
);
exit.send(AppExit);
}
}