Takt und Bewegung der Figur
TickClock rechnet Bildzeit in Takte um und holt nach einem Ruckler höchstens fünf Takte auf. GridMotion bewegt einen Körper über das Kachelraster und lässt ihn an Wänden entlanggleiten. Die Figur läuft fünf Felder pro Sekunde, außerhalb des Gleichschritts. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
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
|
||||
Reference in New Issue
Block a user