Improved queue functionality readability

This commit is contained in:
Yair Morgenstern
2020-01-29 22:25:21 +02:00
parent 267c98be55
commit b29813b389
2 changed files with 49 additions and 50 deletions

View File

@ -126,27 +126,29 @@ class CityConstructions {
return currentConstruction is Building && currentConstruction.isWonder
}
fun isFirstOfItsKind(idx: Int, name: String): Boolean {
/** If the city is constructing multiple units of the same type, subsequent units will require the full cost */
fun isFirstConstructionOfItsKind(constructionQueueIndex: Int, name: String): Boolean {
// idx = 1 is the currentConstruction, so it is the first
if (idx == -1)
if (constructionQueueIndex == -1)
return true
// if the construction name is the same as the current construction, it isn't the first
return !isBeingConstructed(name) &&
idx == constructionQueue.indexOfFirst{it == name}
constructionQueueIndex == constructionQueue.indexOfFirst{it == name}
}
internal fun getConstruction(constructionName: String): IConstruction {
val gameBasics = cityInfo.getRuleset()
if (gameBasics.buildings.containsKey(constructionName))
return gameBasics.buildings[constructionName]!!
else if (gameBasics.units.containsKey(constructionName))
return gameBasics.units[constructionName]!!
else{
if(constructionName=="") return getConstruction("Nothing")
val special = SpecialConstruction.getSpecialConstructions().firstOrNull{it.name==constructionName}
if(special!=null) return special
when {
gameBasics.buildings.containsKey(constructionName) -> return gameBasics.buildings[constructionName]!!
gameBasics.units.containsKey(constructionName) -> return gameBasics.units[constructionName]!!
constructionName=="" -> return getConstruction("Nothing")
else -> {
val special = SpecialConstruction.getSpecialConstructions()
.firstOrNull{it.name==constructionName}
if(special!=null) return special
}
}
class NotBuildingOrUnitException(message:String):Exception(message)
@ -351,30 +353,30 @@ class CityConstructions {
}
}
fun removeFromQueue(idx: Int) {
// idx -1 is the current construction
if (idx < 0) {
fun removeFromQueue(constructionQueueIndex: Int) {
// constructionQueueIndex -1 is the current construction
if (constructionQueueIndex < 0) {
// To prevent Construction Automation
if (constructionQueue.isEmpty()) constructionQueue.add("Nothing")
cancelCurrentConstruction()
} else
constructionQueue.removeAt(idx)
constructionQueue.removeAt(constructionQueueIndex)
}
fun higherPrio(idx: Int) {
fun raisePriority(constructionQueueIndex: Int) {
// change current construction
if(idx == 0) {
if(constructionQueueIndex == 0) {
// Add current construction to queue after the first element
constructionQueue.add(1, currentConstruction)
cancelCurrentConstruction()
}
else
constructionQueue.swap(idx-1, idx)
constructionQueue.swap(constructionQueueIndex-1, constructionQueueIndex)
}
// Lowering == Highering next element in queue
fun lowerPrio(idx: Int) {
higherPrio(idx+1)
fun lowerPriority(constructionQueueIndex: Int) {
raisePriority(constructionQueueIndex+1)
}
//endregion

View File

@ -139,7 +139,6 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre
}
for (building in city.getRuleset().buildings.values.filter { it.shouldBeDisplayed(cityConstructions)}) {
val useStoredProduction = !cityConstructions.isBeingConstructedOrEnqueued(building.name)
val turnsToBuilding = cityConstructions.turnsToConstruction(building.name)
val productionTextButton = getProductionButton(building.name,
building.name.tr() + "\r\n" + turnsToBuilding + turnOrTurns(turnsToBuilding),
@ -162,7 +161,7 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre
availableConstructionsTable.addCategory("Other", specialConstructions)
}
private fun getQueueEntry(idx: Int, name: String, isLast: Boolean, isSelected: Boolean): Table {
private fun getQueueEntry(constructionQueueIndex: Int, name: String, isLast: Boolean, isSelected: Boolean): Table {
val city = cityScreen.city
val cityConstructions = city.cityConstructions
@ -173,23 +172,24 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre
if (isSelected)
table.background = ImageGetter.getBackground(Color.GREEN.cpy().lerp(Color.BLACK, 0.5f))
val turnsToComplete = cityConstructions.turnsToConstruction(name, cityConstructions.isFirstOfItsKind(idx, name))
val isFirstConstructionOfItsKind = cityConstructions.isFirstConstructionOfItsKind(constructionQueueIndex, name)
val turnsToComplete = cityConstructions.turnsToConstruction(name, isFirstConstructionOfItsKind)
val text = name.tr() + "\r\n" + turnsToComplete + turnOrTurns(turnsToComplete)
table.defaults().pad(2f).minWidth(40f)
table.add(ImageGetter.getConstructionImage(name).surroundWithCircle(40f)).padRight(10f)
table.add(text.toLabel()).expandX().fillX().left()
if (idx >= 0) table.add(getHigherPrioButton(idx, name, city)).right()
if (constructionQueueIndex >= 0) table.add(getRaisePriorityButton(constructionQueueIndex, name, city)).right()
else table.add().right()
if (!isLast) table.add(getLowerPrioButton(idx, name, city)).right()
if (!isLast) table.add(getLowerPriorityButton(constructionQueueIndex, name, city)).right()
else table.add().right()
table.touchable = Touchable.enabled
table.onClick {
cityScreen.selectedConstruction = cityScreen.city.cityConstructions.getConstruction(name)
cityScreen.selectedTile = null
selectedQueueEntry = idx
selectedQueueEntry = constructionQueueIndex
cityScreen.update()
}
return table
@ -236,31 +236,30 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre
if (isSelectedQueueEntry()) {
button = TextButton("Remove from queue".tr(), CameraStageBaseScreen.skin)
if (UncivGame.Current.worldScreen.isPlayersTurn && !city.isPuppet) {
if (!UncivGame.Current.worldScreen.isPlayersTurn || city.isPuppet) button.disable()
else {
button.onClick {
cityConstructions.removeFromQueue(selectedQueueEntry)
cityScreen.selectedConstruction = null
selectedQueueEntry = -2
cityScreen.update()
}
} else {
button.disable()
}
} else {
button = TextButton("Add to queue".tr(), CameraStageBaseScreen.skin)
if (construction != null
&& !cityConstructions.isQueueFull()
&& cityConstructions.getConstruction(construction.name).isBuildable(cityConstructions)
&& UncivGame.Current.worldScreen.isPlayersTurn
&& !city.isPuppet) {
if (construction == null
|| cityConstructions.isQueueFull()
|| !cityConstructions.getConstruction(construction.name).isBuildable(cityConstructions)
|| !UncivGame.Current.worldScreen.isPlayersTurn
|| city.isPuppet) {
button.disable()
} else {
button.onClick {
cityConstructions.addToQueue(construction.name)
if (!construction.shouldBeDisplayed(cityConstructions)) cityScreen.selectedConstruction = null
cityScreen.update()
cityScreen.game.settings.addCompletedTutorialTask("Pick construction")
}
} else {
button.disable()
}
}
@ -274,11 +273,12 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre
val button = TextButton("", CameraStageBaseScreen.skin)
if (construction != null
&& construction.canBePurchased()
&& UncivGame.Current.worldScreen.isPlayersTurn
&& !city.isPuppet
&& !city.isInResistance()) {
if (construction == null || !construction.canBePurchased()
|| !UncivGame.Current.worldScreen.isPlayersTurn
|| city.isPuppet || city.isInResistance()) {
button.setText("Buy".tr())
button.disable()
} else {
val constructionGoldCost = construction.getGoldCost(city.civInfo)
button.setText("Buy".tr() + " " + constructionGoldCost)
@ -303,9 +303,6 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre
if (constructionGoldCost > city.civInfo.gold)
button.disable()
} else {
button.setText("Buy".tr())
button.disable()
}
button.labelCell.pad(5f)
@ -313,34 +310,34 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre
return button
}
private fun getHigherPrioButton(idx: Int, name: String, city: CityInfo): Table {
private fun getRaisePriorityButton(constructionQueueIndex: Int, name: String, city: CityInfo): Table {
val tab = Table()
tab.add(ImageGetter.getImage("OtherIcons/Up").surroundWithCircle(40f))
if (UncivGame.Current.worldScreen.isPlayersTurn && !city.isPuppet) {
tab.touchable = Touchable.enabled
tab.onClick {
tab.touchable = Touchable.disabled
city.cityConstructions.higherPrio(idx)
city.cityConstructions.raisePriority(constructionQueueIndex)
cityScreen.selectedConstruction = cityScreen.city.cityConstructions.getConstruction(name)
cityScreen.selectedTile = null
selectedQueueEntry = idx - 1
selectedQueueEntry = constructionQueueIndex - 1
cityScreen.update()
}
}
return tab
}
private fun getLowerPrioButton(idx: Int, name: String, city: CityInfo): Table {
private fun getLowerPriorityButton(constructionQueueIndex: Int, name: String, city: CityInfo): Table {
val tab = Table()
tab.add(ImageGetter.getImage("OtherIcons/Down").surroundWithCircle(40f))
if (UncivGame.Current.worldScreen.isPlayersTurn && !city.isPuppet) {
tab.touchable = Touchable.enabled
tab.onClick {
tab.touchable = Touchable.disabled
city.cityConstructions.lowerPrio(idx)
city.cityConstructions.lowerPriority(constructionQueueIndex)
cityScreen.selectedConstruction = cityScreen.city.cityConstructions.getConstruction(name)
cityScreen.selectedTile = null
selectedQueueEntry = idx + 1
selectedQueueEntry = constructionQueueIndex + 1
cityScreen.update()
}
}