mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-11 11:04:05 +09:00
game-maker selectable game update governor
This commit is contained in:
53
src/net/torvald/terrarum/GameUpdateGovernor.kt
Normal file
53
src/net/torvald/terrarum/GameUpdateGovernor.kt
Normal file
@@ -0,0 +1,53 @@
|
||||
package net.torvald.terrarum
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2022-12-25.
|
||||
*/
|
||||
interface GameUpdateGovernor {
|
||||
/**
|
||||
* @param tickInterval reciprocal of "tick rate" or "frame rate", depending on the type of the governor
|
||||
* @param updateFunction what to do on `update`. Takes one argument that is delta-time
|
||||
* @param renderFunction what to do on `render`. Takes one argument that is delta-time
|
||||
*/
|
||||
fun update(deltaTime: Float, tickInterval: Second, updateFunction: (Float) -> Unit, renderFunction: (Float) -> Unit)
|
||||
fun reset()
|
||||
}
|
||||
|
||||
object Anarchy : GameUpdateGovernor {
|
||||
override fun update(
|
||||
deltaTime: Float,
|
||||
tickInterval: Second,
|
||||
updateFunction: (Float) -> Unit,
|
||||
renderFunction: (Float) -> Unit
|
||||
) {
|
||||
updateFunction(deltaTime)
|
||||
renderFunction(deltaTime)
|
||||
}
|
||||
|
||||
override fun reset() {
|
||||
}
|
||||
}
|
||||
|
||||
object LimitUpdateRate : GameUpdateGovernor {
|
||||
|
||||
private var akku = 0f
|
||||
|
||||
override fun update(deltaTime: Float, tickInterval: Second, updateFunction: (Float) -> Unit, renderFunction: (Float) -> Unit) {
|
||||
akku += deltaTime
|
||||
|
||||
var i = 0L
|
||||
while (akku >= tickInterval) {
|
||||
App.measureDebugTime("Ingame.Update") { updateFunction(tickInterval) }
|
||||
akku -= tickInterval
|
||||
i += 1
|
||||
}
|
||||
App.setDebugTime("Ingame.UpdateCounter", i)
|
||||
|
||||
/** RENDER CODE GOES HERE */
|
||||
App.measureDebugTime("Ingame.Render") { renderFunction(tickInterval) }
|
||||
}
|
||||
|
||||
override fun reset() {
|
||||
akku = 0f
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user