Skip to main content

Time

Struct Time 

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

核心时间资源,跟踪应用的时间信息

Time 提供了游戏开发中必需的时间信息,包括帧间隔时间(delta time)、 总运行时间和帧计数。它是帧率无关游戏逻辑的基础。

§线程安全

Time 实现了 SendSync,可以安全地在多线程环境中使用。

§示例

use anvilkit_core::time::Time;
use std::time::Duration;
 
let mut time = Time::new();
 
// 模拟游戏循环
loop {
    time.update();
     
    // 使用 delta time 进行帧率无关的移动
    let movement_speed = 100.0; // 单位/秒
    let distance = movement_speed * time.delta_seconds();
     
    println!("FPS: {:.1}", time.fps());
     
    // 游戏逻辑...
     
    std::thread::sleep(Duration::from_millis(16)); // ~60 FPS
     
    if time.frame_count() > 100 {
        break;
    }
}

Implementations§

Source§

impl Time

Source

pub fn new() -> Time

创建新的时间资源

初始化时间资源,记录创建时的时间点作为应用启动时间。

§示例
use anvilkit_core::time::Time;
 
let time = Time::new();
assert_eq!(time.frame_count(), 0);
assert_eq!(time.delta_seconds(), 0.0);
Source

pub fn update(&mut self)

更新时间信息

应该在每帧开始时调用此方法来更新时间信息。 这会更新 delta time、elapsed time 和 frame count。

§示例
use anvilkit_core::time::Time;
use std::time::Duration;

let mut time = Time::new();

// 第一次更新初始化时间
time.update();
assert_eq!(time.frame_count(), 1);

// 模拟时间流逝
std::thread::sleep(Duration::from_millis(16));
time.update();

assert!(time.delta_seconds() > 0.0);
assert_eq!(time.frame_count(), 2);
Source

pub fn delta(&self) -> Duration

获取上一帧到当前帧的时间间隔

Delta time 是实现帧率无关游戏逻辑的关键。

§示例
use anvilkit_core::time::Time;
 
let mut time = Time::new();
time.update();
 
let delta = time.delta();
println!("Frame time: {:?}", delta);
Source

pub fn delta_seconds(&self) -> f32

获取 delta time 的秒数表示(f32)

这是最常用的 delta time 获取方法,适用于大多数游戏逻辑计算。

§示例
use anvilkit_core::time::Time;
 
let mut time = Time::new();
time.update();
 
let speed = 100.0; // 单位/秒
let distance = speed * time.delta_seconds();
Source

pub fn delta_seconds_f64(&self) -> f64

获取 delta time 的秒数表示(f64)

提供更高精度的 delta time,适用于需要高精度计算的场景。

Source

pub fn delta_millis(&self) -> u128

获取 delta time 的毫秒数表示

§示例
use anvilkit_core::time::Time;
 
let mut time = Time::new();
time.update();
 
println!("Frame time: {}ms", time.delta_millis());
Source

pub fn elapsed(&self) -> Duration

获取应用启动以来的总时间

§示例
use anvilkit_core::time::Time;
use std::time::Duration;
 
let mut time = Time::new();
std::thread::sleep(Duration::from_millis(100));
time.update();
 
assert!(time.elapsed().as_millis() >= 100);
Source

pub fn elapsed_seconds(&self) -> f32

获取总运行时间的秒数表示(f32)

Source

pub fn elapsed_seconds_f64(&self) -> f64

获取总运行时间的秒数表示(f64)

Source

pub fn frame_count(&self) -> u64

获取总帧数

§示例
use anvilkit_core::time::Time;
 
let mut time = Time::new();
assert_eq!(time.frame_count(), 0);
 
time.update();
assert_eq!(time.frame_count(), 1);
Source

pub fn fps(&self) -> f64

获取平均帧率(基于总运行时间)

计算从应用启动到现在的平均 FPS。

§示例
use anvilkit_core::time::Time;
use std::time::Duration;
 
let mut time = Time::new();
 
// 模拟多帧
for _ in 0..10 {
    std::thread::sleep(Duration::from_millis(16));
    time.update();
}
 
let fps = time.fps();
println!("Average FPS: {:.1}", fps);
Source

pub fn instant_fps(&self) -> f64

获取瞬时帧率(基于当前 delta time)

计算基于当前帧时间的瞬时 FPS,可能会有较大波动。

§示例
use anvilkit_core::time::Time;
 
let mut time = Time::new();
time.update();
 
let instant_fps = time.instant_fps();
println!("Instant FPS: {:.1}", instant_fps);
Source

pub fn startup_time(&self) -> Instant

获取应用启动时间点

返回应用启动时的 Instant,可用于计算绝对时间间隔。

Source

pub fn current_time(&self) -> Instant

获取当前时间点

返回最后一次调用 update() 时的时间点。

Source

pub fn is_first_frame(&self) -> bool

检查是否是第一帧

在某些初始化逻辑中可能需要知道是否是第一帧。

§示例
use anvilkit_core::time::Time;
 
let mut time = Time::new();
assert!(time.is_first_frame());
 
time.update();
assert!(!time.is_first_frame());
Source

pub fn reset(&mut self)

重置时间资源

将时间资源重置到初始状态,就像刚创建一样。 这在场景切换或游戏重启时可能有用。

§示例
use anvilkit_core::time::Time;
 
let mut time = Time::new();
time.update();
 
assert_eq!(time.frame_count(), 1);
 
time.reset();
assert_eq!(time.frame_count(), 0);
Source

pub fn with_scale(&self, scale: f32) -> ScaledTime

设置时间缩放因子

注意:这个方法返回一个新的 ScaledTime 包装器,而不是修改当前实例。

§参数
  • scale: 时间缩放因子,1.0 为正常速度,0.5 为半速,2.0 为双速
§示例
use anvilkit_core::time::Time;

let time = Time::new();
let slow_time = time.with_scale(0.5); // 半速

assert_eq!(slow_time.scale(), 0.5);

Trait Implementations§

Source§

impl Clone for Time

Source§

fn clone(&self) -> Time

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 Time

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Time

Source§

fn default() -> Time

Returns the “default value” for a type. Read more
Source§

impl Resource for Time
where Time: Send + Sync + 'static,

Auto Trait Implementations§

§

impl Freeze for Time

§

impl RefUnwindSafe for Time

§

impl Send for Time

§

impl Sync for Time

§

impl Unpin for Time

§

impl UnsafeUnpin for Time

§

impl UnwindSafe for Time

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> FromWorld for T
where T: Default,

Source§

fn from_world(_world: &mut World) -> T

Creates Self using data from the given World.
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,