pub struct Time { /* private fields */ }Expand description
核心时间资源,跟踪应用的时间信息
Time 提供了游戏开发中必需的时间信息,包括帧间隔时间(delta time)、
总运行时间和帧计数。它是帧率无关游戏逻辑的基础。
§线程安全
Time 实现了 Send 和 Sync,可以安全地在多线程环境中使用。
§示例
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
impl Time
Sourcepub fn new() -> Time
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);Sourcepub fn update(&mut self)
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);Sourcepub fn delta(&self) -> Duration
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);Sourcepub fn delta_seconds(&self) -> f32
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();Sourcepub fn delta_seconds_f64(&self) -> f64
pub fn delta_seconds_f64(&self) -> f64
获取 delta time 的秒数表示(f64)
提供更高精度的 delta time,适用于需要高精度计算的场景。
Sourcepub fn delta_millis(&self) -> u128
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());Sourcepub fn elapsed(&self) -> Duration
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);Sourcepub fn elapsed_seconds(&self) -> f32
pub fn elapsed_seconds(&self) -> f32
获取总运行时间的秒数表示(f32)
Sourcepub fn elapsed_seconds_f64(&self) -> f64
pub fn elapsed_seconds_f64(&self) -> f64
获取总运行时间的秒数表示(f64)
Sourcepub fn frame_count(&self) -> u64
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);Sourcepub fn fps(&self) -> f64
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);Sourcepub fn instant_fps(&self) -> f64
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);Sourcepub fn startup_time(&self) -> Instant
pub fn startup_time(&self) -> Instant
获取应用启动时间点
返回应用启动时的 Instant,可用于计算绝对时间间隔。
Sourcepub fn current_time(&self) -> Instant
pub fn current_time(&self) -> Instant
获取当前时间点
返回最后一次调用 update() 时的时间点。
Sourcepub fn is_first_frame(&self) -> bool
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());Sourcepub fn reset(&mut self)
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);Sourcepub fn with_scale(&self, scale: f32) -> ScaledTime
pub fn with_scale(&self, scale: f32) -> ScaledTime
Trait Implementations§
impl Resource for Time
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> 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
Mutably borrows from an owned value. Read more
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>
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>
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)
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)
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
impl<T> DowncastSync for T
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
Creates
Self using data from the given World.