bevy/src/app/schedule_runner.rs

43 lines
1,006 B
Rust
Raw Normal View History

2020-03-30 18:52:33 +00:00
use crate::{
app::{App, AppBuilder},
prelude::AppPlugin,
};
use std::{thread, time::Duration};
#[derive(Copy, Clone, Debug)]
pub enum RunMode {
2020-04-04 19:43:16 +00:00
Loop { wait: Option<Duration> },
2020-03-30 18:52:33 +00:00
Once,
}
impl Default for RunMode {
fn default() -> Self {
2020-04-04 19:43:16 +00:00
RunMode::Loop { wait: None }
2020-03-30 18:52:33 +00:00
}
}
#[derive(Default)]
pub struct ScheduleRunner {
pub run_mode: RunMode,
}
impl AppPlugin for ScheduleRunner {
fn build(&self, app: AppBuilder) -> AppBuilder {
let run_mode = self.run_mode;
app.set_runner(move |mut app: App| match run_mode {
RunMode::Once => {
app.schedule.execute(&mut app.world, &mut app.resources);
}
2020-04-04 19:43:16 +00:00
RunMode::Loop { wait } => loop {
2020-03-30 18:52:33 +00:00
app.schedule.execute(&mut app.world, &mut app.resources);
if let Some(wait) = wait {
thread::sleep(wait);
}
},
})
}
fn name(&self) -> &'static str {
"ScheduleRun"
}
}