Clarify behaviour of Timer::finished() for repeating timers (#9939)

# Objective

I was wondering whether to use `Timer::finished` or
`Timer::just_finished` for my repeating timer. This PR clarifies their
difference (or rather, lack thereof).

## Solution

More docs & examples.
This commit is contained in:
cyqsimon 2023-09-30 05:57:01 +08:00 committed by GitHub
parent 483f2464a8
commit 14db5b38dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -47,18 +47,28 @@ impl Timer {
} }
} }
/// Returns `true` if the timer has reached its duration at least once. /// Returns `true` if the timer has reached its duration.
/// See also [`Timer::just_finished`](Timer::just_finished). ///
/// For repeating timers, this method behaves identically to [`Timer::just_finished`].
/// ///
/// # Examples /// # Examples
/// ``` /// ```
/// # use bevy_time::*; /// # use bevy_time::*;
/// use std::time::Duration; /// use std::time::Duration;
/// let mut timer = Timer::from_seconds(1.0, TimerMode::Once); ///
/// timer.tick(Duration::from_secs_f32(1.5)); /// let mut timer_once = Timer::from_seconds(1.0, TimerMode::Once);
/// assert!(timer.finished()); /// timer_once.tick(Duration::from_secs_f32(1.5));
/// timer.tick(Duration::from_secs_f32(0.5)); /// assert!(timer_once.finished());
/// assert!(timer.finished()); /// timer_once.tick(Duration::from_secs_f32(0.5));
/// assert!(timer_once.finished());
///
/// let mut timer_repeating = Timer::from_seconds(1.0, TimerMode::Repeating);
/// timer_repeating.tick(Duration::from_secs_f32(1.1));
/// assert!(timer_repeating.finished());
/// timer_repeating.tick(Duration::from_secs_f32(0.8));
/// assert!(!timer_repeating.finished());
/// timer_repeating.tick(Duration::from_secs_f32(0.6));
/// assert!(timer_repeating.finished());
/// ``` /// ```
#[inline] #[inline]
pub fn finished(&self) -> bool { pub fn finished(&self) -> bool {