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