Solved ANR problems related to moving units long distances across the map

This commit is contained in:
Yair Morgenstern 2018-11-28 18:59:18 +02:00
parent 44be2b6d67
commit c807ee5098
3 changed files with 24 additions and 16 deletions

View file

@ -21,8 +21,8 @@ android {
applicationId "com.unciv.game"
minSdkVersion 14
targetSdkVersion 26
versionCode 167
versionName "2.10.6"
versionCode 168
versionName "2.10.7"
}
buildTypes {
release {

View file

@ -95,8 +95,11 @@ class TileMapHolder(internal val worldScreen: WorldScreen, internal val tileMap:
val selectedUnit = worldScreen.bottomBar.unitTable.selectedUnit
if (selectedUnit != null && selectedUnit.getTile() != tileInfo
&& selectedUnit.canMoveTo(tileInfo) && selectedUnit.movementAlgs().canReach(tileInfo)) {
// this can take a long time, because of the unit-to-tile calculation needed, so we put it in a different thread
kotlin.concurrent.thread {
addMoveHereButtonToTile(selectedUnit, tileInfo, tileGroup)
}
}
worldScreen.bottomBar.unitTable.tileSelected(tileInfo)
worldScreen.update()
@ -119,16 +122,21 @@ class TileMapHolder(internal val worldScreen: WorldScreen, internal val tileMap:
if (selectedUnit.currentMovement > 0)
moveHereButton.onClick {
// this can take a long time, because of the unit-to-tile calculation needed, so we put it in a different thread
kotlin.concurrent.thread {
if (selectedUnit.movementAlgs().canReach(tileInfo)) {
selectedUnit.movementAlgs().headTowards(tileInfo)
if (selectedUnit.currentTile != tileInfo)
selectedUnit.action = "moveTo " + tileInfo.position.x.toInt() + "," + tileInfo.position.y.toInt()
}
worldScreen.update()
// we don't update it directly because we're on a different thread; instead, we tell it to update itself
worldScreen.shouldUpdate = true
moveToOverlay!!.remove()
moveToOverlay = null
}
}
else moveHereButton.color.a = 0.5f
addOverlayOnTileGroup(tileGroup, moveHereButton).apply { width = size; height = size }
moveHereButton.y += tileGroup.height

View file

@ -237,7 +237,7 @@ class WorldScreen : CameraStageBaseScreen() {
// but the main thread does other stuff, including showing tutorials which guess what? Changes the game data
// BOOM! Exception!
// That's why this needs to be after the game is saved.
shouldUpdateBecauseOfNewTurn=true
shouldUpdate=true
nextTurnButton.setText("Next turn".tr())
Gdx.input.inputProcessor = stage
@ -256,9 +256,9 @@ class WorldScreen : CameraStageBaseScreen() {
}
}
private var shouldUpdateBecauseOfNewTurn=false
var shouldUpdate=false
override fun render(delta: Float) {
if(shouldUpdateBecauseOfNewTurn){ // This is so that updates happen in the MAIN THREAD, where there is a GL Context,
if(shouldUpdate){ // This is so that updates happen in the MAIN THREAD, where there is a GL Context,
// otherwise images will not load properly!
update()
@ -285,7 +285,7 @@ class WorldScreen : CameraStageBaseScreen() {
if(civInfo.tech.getUniques().contains("Enables embarkation for land units"))
displayTutorials("CanEmbark")
shouldUpdateBecauseOfNewTurn=false
shouldUpdate=false
}
super.render(delta)
}