Skip to content

Commit

Permalink
Add repeating flag to Timer
Browse files Browse the repository at this point in the history
Repeating timers will reset themselves upon finishing, carrying over any
excess time during the last tick. This fixes timer drift upon resetting.
  • Loading branch information
8bit-pudding committed Aug 19, 2020
1 parent 7d1d9dc commit 1feeb70
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
29 changes: 24 additions & 5 deletions crates/bevy_core/src/time/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,58 @@ use bevy_property::Properties;
use std::time::Duration;

/// 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)]
pub struct Timer {
pub elapsed: f32,
pub duration: f32,
pub finished: bool,
/// Stores the last tick's finished state. Is checked against in just_finished.
prev_finished: bool,
pub repeating: bool,
}

impl Timer {
pub fn new(duration: Duration) -> Self {
pub fn new(duration: Duration, repeating: bool) -> Self {
Timer {
duration: duration.as_secs_f32(),
repeating,
..Default::default()
}
}

pub fn from_seconds(seconds: f32) -> Self {
pub fn from_seconds(seconds: f32, repeating: bool) -> Self {
Timer {
duration: seconds,
repeating,
..Default::default()
}
}

pub fn tick(&mut self, delta: f32) {
self.elapsed = (self.elapsed + delta).min(self.duration);
if self.elapsed >= self.duration {
self.finished = true;
if self.elapsed < self.duration {
self.elapsed += delta;
}

self.prev_finished = self.finished;
self.finished = self.elapsed >= self.duration;

if self.repeating && self.finished {
self.elapsed = self.elapsed % self.duration;
}
}

pub fn reset(&mut self) {
self.finished = false;
self.elapsed = 0.0;
}

/// Returns true only on the exact tick elapsed time first exceeds duration.
pub fn just_finished(&self) -> bool {
(!self.prev_finished || self.repeating) && self.finished
}
}

pub(crate) fn timer_system(time: Res<Time>, mut query: Query<&mut Timer>) {
Expand Down
6 changes: 1 addition & 5 deletions crates/bevy_diagnostic/src/print_diagnostics_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Default for PrintDiagnosticsPlugin {
impl Plugin for PrintDiagnosticsPlugin {
fn build(&self, app: &mut bevy_app::AppBuilder) {
app.add_resource(PrintDiagnosticsState {
timer: Timer::new(self.wait_duration),
timer: Timer::new(self.wait_duration, true),
filter: self.filter.clone(),
});

Expand Down Expand Up @@ -82,8 +82,6 @@ impl PrintDiagnosticsPlugin {
Self::print_diagnostic(diagnostic);
}
}

state.timer.reset();
}
}

Expand All @@ -105,8 +103,6 @@ impl PrintDiagnosticsPlugin {
println!("{:#?}\n", diagnostic);
}
}

state.timer.reset();
}
}
}
3 changes: 1 addition & 2 deletions examples/2d/sprite_sheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ fn animate_sprite_system(
if timer.finished {
let texture_atlas = texture_atlases.get(&texture_atlas_handle).unwrap();
sprite.index = ((sprite.index as usize + 1) % texture_atlas.textures.len()) as u32;
timer.reset();
}
}
}
Expand All @@ -43,5 +42,5 @@ fn setup(
scale: Scale(6.0),
..Default::default()
})
.with(Timer::from_seconds(0.1));
.with(Timer::from_seconds(0.1, true));
}
3 changes: 1 addition & 2 deletions examples/app/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Plugin for PrintMessagePlugin {
fn build(&self, app: &mut AppBuilder) {
let state = PrintMessageState {
message: self.message.clone(),
timer: Timer::new(self.wait_duration),
timer: Timer::new(self.wait_duration, true),
};
app.add_resource(state)
.add_system(print_message_system.system());
Expand All @@ -43,6 +43,5 @@ fn print_message_system(mut state: ResMut<PrintMessageState>, time: Res<Time>) {
state.timer.tick(time.delta_seconds);
if state.timer.finished {
println!("{}", state.message);
state.timer.reset();
}
}
4 changes: 1 addition & 3 deletions examples/ecs/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct EventTriggerState {
impl Default for EventTriggerState {
fn default() -> Self {
EventTriggerState {
event_timer: Timer::from_seconds(1.0),
event_timer: Timer::from_seconds(1.0, true),
}
}
}
Expand All @@ -40,8 +40,6 @@ fn event_trigger_system(
my_events.send(MyEvent {
message: "MyEvent just happened!".to_string(),
});

state.event_timer.reset();
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/ui/font_atlas_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Default for State {
Self {
added: false,
handle: Handle::default(),
timer: Timer::from_seconds(0.05),
timer: Timer::from_seconds(0.05, true),
}
}
}
Expand Down

0 comments on commit 1feeb70

Please sign in to comment.