idle unit buttons: set due=false and select the right unit in case of 2 units per tile

This commit is contained in:
martin
2019-05-14 08:45:25 +02:00
committed by Yair Morgenstern
parent 462dc26460
commit 1c3b44af16
3 changed files with 28 additions and 19 deletions

View File

@ -301,6 +301,8 @@ class CivilizationInfo {
units=newList
}
fun getIdleUnits() = getCivUnits().filter { it.isIdle() }
fun getDueUnits() = getCivUnits().filter { it.due && it.isIdle() }
fun shouldOpenTechPicker() = tech.freeTechs != 0
@ -309,11 +311,10 @@ class CivilizationInfo {
fun shouldGoToDueUnit() = UnCivGame.Current.settings.checkForDueUnits && getDueUnits().isNotEmpty()
fun getNextDueUnit(selectedUnit: MapUnit?): MapUnit? {
selectedUnit?.due = false
val dueUnits = getDueUnits()
if(dueUnits.isNotEmpty()) {
var index = dueUnits.indexOf(selectedUnit)
index = ++index % dueUnits.size // for looping
val unit = dueUnits[index]
val unit = dueUnits[0]
unit.due = false
return unit
}

View File

@ -4,19 +4,21 @@ import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.utils.Align
import com.unciv.logic.map.MapUnit
import com.unciv.logic.map.TileInfo
import com.unciv.ui.utils.ImageGetter
import com.unciv.ui.utils.onClick
import com.unciv.ui.worldscreen.TileMapHolder
class IdleUnitButton (internal val unitTable: UnitTable,
val tileMapHolder: TileMapHolder, val previous:Boolean)
: Table() {
class IdleUnitButton (
internal val unitTable: UnitTable,
val tileMapHolder: TileMapHolder,
val previous:Boolean
) : Table() {
val image = ImageGetter.getImage("OtherIcons/BackArrow")
fun getTilesWithIdleUnits() = tileMapHolder.tileMap.values
.filter { it.hasIdleUnit() && it.getUnits().first().owner == unitTable.worldScreen.currentPlayerCiv.civName }
fun hasIdleUnits() = unitTable.worldScreen.currentPlayerCiv.getIdleUnits().isNotEmpty()
init {
val imageSize = 25f
@ -28,20 +30,26 @@ class IdleUnitButton (internal val unitTable: UnitTable,
add(image).size(imageSize).pad(10f,20f,10f,20f)
enable()
onClick {
val tilesWithIdleUnits = getTilesWithIdleUnits()
if(tilesWithIdleUnits.isEmpty()) return@onClick
val tileToSelect: TileInfo
if (unitTable.selectedUnit==null || !tilesWithIdleUnits.contains(unitTable.selectedUnit!!.getTile()))
tileToSelect = tilesWithIdleUnits[0]
val idleUnits = unitTable.worldScreen.currentPlayerCiv.getIdleUnits()
if(idleUnits.isEmpty()) return@onClick
val unitToSelect: MapUnit
if (unitTable.selectedUnit==null || !idleUnits.contains(unitTable.selectedUnit!!))
unitToSelect = idleUnits[0]
else {
var index = tilesWithIdleUnits.indexOf(unitTable.selectedUnit!!.getTile())
var index = idleUnits.indexOf(unitTable.selectedUnit!!)
if(previous) index-- else index++
index += tilesWithIdleUnits.size
index %= tilesWithIdleUnits.size // for looping
tileToSelect = tilesWithIdleUnits[index]
index += idleUnits.size
index %= idleUnits.size // for looping
unitToSelect = idleUnits[index]
}
tileMapHolder.setCenterPosition(tileToSelect.position)
unitToSelect.due = false
tileMapHolder.setCenterPosition(unitToSelect.currentTile.position)
unitTable.selectedUnit = unitToSelect
unitTable.worldScreen.shouldUpdate=true
}
}

View File

@ -92,7 +92,7 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){
}
}
if(prevIdleUnitButton.getTilesWithIdleUnits().isNotEmpty()) { // more efficient to do this check once for both
if(prevIdleUnitButton.hasIdleUnits()) { // more efficient to do this check once for both
prevIdleUnitButton.enable()
nextIdleUnitButton.enable()
}