mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
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:
parent
7c3b49cb6f
commit
47f3a0b8be
1 changed files with 12 additions and 2 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue