Struct TimeTick

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

A struct to manage and calculate the number of ticks for a game loop based on elapsed time.

The TimeTick struct is designed to track time intervals for game updates, allowing you to specify a duration for each tick and the maximum number of ticks that can occur in a single update. This is particularly useful for games that require consistent timing for game logic updates.

Implementations§

Source§

impl TimeTick

Source

pub const fn new( now: Millis, tick_duration: MillisDuration, max_tick_per_update: u16, ) -> Self

Creates a new TimeTick instance.

§Arguments
  • now - The current absolute time in milliseconds.
  • tick_duration - The duration of each tick in milliseconds.
  • max_tick_per_update - The maximum number of ticks that can be processed in a single update.
§Returns

A new instance of TimeTick initialized with the provided values.

Source

pub fn set_tick_duration(&mut self, tick_duration: MillisDuration)

Sets a new tick duration.

This method allows you to change the duration of each tick dynamically.

§Arguments
  • tick_duration - The new duration for each tick in milliseconds.
Source

pub fn reset(&mut self, now: Millis)

Resets the consumed absolute time to the current time.

This method is useful when you want to restart the timing calculations, for example, when restarting a game or resetting the state.

§Arguments
  • now - The current absolute time in milliseconds to reset to.
Source

pub fn calculate_ticks(&mut self, now: Millis) -> u16

Calculates the number of ticks that should be performed based on the elapsed time since the last update.

This method should be called each frame in the game loop. It computes how many ticks can be processed based on the time that has passed since the last update, clamping the result to the maximum ticks allowed per update.

§Arguments
  • now - The current absolute time in milliseconds.
§Returns

The number of ticks that should be performed this frame.

Source

pub fn performed_ticks(&mut self, tick_count: u16)

Updates the consumed absolute time based on the number of ticks performed.

This method should be called after processing the ticks to adjust the internal timer accordingly. It calculates the new consumed absolute time based on the ticks that have been processed in this update.

In most cases, the value passed to this method should be the same as the number of ticks returned by calculate_ticks. However, it can be a lower value if fewer ticks were actually processed (e.g., due to frame rate limitations or logic constraints). It is important to note that this value must not exceed the number of ticks returned by calculate_ticks.

§Arguments
  • tick_count - The number of ticks that were performed during this update. This value should typically match the value returned by calculate_ticks, but can be lower in certain cases.

Trait Implementations§

Source§

impl Debug for TimeTick

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.