pub struct Timer(pub Rc<RefCell<TimerData>>);Expand description
§Timer
A timer that counts down each frame. Can be paused and extended. Actions can be boundd to the timer that will run on completion.
Don’t put the timer in the loop
Timers won’t run by default, you must call add_timer() and pass in the timer
to add them to the timer list. Or call spawn_timer(seconds) and it will run immediately.
let mut my_timer = add_timer(Timer::new(5.0));
// or
let mut my_timer = spawn_timer(5.0);println!("Time left: {}", my_timer.time_left);
println!("Timer is paused: {}", my_timer.paused);my_timer.extend(2.0); // extend by 2 seconds
if my_timer.completed() {
// timer is completed
}
if my_timer.ongoing() {
// timer is active
}
// Pause/Unpause a timer
my_timer.pause();
my_timer.unpause();
my_timer.set_paused(true);
my_timer.toggle_paused();
// Action will run if the timer is completed (must be called each frame)
my_timer.on_completion(|| {
println!("Timer was completed!");
})
// Reset the time left to the original (does not include `extend()`)
my_timer.reset();
// Stop the timer (marks as completed())
my_timer.finish();Once a timer is completed it will be removed from the timer list.
The timer will stop ticking and on_completion() will stop firing.
// Re-add a timer to the timer list (REMEMBER TO RESET IT)
my_timer.reset();
add_timer(my_timer);Tuple Fields§
§0: Rc<RefCell<TimerData>>Implementations§
Source§impl Timer
impl Timer
Sourcepub fn new_repeating(seconds: f32) -> Self
pub fn new_repeating(seconds: f32) -> Self
Create a new repeating timer using seconds. Restarts after completing
Sourcepub fn tick(&mut self, dt: f32)
pub fn tick(&mut self, dt: f32)
Tick down the time left by dt (deltatime) only if it’s unpaused
Sourcepub fn finish(&self)
pub fn finish(&self)
Sets time left to zero (completed) which may also remove the timer from the timer list
Sourcepub fn set_paused(&self, paused: bool)
pub fn set_paused(&self, paused: bool)
Set whether timer is paused or not
Sourcepub fn toggle_paused(&self)
pub fn toggle_paused(&self)
Toggle the timer paused/unpaused
Sourcepub fn on_completion(&self, action: impl FnMut())
pub fn on_completion(&self, action: impl FnMut())
Runs an action if timer is completed (only runs if timer is in timer list)
Sourcepub fn completed_and_restart(&self) -> bool
pub fn completed_and_restart(&self) -> bool
Restarts the timer if it just got completed this frame,
also returns whether its completed() or not
pub async fn wait(&self)
Trait Implementations§
Source§impl PartialOrd for Timer
impl PartialOrd for Timer
impl StructuralPartialEq for Timer
Auto Trait Implementations§
impl !RefUnwindSafe for Timer
impl !Send for Timer
impl !Sync for Timer
impl !UnwindSafe for Timer
impl Freeze for Timer
impl Unpin for Timer
impl UnsafeUnpin for Timer
Blanket Implementations§
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more