game-maker selectable game update governor

This commit is contained in:
minjaesong
2022-12-25 17:23:05 +09:00
parent 71fd92ad17
commit b5ed33e2e9
6 changed files with 68 additions and 54 deletions

View File

@@ -91,7 +91,7 @@ class AssembledSpriteAnimation(
}
fun renderThisAnimation(batch: SpriteBatch, posX: Float, posY: Float, scale: Float, animName: String) {
val animNameRoot = animName.substring(0, animName.indexOfLast { it == '_' })
val animNameRoot = animName.substring(0, animName.indexOfLast { it == '_' }).ifBlank { return@renderThisAnimation }
val tx = (parentActor.hitboxTranslateX) * scale
val txFlp = -(parentActor.hitboxTranslateX) * scale
@@ -100,7 +100,7 @@ class AssembledSpriteAnimation(
val tyFlp = (parentActor.hitboxTranslateY) * scale
adp.animations[animNameRoot]!!.let { theAnim ->
adp.animations[animNameRoot]?.let { theAnim ->
val skeleton = theAnim.skeleton.joints.reversed()
val transforms = adp.getTransform(animName)
val bodypartOrigins = adp.bodypartJoints
@@ -156,7 +156,7 @@ class AssembledSpriteAnimation(
}
}
}
}
} ?: throw NullPointerException("Animation with name '$animNameRoot' is not found")
}
override fun render(batch: SpriteBatch, posX: Float, posY: Float, scale: Float) {

View 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
}
}

View File

@@ -151,6 +151,8 @@ open class IngameInstance(val batch: FlippingSpriteBatch, val isMultiplayer: Boo
val blockMarkingActor: BlockMarkerActor
get() = CommonResourcePool.get("blockmarking_actor") as BlockMarkerActor
protected lateinit var gameUpdateGovernor: GameUpdateGovernor
override fun hide() {
}

View File

@@ -96,6 +96,8 @@ class BuildingMaker(batch: FlippingSpriteBatch) : IngameInstance(batch) {
gameWorld.worldTime.addTime(WorldTime.DAY_LENGTH * 32)
world = gameWorld
gameUpdateGovernor = LimitUpdateRate.also { it.reset() }
}
@@ -307,27 +309,13 @@ class BuildingMaker(batch: FlippingSpriteBatch) : IngameInstance(batch) {
super.show()
}
private var updateAkku = 0f
override fun render(updateRate: Float) {
Gdx.graphics.setTitle(TerrarumIngame.getCanonicalTitle())
// ASYNCHRONOUS UPDATE AND RENDER //
val dt = Gdx.graphics.deltaTime
updateAkku += dt
var i = 0L
while (updateAkku >= updateRate) {
App.measureDebugTime("Ingame.Update") { updateGame(updateRate) }
updateAkku -= updateRate
i += 1
}
App.setDebugTime("Ingame.UpdateCounter", i)
// render? just do it anyway
App.measureDebugTime("Ingame.Render") { renderGame() }
gameUpdateGovernor.update(Gdx.graphics.deltaTime, App.UPDATE_RATE, { dt -> updateGame(dt) }, { renderGame() })
App.setDebugTime("Ingame.Render - (Light + Tiling)",
((App.debugTimers["Ingame.Render"] as? Long) ?: 0) -
(

View File

@@ -158,6 +158,8 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) {
particlesContainer.overwritingPolicy = {
it.dispose()
}
gameUpdateGovernor = LimitUpdateRate
}
@@ -703,32 +705,22 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) {
postInit()
gameUpdateGovernor.reset()
gameFullyLoaded = true
}
ingameController.update()
// define custom update rate
val updateRate = App.UPDATE_RATE // if (KeyToggler.isOn(Input.Keys.APOSTROPHE)) 1f / 8f else App.UPDATE_RATE
// ASYNCHRONOUS UPDATE AND RENDER //
/** UPDATE CODE GOES HERE */
val dt = Gdx.graphics.deltaTime
updateAkku += dt
autosaveTimer += dt
var i = 0L
while (updateAkku >= updateRate) {
measureDebugTime("Ingame.Update") { updateGame(updateRate) }
updateAkku -= updateRate
i += 1
}
setDebugTime("Ingame.UpdateCounter", i)
gameUpdateGovernor.update(dt, App.UPDATE_RATE, { dt -> updateGame(dt) }, { renderGame() })
/** RENDER CODE GOES HERE */
measureDebugTime("Ingame.Render") { renderGame() }
val autosaveInterval = App.getConfigInt("autosaveinterval").coerceAtLeast(60000) / 1000f
if (autosaveTimer >= autosaveInterval) {

View File

@@ -126,6 +126,7 @@ class TitleScreen(batch: FlippingSpriteBatch) : IngameInstance(batch) {
init {
warning32bitJavaIcon.flip(false, false)
gameUpdateGovernor = LimitUpdateRate.also { it.reset() }
}
private fun loadThingsWhileIntroIsVisible() {
@@ -246,32 +247,10 @@ class TitleScreen(batch: FlippingSpriteBatch) : IngameInstance(batch) {
private val introUncoverTime: Second = 0.3f
private var introUncoverDeltaCounter = 0f
private var updateAkku = 0f
private var fucklatch = false
override fun render(updateRate: Float) {
if (!fucklatch) {
printdbg(this, "render start")
fucklatch = true
}
// async update and render
val dt = Gdx.graphics.deltaTime
updateAkku += dt
var i = 0L
while (updateAkku >= updateRate) {
App.measureDebugTime("Ingame.Update") { updateScreen(updateRate) }
updateAkku -= updateRate
i += 1
}
App.setDebugTime("Ingame.UpdateCounter", i)
// render? just do it anyway
App.measureDebugTime("Ingame.Render") { renderScreen() }
gameUpdateGovernor.update(Gdx.graphics.deltaTime, App.UPDATE_RATE, { dt -> updateScreen(dt) }, { renderScreen() })
}
fun updateScreen(delta: Float) {