Add discard_overstep function to Time<Fixed> (#10453)
# Objective There is no easy way to discard some amount for `Time<Fixed>`'s overstep. This can be useful for online games when the client receives information about a tick (which happens when you get a FPS drop or the ping changes for example) it has not yet processed, it can discard overstep equal to the number of ticks it can jump ahead. Currently the workaround would be to create a new `Time<Fixed>` copy the old timestep, advance it by the overstep amount that would remain after subtracting the discarded amount, and using `.context_mut()` to overwrite the old context with the new one. If you overwrite the whole `Time<Fixed>` or forget to copy over the timestep you can introduce undesirable side effects. ## Solution Introduce a `discard_overstep` method, which discards the provided amount of overstep. It uses satuarting_sub to avoid errors (negative `Duration`s do not exist). --- ## Changelog - Added `discard_overstep` function to `Time<Fixed>` --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
This commit is contained in:
parent
7ee9f8e392
commit
1d3ae677df
@ -175,6 +175,15 @@ impl Time<Fixed> {
|
||||
self.context().overstep
|
||||
}
|
||||
|
||||
/// Discard a part of the overstep amount.
|
||||
///
|
||||
/// If `discard` is higher than overstep, the overstep becomes zero.
|
||||
#[inline]
|
||||
pub fn discard_overstep(&mut self, discard: Duration) {
|
||||
let context = self.context_mut();
|
||||
context.overstep = context.overstep.saturating_sub(discard);
|
||||
}
|
||||
|
||||
/// Returns the amount of overstep time accumulated toward new steps, as an
|
||||
/// [`f32`] fraction of the timestep.
|
||||
#[inline]
|
||||
|
||||
Loading…
Reference in New Issue
Block a user