class_name TickClock extends RefCounted ## Turns frame time into a whole number of simulation ticks. ## Most ticks one frame may run. After a long stall the simulation catches ## up only this far and drops the rest, instead of freezing to catch up. const MAX_TICKS_PER_FRAME := 5 var seconds_per_tick: float var paused := false var _accumulated := 0.0 func _init(ticks_per_second: int) -> void: seconds_per_tick = 1.0 / ticks_per_second ## Returns how many ticks to run for a frame that took `delta` seconds. func advance(delta: float) -> int: if paused: return 0 _accumulated += delta var ticks := int(_accumulated / seconds_per_tick) if ticks > MAX_TICKS_PER_FRAME: _accumulated = 0.0 return MAX_TICKS_PER_FRAME _accumulated -= ticks * seconds_per_tick return ticks