Add repeating flag to Timer (#165)
Repeating timers will reset themselves upon finishing, carrying over any excess time during the last tick. This fixes timer drift upon resetting.
This commit is contained in:
parent
1eca55e571
commit
68d419d40f
@ -4,37 +4,53 @@ use bevy_property::Properties;
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
/// Tracks elapsed time. Enters the finished state once `duration` is reached.
|
/// Tracks elapsed time. Enters the finished state once `duration` is reached.
|
||||||
|
///
|
||||||
|
/// Non repeating timers will stop tracking and stay in the finished state until reset.
|
||||||
|
/// Repeating timers will only be in the finished state on each tick `duration` is reached or exceeded, and can still be reset at any given point.
|
||||||
#[derive(Clone, Debug, Default, Properties)]
|
#[derive(Clone, Debug, Default, Properties)]
|
||||||
pub struct Timer {
|
pub struct Timer {
|
||||||
pub elapsed: f32,
|
pub elapsed: f32,
|
||||||
pub duration: f32,
|
pub duration: f32,
|
||||||
pub finished: bool,
|
pub finished: bool,
|
||||||
|
/// Will only be true on the tick `duration` is reached or exceeded.
|
||||||
|
pub just_finished: bool,
|
||||||
|
pub repeating: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Timer {
|
impl Timer {
|
||||||
pub fn new(duration: Duration) -> Self {
|
pub fn new(duration: Duration, repeating: bool) -> Self {
|
||||||
Timer {
|
Timer {
|
||||||
duration: duration.as_secs_f32(),
|
duration: duration.as_secs_f32(),
|
||||||
|
repeating,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_seconds(seconds: f32) -> Self {
|
pub fn from_seconds(seconds: f32, repeating: bool) -> Self {
|
||||||
Timer {
|
Timer {
|
||||||
duration: seconds,
|
duration: seconds,
|
||||||
|
repeating,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tick(&mut self, delta: f32) {
|
pub fn tick(&mut self, delta: f32) {
|
||||||
self.elapsed = (self.elapsed + delta).min(self.duration);
|
let prev_finished = self.elapsed >= self.duration;
|
||||||
if self.elapsed >= self.duration {
|
if !prev_finished {
|
||||||
self.finished = true;
|
self.elapsed += delta;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.finished = self.elapsed >= self.duration;
|
||||||
|
self.just_finished = !prev_finished && self.finished;
|
||||||
|
|
||||||
|
if self.repeating && self.finished {
|
||||||
|
self.elapsed %= self.duration;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reset(&mut self) {
|
pub fn reset(&mut self) {
|
||||||
self.finished = false;
|
self.finished = false;
|
||||||
|
self.just_finished = false;
|
||||||
self.elapsed = 0.0;
|
self.elapsed = 0.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ impl Default for PrintDiagnosticsPlugin {
|
|||||||
impl Plugin for PrintDiagnosticsPlugin {
|
impl Plugin for PrintDiagnosticsPlugin {
|
||||||
fn build(&self, app: &mut bevy_app::AppBuilder) {
|
fn build(&self, app: &mut bevy_app::AppBuilder) {
|
||||||
app.add_resource(PrintDiagnosticsState {
|
app.add_resource(PrintDiagnosticsState {
|
||||||
timer: Timer::new(self.wait_duration),
|
timer: Timer::new(self.wait_duration, true),
|
||||||
filter: self.filter.clone(),
|
filter: self.filter.clone(),
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -82,8 +82,6 @@ impl PrintDiagnosticsPlugin {
|
|||||||
Self::print_diagnostic(diagnostic);
|
Self::print_diagnostic(diagnostic);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
state.timer.reset();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,8 +103,6 @@ impl PrintDiagnosticsPlugin {
|
|||||||
println!("{:#?}\n", diagnostic);
|
println!("{:#?}\n", diagnostic);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
state.timer.reset();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,11 +12,10 @@ fn animate_sprite_system(
|
|||||||
texture_atlases: Res<Assets<TextureAtlas>>,
|
texture_atlases: Res<Assets<TextureAtlas>>,
|
||||||
mut query: Query<(&mut Timer, &mut TextureAtlasSprite, &Handle<TextureAtlas>)>,
|
mut query: Query<(&mut Timer, &mut TextureAtlasSprite, &Handle<TextureAtlas>)>,
|
||||||
) {
|
) {
|
||||||
for (mut timer, mut sprite, texture_atlas_handle) in &mut query.iter() {
|
for (timer, mut sprite, texture_atlas_handle) in &mut query.iter() {
|
||||||
if timer.finished {
|
if timer.finished {
|
||||||
let texture_atlas = texture_atlases.get(&texture_atlas_handle).unwrap();
|
let texture_atlas = texture_atlases.get(&texture_atlas_handle).unwrap();
|
||||||
sprite.index = ((sprite.index as usize + 1) % texture_atlas.textures.len()) as u32;
|
sprite.index = ((sprite.index as usize + 1) % texture_atlas.textures.len()) as u32;
|
||||||
timer.reset();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -43,5 +42,5 @@ fn setup(
|
|||||||
scale: Scale(6.0),
|
scale: Scale(6.0),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
.with(Timer::from_seconds(0.1));
|
.with(Timer::from_seconds(0.1, true));
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ impl Plugin for PrintMessagePlugin {
|
|||||||
fn build(&self, app: &mut AppBuilder) {
|
fn build(&self, app: &mut AppBuilder) {
|
||||||
let state = PrintMessageState {
|
let state = PrintMessageState {
|
||||||
message: self.message.clone(),
|
message: self.message.clone(),
|
||||||
timer: Timer::new(self.wait_duration),
|
timer: Timer::new(self.wait_duration, true),
|
||||||
};
|
};
|
||||||
app.add_resource(state)
|
app.add_resource(state)
|
||||||
.add_system(print_message_system.system());
|
.add_system(print_message_system.system());
|
||||||
@ -43,6 +43,5 @@ fn print_message_system(mut state: ResMut<PrintMessageState>, time: Res<Time>) {
|
|||||||
state.timer.tick(time.delta_seconds);
|
state.timer.tick(time.delta_seconds);
|
||||||
if state.timer.finished {
|
if state.timer.finished {
|
||||||
println!("{}", state.message);
|
println!("{}", state.message);
|
||||||
state.timer.reset();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ struct EventTriggerState {
|
|||||||
impl Default for EventTriggerState {
|
impl Default for EventTriggerState {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
EventTriggerState {
|
EventTriggerState {
|
||||||
event_timer: Timer::from_seconds(1.0),
|
event_timer: Timer::from_seconds(1.0, true),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -40,8 +40,6 @@ fn event_trigger_system(
|
|||||||
my_events.send(MyEvent {
|
my_events.send(MyEvent {
|
||||||
message: "MyEvent just happened!".to_string(),
|
message: "MyEvent just happened!".to_string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
state.event_timer.reset();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ impl Default for State {
|
|||||||
Self {
|
Self {
|
||||||
added: false,
|
added: false,
|
||||||
handle: Handle::default(),
|
handle: Handle::default(),
|
||||||
timer: Timer::from_seconds(0.05),
|
timer: Timer::from_seconds(0.05, true),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user