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
impl TimeTick
Sourcepub const fn new(
now: Millis,
tick_duration: MillisDuration,
max_tick_per_update: u16,
) -> Self
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.
Sourcepub fn set_tick_duration(&mut self, tick_duration: MillisDuration)
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.
Sourcepub fn reset(&mut self, now: Millis)
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.
Sourcepub fn calculate_ticks(&mut self, now: Millis) -> u16
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.
Sourcepub fn performed_ticks(&mut self, tick_count: u16)
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 bycalculate_ticks
, but can be lower in certain cases.