#[repr(C)]pub struct Time { /* private fields */ }
Expand description
A clock that tracks how much it has advanced (and how much real time has elapsed) since its previous update and since its creation.
Implementations§
source§impl Time
impl Time
sourcepub fn new(startup: Instant) -> Self
pub fn new(startup: Instant) -> Self
Constructs a new Time
instance with a specific startup Instant
.
sourcepub fn update(&mut self)
pub fn update(&mut self)
Updates the internal time measurements.
Calling this method as part of your app will most likely result in inaccurate timekeeping,
as the Time
resource is ordinarily managed by the bones rendering backend.
sourcepub fn update_with_instant(&mut self, instant: Instant)
pub fn update_with_instant(&mut self, instant: Instant)
Updates time with a specified Instant
.
This method is provided for use in tests. Calling this method as part of your app will most
likely result in inaccurate timekeeping, as the Time
resource is ordinarily managed by
whatever bones renderer you are using.
§Examples
#[derive(Resource)]
struct Health {
// Health value between 0.0 and 1.0
health_value: f32,
}
fn health_system(time: Res<Time>, mut health: ResMut<Health>) {
// Increase health value by 0.1 per second, independent of frame rate,
// but not beyond 1.0
health.health_value = (health.health_value + 0.1 * time.delta_seconds()).min(1.0);
}
// Mock time in tests
fn test_health_system() {
let mut world = World::default();
let mut time = Time::default();
time.update();
world.insert_resource(time);
world.insert_resource(Health { health_value: 0.2 });
let mut schedule = Schedule::new();
schedule.add_system(health_system);
// Simulate that 30 ms have passed
let mut time = world.resource_mut::<Time>();
let last_update = time.last_update().unwrap();
time.update_with_instant(last_update + Duration::from_millis(30));
// Run system
schedule.run(&mut world);
// Check that 0.003 has been added to the health value
let expected_health_value = 0.2 + 0.1 * 0.03;
let actual_health_value = world.resource::<Health>().health_value;
assert_eq!(expected_health_value, actual_health_value);
}
sourcepub fn advance_exact(&mut self, duration: Duration)
pub fn advance_exact(&mut self, duration: Duration)
Advance the time exactly by the given duration.
This is useful when ticking the time exactly by a fixed timestep.
sourcepub fn delta_seconds(&self) -> f32
pub fn delta_seconds(&self) -> f32
sourcepub fn delta_seconds_f64(&self) -> f64
pub fn delta_seconds_f64(&self) -> f64
sourcepub fn elapsed_seconds(&self) -> f32
pub fn elapsed_seconds(&self) -> f32
Returns how much time has advanced since startup
, as f32
seconds.
Note: This is a monotonically increasing value. It’s precision will degrade over time.
If you need an f32
but that precision loss is unacceptable,
use elapsed_seconds_wrapped
.
sourcepub fn elapsed_seconds_f64(&self) -> f64
pub fn elapsed_seconds_f64(&self) -> f64
Trait Implementations§
source§impl HasSchema for Time
impl HasSchema for Time
§fn register_schema()
fn register_schema()
§fn cast<T>(this: &Self) -> &Twhere
T: HasSchema,
fn cast<T>(this: &Self) -> &Twhere
T: HasSchema,
§fn try_cast<T>(this: &Self) -> Result<&T, SchemaMismatchError>where
T: HasSchema,
fn try_cast<T>(this: &Self) -> Result<&T, SchemaMismatchError>where
T: HasSchema,
§fn cast_mut<T>(this: &mut Self) -> &mut Twhere
T: HasSchema,
fn cast_mut<T>(this: &mut Self) -> &mut Twhere
T: HasSchema,
§fn try_cast_mut<T>(this: &mut Self) -> Result<&mut T, SchemaMismatchError>where
T: HasSchema,
fn try_cast_mut<T>(this: &mut Self) -> Result<&mut T, SchemaMismatchError>where
T: HasSchema,
§fn as_schema_ref(&self) -> SchemaRef<'_>where
Self: Sized,
fn as_schema_ref(&self) -> SchemaRef<'_>where
Self: Sized,
T
to a SchemaRef
§fn as_schema_mut(&mut self) -> SchemaRefMut<'_>where
Self: Sized,
fn as_schema_mut(&mut self) -> SchemaRefMut<'_>where
Self: Sized,
T
to a SchemaRefMut
impl Copy 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 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
source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
§fn from_world(_world: &World) -> T
fn from_world(_world: &World) -> T
Self
using data from the given World
.