Skip to main content

Timer

Struct Timer 

Source
pub struct Timer { /* private fields */ }
Expand description

灵活的计时器工具

Timer 提供了丰富的计时功能,支持一次性和重复计时、暂停恢复等操作。 它是实现时间相关游戏逻辑的核心工具。

§设计特点

  • 零分配: 所有操作都不涉及内存分配
  • 高精度: 使用 Duration 提供微秒级精度
  • 状态管理: 清晰的状态转换和查询接口
  • 灵活配置: 支持多种创建和配置方式

§示例

use anvilkit_core::time::Timer;
use std::time::Duration;
 
// 创建 3 秒一次性计时器
let mut timer = Timer::from_seconds(3.0);
 
// 创建 1 秒重复计时器
let mut repeat_timer = Timer::repeating_from_seconds(1.0);
 
// 在游戏循环中更新
let delta = Duration::from_millis(16); // ~60 FPS
 
timer.tick(delta);
repeat_timer.tick(delta);
 
if timer.just_finished() {
    println!("Timer finished!");
}
 
if repeat_timer.just_finished() {
    println!("Repeat timer triggered!");
}

Implementations§

Source§

impl Timer

Source

pub fn new(duration: Duration) -> Timer

创建新的一次性计时器

§参数
  • duration: 计时时长
§示例
use anvilkit_core::time::Timer;
use std::time::Duration;
 
let timer = Timer::new(Duration::from_secs(5));
assert_eq!(timer.duration(), Duration::from_secs(5));
assert!(!timer.is_repeating());
Source

pub fn repeating(duration: Duration) -> Timer

创建新的重复计时器

§参数
  • duration: 每次计时的时长
§示例
use anvilkit_core::time::Timer;
use std::time::Duration;
 
let timer = Timer::repeating(Duration::from_secs(2));
assert!(timer.is_repeating());
Source

pub fn from_seconds(seconds: f32) -> Timer

从秒数创建一次性计时器

§示例
use anvilkit_core::time::Timer;
 
let timer = Timer::from_seconds(3.5);
assert_eq!(timer.duration_seconds(), 3.5);
Source

pub fn repeating_from_seconds(seconds: f32) -> Timer

从秒数创建重复计时器

§示例
use anvilkit_core::time::Timer;
 
let timer = Timer::repeating_from_seconds(1.0);
assert!(timer.is_repeating());
assert_eq!(timer.duration_seconds(), 1.0);
Source

pub fn from_millis(millis: u64) -> Timer

从毫秒数创建一次性计时器

Source

pub fn repeating_from_millis(millis: u64) -> Timer

从毫秒数创建重复计时器

Source

pub fn tick(&mut self, delta: Duration)

更新计时器

应该在每帧调用此方法来推进计时器。

§参数
  • delta: 自上次更新以来的时间间隔
§示例
use anvilkit_core::time::Timer;
use std::time::Duration;
 
let mut timer = Timer::from_seconds(1.0);
 
// 模拟 0.5 秒
timer.tick(Duration::from_millis(500));
assert_eq!(timer.percent(), 0.5);
assert!(!timer.finished());
 
// 再模拟 0.5 秒
timer.tick(Duration::from_millis(500));
assert!(timer.finished());
assert!(timer.just_finished());
Source

pub fn finished(&self) -> bool

检查计时器是否已完成

对于一次性计时器,完成后会一直返回 true。 对于重复计时器,只有在刚完成的那一帧返回 true

§示例
use anvilkit_core::time::Timer;
use std::time::Duration;
 
let mut timer = Timer::from_seconds(1.0);
assert!(!timer.finished());
 
timer.tick(Duration::from_secs(1));
assert!(timer.finished());
Source

pub fn just_finished(&self) -> bool

检查计时器是否在本帧刚完成

这对于触发一次性事件非常有用,避免重复触发。

§示例
use anvilkit_core::time::Timer;
use std::time::Duration;
 
let mut timer = Timer::from_seconds(1.0);
 
timer.tick(Duration::from_millis(999));
assert!(!timer.just_finished());
 
timer.tick(Duration::from_millis(1));
assert!(timer.just_finished());
 
