Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions crates/bevy_time/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,24 @@ impl Timer {
self.tick(remaining);
}

/// Almost finishes the timer leaving 1 ns of remaining time.
/// This can be useful when needing an immediate action without having
/// to wait for the set duration of the timer in the first tick.
///
/// # Examples
/// ```
/// # use bevy_time::*;
/// use std::time::Duration;
/// let mut timer = Timer::from_seconds(1.5, TimerMode::Once);
/// timer.almost_finish();
/// assert!(!timer.is_finished());
/// assert_eq!(timer.remaining(), Duration::from_nanos(1));
/// ```
pub fn almost_finish(&mut self) {
let remaining = self.remaining() - Duration::from_nanos(1);
self.tick(remaining);
}

/// Returns the mode of the timer.
///
/// # Examples
Expand Down Expand Up @@ -628,6 +646,21 @@ mod tests {
assert_eq!(t.times_finished_this_tick(), 34);
}

#[test]
fn almost_finished_repeating() {
let mut t = Timer::from_seconds(10.0, TimerMode::Repeating);
let duration = Duration::from_nanos(1);

t.almost_finish();
assert!(!t.is_finished());
assert_eq!(t.times_finished_this_tick(), 0);
assert_eq!(t.remaining(), Duration::from_nanos(1));

t.tick(duration);
assert!(t.is_finished());
assert_eq!(t.times_finished_this_tick(), 1);
}

#[test]
fn paused() {
let mut t = Timer::from_seconds(10.0, TimerMode::Once);
Expand Down