Changes ScheduleRunnerPlugin RunMode::Loop to run on fixed interval (#233)

* Changes ScheduleRunnerPlugin RunMode::Loop to run on fixed interval

* fix formatting
This commit is contained in:
mfrancis107 2020-08-21 22:31:46 -04:00 committed by GitHub
parent 7c3b49cb6f
commit 47f3a0b8be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,7 +4,10 @@ use crate::{
event::{EventReader, Events}, event::{EventReader, Events},
plugin::Plugin, plugin::Plugin,
}; };
use std::{thread, time::Duration}; use std::{
thread,
time::{Duration, Instant},
};
/// Determines the method used to run an [App]'s `Schedule` /// Determines the method used to run an [App]'s `Schedule`
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
@ -51,6 +54,8 @@ impl Plugin for ScheduleRunnerPlugin {
app.schedule.run(&mut app.world, &mut app.resources); app.schedule.run(&mut app.world, &mut app.resources);
} }
RunMode::Loop { wait } => loop { RunMode::Loop { wait } => loop {
let start_time = Instant::now();
if let Some(app_exit_events) = app.resources.get_mut::<Events<AppExit>>() { if let Some(app_exit_events) = app.resources.get_mut::<Events<AppExit>>() {
if app_exit_event_reader.latest(&app_exit_events).is_some() { if app_exit_event_reader.latest(&app_exit_events).is_some() {
break; break;
@ -65,8 +70,13 @@ impl Plugin for ScheduleRunnerPlugin {
} }
} }
let end_time = Instant::now();
if let Some(wait) = wait { if let Some(wait) = wait {
thread::sleep(wait); let exe_time = end_time - start_time;
if exe_time < wait {
thread::sleep(wait - exe_time);
}
} }
}, },
} }