timer.tick(Duration::from_millis(1));
assert!(!timer.just_finished()); // 下一帧不再是 "刚完成"
Source

pub fn elapsed(&self) -> Duration

获取已经过的时间

Source

pub fn elapsed_seconds(&self) -> f32

获取已经过的时间(秒)

Source

pub fn duration(&self) -> Duration

获取计时器总时长

Source

pub fn duration_seconds(&self) -> f32

获取计时器总时长(秒)

Source

pub fn percent(&self) -> f32

获取完成百分比 (0.0 到 1.0)

§示例
use anvilkit_core::time::Timer;
use std::time::Duration;
 
let mut timer = Timer::from_seconds(2.0);
timer.tick(Duration::from_secs(1));
 
assert_eq!(timer.percent(), 0.5);
Source

pub fn remaining(&self) -> Duration

获取剩余时间

§示例
use anvilkit_core::time::Timer;
use std::time::Duration;
 
let mut timer = Timer::from_seconds(3.0);
timer.tick(Duration::from_secs(1));
 
assert_eq!(timer.remaining(), Duration::from_secs(2));
Source

pub fn remaining_seconds(&self) -> f32

获取剩余时间(秒)

Source

pub fn reset(&mut self)

重置计时器到初始状态

§示例
use anvilkit_core::time::Timer;
use std::time::Duration;
 
let mut timer = Timer::from_seconds(1.0);
timer.tick(Duration::from_millis(500));
 
assert_eq!(timer.percent(), 0.5);
 
timer.reset();
assert_eq!(timer.percent(), 0.0);
assert!(!timer.finished());
Source

pub fn pause(&mut self)

暂停计时器

§示例
use anvilkit_core::time::Timer;
use std::time::Duration;
 
let mut timer = Timer::from_seconds(1.0);
timer.pause();
 
assert!(timer.is_paused());
 
// 暂停状态下 tick 不会推进时间
timer.tick(Duration::from_secs(1));
assert_eq!(timer.elapsed_seconds(), 0.0);
Source

pub fn resume(&mut self)

恢复计时器

§示例
use anvilkit_core::time::Timer;
use std::time::Duration;
 
let mut timer = Timer::from_seconds(1.0);
timer.pause();
timer.resume();
 
assert!(timer.is_running());
Source

pub fn set_duration(&mut self, duration: Duration)

设置计时器时长

§注意

如果新时长小于已经过的时间,计时器会立即完成。

§示例
use anvilkit_core::time::Timer;
use std::time::Duration;
 
let mut timer = Timer::from_seconds(1.0);
timer.set_duration(Duration::from_secs(2));
 
assert_eq!(timer.duration_seconds(), 2.0);
Source

pub fn set_repeating(&mut self, repeating: bool)

设置是否为重复计时器

§示例
use anvilkit_core::time::Timer;
 
let mut timer = Timer::from_seconds(1.0);
assert!(!timer.is_repeating());
 
timer.set_repeating(true);
assert!(timer.is_repeating());
Source

pub fn is_repeating(&self) -> bool

检查是否为重复计时器

Source

pub fn is_running(&self) -> bool

检查计时器是否正在运行

Source

pub fn is_paused(&self) -> bool

检查计时器是否已暂停

Source

pub fn state(&self) -> TimerState

获取计时器状态

Source

pub fn finish(&mut self)

强制完成计时器

将已经过时间设置为总时长,并触发完成状态。

§示例
use anvilkit_core::time::Timer;
 
let mut timer = Timer::from_seconds(10.0);
timer.finish();
 
assert!(timer.finished());
assert!(timer.just_finished());

Trait Implementations§

Source§

impl Clone for Timer

Source§

fn clone(&self) -> Timer

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Timer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Timer

§

impl RefUnwindSafe for Timer

§

impl Send for Timer

§

impl Sync for Timer

§

impl Unpin for Timer

§

impl UnsafeUnpin for Timer

§

impl UnwindSafe for Timer

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &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
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> TypeData for T
where T: 'static + Send + Sync + Clone,

Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ConditionalSend for T
where T: Send,

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,