Movement popup can handle multiple selected units, but only considers one of them 'moveable' - so far no functional change, only bricklaying

This commit is contained in:
Yair Morgenstern 2020-09-11 16:16:25 +03:00
parent b9938eb116
commit c659af8762

View File

@ -23,6 +23,7 @@ import com.unciv.ui.map.TileGroupMap
import com.unciv.ui.tilegroups.TileSetStrings import com.unciv.ui.tilegroups.TileSetStrings
import com.unciv.ui.tilegroups.WorldTileGroup import com.unciv.ui.tilegroups.WorldTileGroup
import com.unciv.ui.utils.* import com.unciv.ui.utils.*
import java.util.concurrent.ConcurrentHashMap
import kotlin.concurrent.thread import kotlin.concurrent.thread
@ -82,16 +83,16 @@ class WorldMapHolder(internal val worldScreen: WorldScreen, internal val tileMap
selectedTile = tileInfo selectedTile = tileInfo
val unitTable = worldScreen.bottomUnitTable val unitTable = worldScreen.bottomUnitTable
val previousSelectedUnit = unitTable.selectedUnit val previousSelectedUnits = unitTable.selectedUnits.toList() // create copy
val previousSelectedCity = unitTable.selectedCity val previousSelectedCity = unitTable.selectedCity
unitTable.tileSelected(tileInfo) unitTable.tileSelected(tileInfo)
val newSelectedUnit = unitTable.selectedUnit val newSelectedUnit = unitTable.selectedUnit
if (previousSelectedUnit != null && previousSelectedUnit.getTile() != tileInfo if (previousSelectedUnits.isNotEmpty() && previousSelectedUnits.any { it.getTile() != tileInfo }
&& worldScreen.isPlayersTurn && worldScreen.isPlayersTurn
&& previousSelectedUnit.movement.canMoveTo(tileInfo)) { && previousSelectedUnits.any { it.movement.canMoveTo(tileInfo) }) {
// this can take a long time, because of the unit-to-tile calculation needed, so we put it in a different thread // this can take a long time, because of the unit-to-tile calculation needed, so we put it in a different thread
addTileOverlaysWithUnitMovement(previousSelectedUnit, tileInfo) addTileOverlaysWithUnitMovement(previousSelectedUnits, tileInfo)
} }
else addTileOverlays(tileInfo) // no unit movement but display the units in the tile etc. else addTileOverlays(tileInfo) // no unit movement but display the units in the tile etc.
@ -146,28 +147,42 @@ class WorldMapHolder(internal val worldScreen: WorldScreen, internal val tileMap
} }
private fun addTileOverlaysWithUnitMovement(selectedUnit: MapUnit, tileInfo: TileInfo) { private fun addTileOverlaysWithUnitMovement(selectedUnits: List<MapUnit>, tileInfo: TileInfo) {
thread(name="TurnsToGetThere") { thread(name = "TurnsToGetThere") {
/** LibGdx sometimes has these weird errors when you try to edit the UI layout from 2 separate threads. /** LibGdx sometimes has these weird errors when you try to edit the UI layout from 2 separate threads.
* And so, all UI editing will be done on the main thread. * And so, all UI editing will be done on the main thread.
* The only "heavy lifting" that needs to be done is getting the turns to get there, * The only "heavy lifting" that needs to be done is getting the turns to get there,
* so that and that alone will be relegated to the concurrent thread. * so that and that alone will be relegated to the concurrent thread.
*/ */
val turnsToGetThere = if(selectedUnit.type.isAirUnit()){
if (selectedUnit.movement.canReach(tileInfo)) 1 val unitToTurnsToTile = HashMap<MapUnit,Int>()
else 0 for(unit in selectedUnits) {
val turnsToGetThere = if (unit.type.isAirUnit()) {
if (unit.movement.canReach(tileInfo)) 1
else 0
} else unit.movement.getShortestPath(tileInfo).size // this is what takes the most time, tbh
unitToTurnsToTile[unit] = turnsToGetThere
} }
else selectedUnit.movement.getShortestPath(tileInfo).size // this is what takes the most time, tbh
Gdx.app.postRunnable { Gdx.app.postRunnable {
if(UncivGame.Current.settings.singleTapMove && turnsToGetThere==1) { val unitsWhoCanMoveThere = unitToTurnsToTile.filter { it.value!=0 }
if (unitsWhoCanMoveThere.isEmpty()){ // give the regular tile overlays with no unit movement
addTileOverlays(tileInfo)
worldScreen.shouldUpdate = true
return@postRunnable
}
val turnsToGetThere = unitsWhoCanMoveThere.values.first()
val selectedUnit = unitsWhoCanMoveThere.keys.first()
if (UncivGame.Current.settings.singleTapMove && turnsToGetThere == 1) {
// single turn instant move // single turn instant move
selectedUnit.movement.headTowards(tileInfo) selectedUnit.movement.headTowards(tileInfo)
worldScreen.bottomUnitTable.selectUnits(selectedUnit) // keep moved unit selected worldScreen.bottomUnitTable.selectUnits(selectedUnit) // keep moved unit selected
} else { } else {
// add "move to" button if there is a path to tileInfo // add "move to" button if there is a path to tileInfo
val moveHereButtonDto = if (turnsToGetThere != 0) MoveHereButtonDto(hashMapOf(selectedUnit to turnsToGetThere), tileInfo) val moveHereButtonDto = if (turnsToGetThere != 0) MoveHereButtonDto(hashMapOf(selectedUnit to turnsToGetThere), tileInfo)
else null else null
addTileOverlays(tileInfo, moveHereButtonDto) addTileOverlays(tileInfo, moveHereButtonDto)
} }
worldScreen.shouldUpdate = true worldScreen.shouldUpdate = true