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" applicationId "com.unciv.game"
minSdkVersion 14 minSdkVersion 14
targetSdkVersion 26 targetSdkVersion 26
versionCode 167 versionCode 168
versionName "2.10.6" versionName "2.10.7"
} }
buildTypes { buildTypes {
release { release {

View file

@ -37,7 +37,7 @@ class TileMapHolder(internal val worldScreen: WorldScreen, internal val tileMap:
for (tileInfo in tileMap.values) { for (tileInfo in tileMap.values) {
val tileGroup = WorldTileGroup(tileInfo) val tileGroup = WorldTileGroup(tileInfo)
tileGroup.onClick{onTileClicked(tileInfo, tileGroup)} tileGroup.onClick{ onTileClicked(tileInfo, tileGroup)}
val positionalVector = HexMath().hex2WorldCoords(tileInfo.position) val positionalVector = HexMath().hex2WorldCoords(tileInfo.position)
val groupSize = 50 val groupSize = 50
@ -95,7 +95,10 @@ class TileMapHolder(internal val worldScreen: WorldScreen, internal val tileMap:
val selectedUnit = worldScreen.bottomBar.unitTable.selectedUnit val selectedUnit = worldScreen.bottomBar.unitTable.selectedUnit
if (selectedUnit != null && selectedUnit.getTile() != tileInfo if (selectedUnit != null && selectedUnit.getTile() != tileInfo
&& selectedUnit.canMoveTo(tileInfo) && selectedUnit.movementAlgs().canReach(tileInfo)) { && selectedUnit.canMoveTo(tileInfo) && selectedUnit.movementAlgs().canReach(tileInfo)) {
addMoveHereButtonToTile(selectedUnit, tileInfo, tileGroup) // 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.bottomBar.unitTable.tileSelected(tileInfo)
@ -119,16 +122,21 @@ class TileMapHolder(internal val worldScreen: WorldScreen, internal val tileMap:
if (selectedUnit.currentMovement > 0) if (selectedUnit.currentMovement > 0)
moveHereButton.onClick { moveHereButton.onClick {
if (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
selectedUnit.movementAlgs().headTowards(tileInfo) kotlin.concurrent.thread {
if (selectedUnit.currentTile != tileInfo) if (selectedUnit.movementAlgs().canReach(tileInfo)) {
selectedUnit.action = "moveTo " + tileInfo.position.x.toInt() + "," + tileInfo.position.y.toInt() 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
moveToOverlay!!.remove() worldScreen.shouldUpdate = true
moveToOverlay = null moveToOverlay!!.remove()
moveToOverlay = null
}
} }
else moveHereButton.color.a = 0.5f else moveHereButton.color.a = 0.5f
addOverlayOnTileGroup(tileGroup, moveHereButton).apply { width = size; height = size } addOverlayOnTileGroup(tileGroup, moveHereButton).apply { width = size; height = size }
moveHereButton.y += tileGroup.height 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 // but the main thread does other stuff, including showing tutorials which guess what? Changes the game data
// BOOM! Exception! // BOOM! Exception!
// That's why this needs to be after the game is saved. // That's why this needs to be after the game is saved.
shouldUpdateBecauseOfNewTurn=true shouldUpdate=true
nextTurnButton.setText("Next turn".tr()) nextTurnButton.setText("Next turn".tr())
Gdx.input.inputProcessor = stage Gdx.input.inputProcessor = stage
@ -256,9 +256,9 @@ class WorldScreen : CameraStageBaseScreen() {
} }
} }
private var shouldUpdateBecauseOfNewTurn=false var shouldUpdate=false
override fun render(delta: Float) { 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! // otherwise images will not load properly!
update() update()
@ -285,7 +285,7 @@ class WorldScreen : CameraStageBaseScreen() {
if(civInfo.tech.getUniques().contains("Enables embarkation for land units")) if(civInfo.tech.getUniques().contains("Enables embarkation for land units"))
displayTutorials("CanEmbark") displayTutorials("CanEmbark")
shouldUpdateBecauseOfNewTurn=false shouldUpdate=false
} }
super.render(delta) super.render(delta)
} }