fixed a bug where player input can be ignored when framerate is significantly faster than update rate, which causes gameupdate to be called less often

This commit is contained in:
minjaesong
2021-08-26 17:27:52 +09:00
parent 2099213e18
commit e5c25c5a10
3 changed files with 64 additions and 49 deletions

View File

@@ -66,7 +66,62 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() {
private val mouseStatus = BitSet(8)
private val controllerButtonStatus = BitSet(64)
fun update(delta: Float) {
private fun updateKeyboard() {
/////////////////////
// GAMEPAD CONTROL //
/////////////////////
//////////////////////
// KEYBOARD CONTROL //
//////////////////////
//KeyToggler.update(terrarumIngame.canPlayerControl)
//printdbg(this, terrarumIngame.canPlayerControl)
// control key events
var noKeyHeldDown = true
for (key in 1..Input.Keys.MAX_KEYCODE) {
val keyDown = Gdx.input.isKeyPressed(key)
noKeyHeldDown = noKeyHeldDown and keyDown
if (keyDown && !keyStatus[key])
tKeyDown(key)
else if (!keyDown && keyStatus[key])
tKeyUp(key)
keyStatus[key] = keyDown
if (key == Input.Keys.ENTER && keyDown) {
printdbg(this, "ENTER down")
}
}
// control mouse/touch events
val newmx = Gdx.input.x
val newmy = Gdx.input.y
for (touch in 0..4) {
val touchDown = Gdx.input.isButtonPressed(touch)
if (touchDown && !mouseStatus[touch])
tTouchDown(newmx, newmy, 0, touch)
else if (!touchDown && keyStatus[touch])
tTouchUp(newmx, newmy, 0, touch)
if (touchDown && mouseStatus.bitCount() != 0) {
tTouchDragged(newmx, newmy, 0)
}
mouseStatus[touch] = touchDown
}
inputMouseX = newmx
inputMouseY = newmy
}
fun update() {
///////////////////
// MOUSE CONTROL //
@@ -100,52 +155,7 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() {
}
/////////////////////
// GAMEPAD CONTROL //
/////////////////////
//////////////////////
// KEYBOARD CONTROL //
//////////////////////
//KeyToggler.update(terrarumIngame.canPlayerControl)
//printdbg(this, terrarumIngame.canPlayerControl)
// control key events
var noKeyHeldDown = true
for (key in 1..Input.Keys.MAX_KEYCODE) {
val keyDown = Gdx.input.isKeyPressed(key)
noKeyHeldDown = noKeyHeldDown and keyDown
if (keyDown && !keyStatus[key])
tKeyDown(key)
else if (!keyDown && keyStatus[key])
tKeyUp(key)
keyStatus[key] = keyDown
}
// control mouse/touch events
val newmx = Gdx.input.x
val newmy = Gdx.input.y
for (touch in 0..4) {
val touchDown = Gdx.input.isButtonPressed(touch)
if (touchDown && !mouseStatus[touch])
tTouchDown(newmx, newmy, 0, touch)
else if (!touchDown && keyStatus[touch])
tTouchUp(newmx, newmy, 0, touch)
if (touchDown && mouseStatus.bitCount() != 0) {
tTouchDragged(newmx, newmy, 0)
}
mouseStatus[touch] = touchDown
}
inputMouseX = newmx
inputMouseY = newmy
updateKeyboard()
}
private var f12Down = false

View File

@@ -516,6 +516,10 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
gameFullyLoaded = true
}
ingameController.update()
// define custom update rate
val updateRate = if (KeyToggler.isOn(Input.Keys.APOSTROPHE)) 1f / 8f else AppLoader.UPDATE_RATE
@@ -554,7 +558,7 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
// synchronised Ingame Input Updater
// will also queue up the block/wall/wire placed events
ingameController.update(delta)
ingameController.update()
if (!paused) {

View File

@@ -15,6 +15,7 @@ import net.torvald.terrarum.imagefont.TinyAlphNum
import net.torvald.terrarum.modulebasegame.IngameRenderer
import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarum.modulebasegame.ui.ItemSlotImageFactory
import net.torvald.terrarum.realestate.LandUtil
import net.torvald.terrarum.worlddrawer.CreateTileAtlas
import net.torvald.terrarum.worlddrawer.LightmapRenderer
import net.torvald.terrarum.worlddrawer.WorldCamera
@@ -163,7 +164,7 @@ class BasicDebugInfoWindow : UICanvas() {
val wireCount = wires?.size?.toString() ?: "no"
printLine(batch, 9, "tile@cursor ${ccO}W$ccG$wallNum ${ccO}T$ccG$tileNum ${ccO}C$ccG($wireCount wires) $ccY($mtX, $mtY)")
printLine(batch, 9, "tile@cursor ${ccO}W$ccG$wallNum ${ccO}T$ccG$tileNum ${ccO}C$ccG($wireCount wires) $ccY($mtX,$mtY;$ccO${LandUtil.getBlockAddr(ingame!!.world, mouseTileX, mouseTileY)}$ccY)")
printLine(batch, 10, "fluid@cursor ${ccO}Type $ccG${fluid.type.value} ${ccO}Fill $ccG${fluid.amount}f")
}