Struct bevy_internal::time::Timer
source · pub struct Timer { /* private fields */ }
Expand description
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.
Paused timers will not have elapsed time increased.
Implementations§
source§impl Timer
impl Timer
sourcepub fn new(duration: Duration, mode: TimerMode) -> Timer
pub fn new(duration: Duration, mode: TimerMode) -> Timer
Creates a new timer with a given duration.
See also Timer::from_seconds
.
sourcepub fn from_seconds(duration: f32, mode: TimerMode) -> Timer
pub fn from_seconds(duration: f32, mode: TimerMode) -> Timer
Creates a new timer with a given duration in seconds.
Example
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
sourcepub fn finished(&self) -> bool
pub fn finished(&self) -> bool
Returns true
if the timer has reached its duration.
For repeating timers, this method behaves identically to Timer::just_finished
.
Examples
use std::time::Duration;
let mut timer_once = Timer::from_seconds(1.0, TimerMode::Once);
timer_once.tick(Duration::from_secs_f32(1.5));
assert!(timer_once.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());
sourcepub fn just_finished(&self) -> bool
pub fn just_finished(&self) -> bool
Returns true
only on the tick the timer reached its duration.
Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
timer.tick(Duration::from_secs_f32(1.5));
assert!(timer.just_finished());
timer.tick(Duration::from_secs_f32(0.5));
assert!(!timer.just_finished());
sourcepub fn elapsed(&self) -> Duration
pub fn elapsed(&self) -> Duration
Returns the time elapsed on the timer. Guaranteed to be between 0.0 and duration
.
Will only equal duration
when the timer is finished and non repeating.
See also Stopwatch::elapsed
.
Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
timer.tick(Duration::from_secs_f32(0.5));
assert_eq!(timer.elapsed(), Duration::from_secs_f32(0.5));
sourcepub fn elapsed_secs(&self) -> f32
pub fn elapsed_secs(&self) -> f32
Returns the time elapsed on the timer as an f32
.
See also Timer::elapsed
.
sourcepub fn set_elapsed(&mut self, time: Duration)
pub fn set_elapsed(&mut self, time: Duration)
Sets the elapsed time of the timer without any other considerations.
See also Stopwatch::set
.
use std::time::Duration;
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
timer.set_elapsed(Duration::from_secs(2));
assert_eq!(timer.elapsed(), Duration::from_secs(2));
// the timer is not finished even if the elapsed time is greater than the duration.
assert!(!timer.finished());
sourcepub fn duration(&self) -> Duration
pub fn duration(&self) -> Duration
Returns the duration of the timer.
Examples
use std::time::Duration;
let timer = Timer::new(Duration::from_secs(1), TimerMode::Once);
assert_eq!(timer.duration(), Duration::from_secs(1));
sourcepub fn set_duration(&mut self, duration: Duration)
pub fn set_duration(&mut self, duration: Duration)
Sets the duration of the timer.
Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(1.5, TimerMode::Once);
timer.set_duration(Duration::from_secs(1));
assert_eq!(timer.duration(), Duration::from_secs(1));
sourcepub fn mode(&self) -> TimerMode
pub fn mode(&self) -> TimerMode
Returns the mode of the timer.
Examples
let mut timer = Timer::from_seconds(1.0, TimerMode::Repeating);
assert_eq!(timer.mode(), TimerMode::Repeating);
sourcepub fn set_mode(&mut self, mode: TimerMode)
pub fn set_mode(&mut self, mode: TimerMode)
Sets the mode of the timer.
Examples
let mut timer = Timer::from_seconds(1.0, TimerMode::Repeating);
timer.set_mode(TimerMode::Once);
assert_eq!(timer.mode(), TimerMode::Once);
sourcepub fn tick(&mut self, delta: Duration) -> &Timer
pub fn tick(&mut self, delta: Duration) -> &Timer
Advance the timer by delta
seconds.
Non repeating timer will clamp at duration.
Repeating timer will wrap around.
Will not affect paused timers.
See also Stopwatch::tick
.
Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
let mut repeating = Timer::from_seconds(1.0, TimerMode::Repeating);
timer.tick(Duration::from_secs_f32(1.5));
repeating.tick(Duration::from_secs_f32(1.5));
assert_eq!(timer.elapsed_secs(), 1.0);
assert_eq!(repeating.elapsed_secs(), 0.5);
sourcepub fn pause(&mut self)
pub fn pause(&mut self)
Pauses the Timer. Disables the ticking of the timer.
See also Stopwatch::pause
.
Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
timer.pause();
timer.tick(Duration::from_secs_f32(0.5));
assert_eq!(timer.elapsed_secs(), 0.0);
sourcepub fn unpause(&mut self)
pub fn unpause(&mut self)
Unpauses the Timer. Resumes the ticking of the timer.
See also Stopwatch::unpause()
.
Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
timer.pause();
timer.tick(Duration::from_secs_f32(0.5));
timer.unpause();
timer.tick(Duration::from_secs_f32(0.5));
assert_eq!(timer.elapsed_secs(), 0.5);
sourcepub fn paused(&self) -> bool
pub fn paused(&self) -> bool
Returns true
if the timer is paused.
See also Stopwatch::paused
.
Examples
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
assert!(!timer.paused());
timer.pause();
assert!(timer.paused());
timer.unpause();
assert!(!timer.paused());
sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the timer. The reset doesn’t affect the paused
state of the timer.
See also Stopwatch::reset
.
Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
timer.tick(Duration::from_secs_f32(1.5));
timer.reset();
assert!(!timer.finished());
assert!(!timer.just_finished());
assert_eq!(timer.elapsed_secs(), 0.0);
sourcepub fn fraction(&self) -> f32
pub fn fraction(&self) -> f32
Returns the fraction of the timer elapsed time (goes from 0.0 to 1.0).
Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(2.0, TimerMode::Once);
timer.tick(Duration::from_secs_f32(0.5));
assert_eq!(timer.fraction(), 0.25);
sourcepub fn fraction_remaining(&self) -> f32
pub fn fraction_remaining(&self) -> f32
Returns the fraction of the timer remaining time (goes from 1.0 to 0.0).
Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(2.0, TimerMode::Once);
timer.tick(Duration::from_secs_f32(0.5));
assert_eq!(timer.fraction_remaining(), 0.75);
sourcepub fn remaining_secs(&self) -> f32
pub fn remaining_secs(&self) -> f32
Returns the remaining time in seconds
Examples
use std::cmp::Ordering;
use std::time::Duration;
let mut timer = Timer::from_seconds(2.0, TimerMode::Once);
timer.tick(Duration::from_secs_f32(0.5));
let result = timer.remaining_secs().total_cmp(&1.5);
assert_eq!(Ordering::Equal, result);
sourcepub fn remaining(&self) -> Duration
pub fn remaining(&self) -> Duration
Returns the remaining time using Duration
Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(2.0, TimerMode::Once);
timer.tick(Duration::from_secs_f32(0.5));
assert_eq!(timer.remaining(), Duration::from_secs_f32(1.5));
sourcepub fn times_finished_this_tick(&self) -> u32
pub fn times_finished_this_tick(&self) -> u32
Returns the number of times a repeating timer
finished during the last tick
call.
For non repeating-timers, this method will only ever return 0 or 1.
Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(1.0, TimerMode::Repeating);
timer.tick(Duration::from_secs_f32(6.0));
assert_eq!(timer.times_finished_this_tick(), 6);
timer.tick(Duration::from_secs_f32(2.0));
assert_eq!(timer.times_finished_this_tick(), 2);
timer.tick(Duration::from_secs_f32(0.5));
assert_eq!(timer.times_finished_this_tick(), 0);
Trait Implementations§
source§impl FromReflect for Timerwhere
Timer: Any + Send + Sync,
Stopwatch: FromReflect + TypePath,
Duration: FromReflect + TypePath,
TimerMode: FromReflect + TypePath,
bool: FromReflect + TypePath,
u32: FromReflect + TypePath,
impl FromReflect for Timerwhere
Timer: Any + Send + Sync,
Stopwatch: FromReflect + TypePath,
Duration: FromReflect + TypePath,
TimerMode: FromReflect + TypePath,
bool: FromReflect + TypePath,
u32: FromReflect + TypePath,
source§fn from_reflect(reflect: &(dyn Reflect + 'static)) -> Option<Timer>
fn from_reflect(reflect: &(dyn Reflect + 'static)) -> Option<Timer>
Self
from a reflected value.source§fn take_from_reflect(
reflect: Box<dyn Reflect>
) -> Result<Self, Box<dyn Reflect>>
fn take_from_reflect( reflect: Box<dyn Reflect> ) -> Result<Self, Box<dyn Reflect>>
Self
using,
constructing the value using from_reflect
if that fails. Read moresource§impl GetTypeRegistration for Timerwhere
Timer: Any + Send + Sync,
Stopwatch: FromReflect + TypePath,
Duration: FromReflect + TypePath,
TimerMode: FromReflect + TypePath,
bool: FromReflect + TypePath,
u32: FromReflect + TypePath,
impl GetTypeRegistration for Timerwhere
Timer: Any + Send + Sync,
Stopwatch: FromReflect + TypePath,
Duration: FromReflect + TypePath,
TimerMode: FromReflect + TypePath,
bool: FromReflect + TypePath,
u32: FromReflect + TypePath,
source§impl PartialEq for Timer
impl PartialEq for Timer
source§impl Reflect for Timerwhere
Timer: Any + Send + Sync,
Stopwatch: FromReflect + TypePath,
Duration: FromReflect + TypePath,
TimerMode: FromReflect + TypePath,
bool: FromReflect + TypePath,
u32: FromReflect + TypePath,
impl Reflect for Timerwhere
Timer: Any + Send + Sync,
Stopwatch: FromReflect + TypePath,
Duration: FromReflect + TypePath,
TimerMode: FromReflect + TypePath,
bool: FromReflect + TypePath,
u32: FromReflect + TypePath,
source§fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut dyn Any
.source§fn into_reflect(self: Box<Timer>) -> Box<dyn Reflect>
fn into_reflect(self: Box<Timer>) -> Box<dyn Reflect>
source§fn as_reflect(&self) -> &(dyn Reflect + 'static)
fn as_reflect(&self) -> &(dyn Reflect + 'static)
source§fn as_reflect_mut(&mut self) -> &mut (dyn Reflect + 'static)
fn as_reflect_mut(&mut self) -> &mut (dyn Reflect + 'static)
source§fn clone_value(&self) -> Box<dyn Reflect>
fn clone_value(&self) -> Box<dyn Reflect>
Reflect
trait object. Read moresource§fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>
fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>
source§fn apply(&mut self, value: &(dyn Reflect + 'static))
fn apply(&mut self, value: &(dyn Reflect + 'static))
source§fn reflect_kind(&self) -> ReflectKind
fn reflect_kind(&self) -> ReflectKind
source§fn reflect_ref(&self) -> ReflectRef<'_>
fn reflect_ref(&self) -> ReflectRef<'_>
source§fn reflect_mut(&mut self) -> ReflectMut<'_>
fn reflect_mut(&mut self) -> ReflectMut<'_>
source§fn reflect_owned(self: Box<Timer>) -> ReflectOwned
fn reflect_owned(self: Box<Timer>) -> ReflectOwned
source§fn reflect_partial_eq(&self, value: &(dyn Reflect + 'static)) -> Option<bool>
fn reflect_partial_eq(&self, value: &(dyn Reflect + 'static)) -> Option<bool>
source§fn reflect_hash(&self) -> Option<u64>
fn reflect_hash(&self) -> Option<u64>
source§fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
source§fn serializable(&self) -> Option<Serializable<'_>>
fn serializable(&self) -> Option<Serializable<'_>>
source§fn is_dynamic(&self) -> bool
fn is_dynamic(&self) -> bool
source§impl Struct for Timerwhere
Timer: Any + Send + Sync,
Stopwatch: FromReflect + TypePath,
Duration: FromReflect + TypePath,
TimerMode: FromReflect + TypePath,
bool: FromReflect + TypePath,
u32: FromReflect + TypePath,
impl Struct for Timerwhere
Timer: Any + Send + Sync,
Stopwatch: FromReflect + TypePath,
Duration: FromReflect + TypePath,
TimerMode: FromReflect + TypePath,
bool: FromReflect + TypePath,
u32: FromReflect + TypePath,
source§fn field(&self, name: &str) -> Option<&(dyn Reflect + 'static)>
fn field(&self, name: &str) -> Option<&(dyn Reflect + 'static)>
name
as a &dyn Reflect
.source§fn field_mut(&mut self, name: &str) -> Option<&mut (dyn Reflect + 'static)>
fn field_mut(&mut self, name: &str) -> Option<&mut (dyn Reflect + 'static)>
name
as a
&mut dyn Reflect
.source§fn field_at(&self, index: usize) -> Option<&(dyn Reflect + 'static)>
fn field_at(&self, index: usize) -> Option<&(dyn Reflect + 'static)>
index
as a
&dyn Reflect
.source§fn field_at_mut(&mut self, index: usize) -> Option<&mut (dyn Reflect + 'static)>
fn field_at_mut(&mut self, index: usize) -> Option<&mut (dyn Reflect + 'static)>
index
as a &mut dyn Reflect
.source§fn name_at(&self, index: usize) -> Option<&str>
fn name_at(&self, index: usize) -> Option<&str>
index
.source§fn iter_fields(&self) -> FieldIter<'_> ⓘ
fn iter_fields(&self) -> FieldIter<'_> ⓘ
source§fn clone_dynamic(&self) -> DynamicStruct
fn clone_dynamic(&self) -> DynamicStruct
DynamicStruct
.source§impl TypePath for Timer
impl TypePath for Timer
source§fn type_path() -> &'static str
fn type_path() -> &'static str
source§fn short_type_path() -> &'static str
fn short_type_path() -> &'static str
source§fn type_ident() -> Option<&'static str>
fn type_ident() -> Option<&'static str>
source§fn crate_name() -> Option<&'static str>
fn crate_name() -> Option<&'static str>
source§impl Typed for Timerwhere
Timer: Any + Send + Sync,
Stopwatch: FromReflect + TypePath,
Duration: FromReflect + TypePath,
TimerMode: FromReflect + TypePath,
bool: FromReflect + TypePath,
u32: FromReflect + TypePath,
impl Typed for Timerwhere
Timer: Any + Send + Sync,
Stopwatch: FromReflect + TypePath,
Duration: FromReflect + TypePath,
TimerMode: FromReflect + TypePath,
bool: FromReflect + TypePath,
u32: FromReflect + TypePath,
impl Eq for Timer
impl StructuralEq for Timer
impl StructuralPartialEq for Timer
Auto Trait Implementations§
impl RefUnwindSafe for Timer
impl Send for Timer
impl Sync for Timer
impl Unpin for Timer
impl UnwindSafe for Timer
Blanket Implementations§
source§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<Image>) -> U
T
ShaderType
for self
. When used in AsBindGroup
derives, it is safe to assume that all images in self
exist.source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
source§impl<T> DynamicTypePath for Twhere
T: TypePath,
impl<T> DynamicTypePath for Twhere
T: TypePath,
source§fn reflect_type_path(&self) -> &str
fn reflect_type_path(&self) -> &str
TypePath::type_path
.source§fn reflect_short_type_path(&self) -> &str
fn reflect_short_type_path(&self) -> &str
source§fn reflect_type_ident(&self) -> Option<&str>
fn reflect_type_ident(&self) -> Option<&str>
TypePath::type_ident
.source§fn reflect_crate_name(&self) -> Option<&str>
fn reflect_crate_name(&self) -> Option<&str>
TypePath::crate_name
.source§fn reflect_module_path(&self) -> Option<&str>
fn reflect_module_path(&self) -> Option<&str>
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self
using data from the given World
.source§impl<S> GetField for Swhere
S: Struct,
impl<S> GetField for Swhere
S: Struct,
source§impl<T> GetPath for T
impl<T> GetPath for T
source§fn reflect_path<'p>(
&self,
path: impl ReflectPath<'p>
) -> Result<&(dyn Reflect + 'static), ReflectPathError<'p>>
fn reflect_path<'p>( &self, path: impl ReflectPath<'p> ) -> Result<&(dyn Reflect + 'static), ReflectPathError<'p>>
path
. Read moresource§fn reflect_path_mut<'p>(
&mut self,
path: impl ReflectPath<'p>
) -> Result<&mut (dyn Reflect + 'static), ReflectPathError<'p>>
fn reflect_path_mut<'p>( &mut self, path: impl ReflectPath<'p> ) -> Result<&mut (dyn Reflect + 'static), ReflectPathError<'p>>
path
. Read moresource§fn path<'p, T>(
&self,
path: impl ReflectPath<'p>
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
fn path<'p, T>(
&self,
path: impl ReflectPath<'p>
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
path
. Read moresource§fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
path
. Read more