Skip to content

Commit

Permalink
Remaining fn in Timer (bevyengine#5971)
Browse files Browse the repository at this point in the history
# Objective

Fixes bevyengine#5963 

## Solution

Add remaining fn in Timer class, this function only minus total duration with elapsed time.

Co-authored-by: Sergi-Ferrez <61662926+Sergi-Ferrez@users.noreply.github.com>
  • Loading branch information
2 people authored and james7132 committed Oct 28, 2022
1 parent ce9cf20 commit 775bbc6
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions crates/bevy_time/src/timer.rs
Expand Up @@ -343,6 +343,38 @@ impl Timer {
1.0 - self.percent()
}

/// Returns the remaining time in seconds
///
/// # Examples
/// ```
/// # use bevy_time::*;
/// use std::cmp::Ordering;
/// use std::time::Duration;
/// let mut timer = Timer::from_seconds(2.0, false);
/// timer.tick(Duration::from_secs_f32(0.5));
/// let result = timer.remaining_secs().total_cmp(&1.5);
/// assert_eq!(Ordering::Equal, result);
/// ```
#[inline]
pub fn remaining_secs(&self) -> f32 {
self.remaining().as_secs_f32()
}

/// Returns the remaining time using Duration
///
/// # Examples
/// ```
/// # use bevy_time::*;
/// use std::time::Duration;
/// let mut timer = Timer::from_seconds(2.0, false);
/// timer.tick(Duration::from_secs_f32(0.5));
/// assert_eq!(timer.remaining(), Duration::from_secs_f32(1.5));
/// ```
#[inline]
pub fn remaining(&self) -> Duration {
self.duration() - self.elapsed()
}

/// Returns the number of times a repeating timer
/// finished during the last [`tick`](Timer<T>::tick) call.
///
Expand Down

0 comments on commit 775bbc6

Please sign in to comment.