Split unit actions to a separate file (unitactions), because of all the separate cases

This commit is contained in:
Yair Morgenstern 2018-03-03 23:30:27 +02:00
parent f2c790aec4
commit 064aee2f07
4 changed files with 152 additions and 143 deletions

View file

@ -3,7 +3,7 @@ language: android
jdk: jdk:
- oraclejdk8 - oraclejdk8
before_install: before_install:
- chmod +x gradlew - chmod +x gradlew
install: install:
@ -12,8 +12,6 @@ install:
- echo yes | sdkmanager "build-tools;26.0.2" &>/dev/null - echo yes | sdkmanager "build-tools;26.0.2" &>/dev/null
- echo yes | sdkmanager "platforms;android-26" &>/dev/null - echo yes | sdkmanager "platforms;android-26" &>/dev/null
sudo: false
before_cache: before_cache:
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
- rm -fr $HOME/.gradle/caches/*/plugin-resolution/ - rm -fr $HOME/.gradle/caches/*/plugin-resolution/

View file

@ -138,4 +138,5 @@ class MapUnit {
currentMovement = maxMovement.toFloat() currentMovement = maxMovement.toFloat()
doPreTurnAction(tileInfo) doPreTurnAction(tileInfo)
} }
} }

View file

@ -8,11 +8,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.badlogic.gdx.utils.Align import com.badlogic.gdx.utils.Align
import com.unciv.logic.civilization.CivilizationInfo import com.unciv.logic.civilization.CivilizationInfo
import com.unciv.logic.map.TileInfo import com.unciv.logic.map.TileInfo
import com.unciv.models.gamebasics.Building
import com.unciv.models.gamebasics.GameBasics
import com.unciv.models.linq.Linq
import com.unciv.ui.cityscreen.addClickListener import com.unciv.ui.cityscreen.addClickListener
import com.unciv.ui.pickerscreens.TechPickerScreen
import com.unciv.ui.utils.CameraStageBaseScreen import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.ImageGetter import com.unciv.ui.utils.ImageGetter
@ -26,16 +22,16 @@ class TileInfoTable(private val worldScreen: WorldScreen, internal val civInfo:
background = tileTableBackground background = tileTableBackground
} }
internal fun updateTileTable(selectedTile: TileInfo) { internal fun updateTileTable(tile: TileInfo) {
clearChildren() clearChildren()
val stats = selectedTile.getTileStats(civInfo) val stats = tile.getTileStats(civInfo)
pad(20f) pad(20f)
columnDefaults(0).padRight(10f) columnDefaults(0).padRight(10f)
val skin = CameraStageBaseScreen.skin val skin = CameraStageBaseScreen.skin
if (selectedTile.explored) { if (tile.explored) {
add(Label(selectedTile.toString(), skin)).colspan(2) add(Label(tile.toString(), skin)).colspan(2)
row() row()
@ -47,11 +43,11 @@ class TileInfoTable(private val worldScreen: WorldScreen, internal val civInfo:
} }
if (selectedTile.unit != null) { if (tile.unit != null) {
var moveUnitButton = TextButton("Move to", skin) var moveUnitButton = TextButton("Move to", skin)
if (worldScreen.tileMapHolder.unitTile == selectedTile) moveUnitButton = TextButton("Stop movement", skin) if (worldScreen.tileMapHolder.unitTile == tile) moveUnitButton = TextButton("Stop movement", skin)
moveUnitButton.label.setFontScale(worldScreen.buttonScale) moveUnitButton.label.setFontScale(worldScreen.buttonScale)
if (selectedTile.unit!!.currentMovement == 0f) { if (tile.unit!!.currentMovement == 0f) {
moveUnitButton.color = Color.GRAY moveUnitButton.color = Color.GRAY
moveUnitButton.touchable = Touchable.disabled moveUnitButton.touchable = Touchable.disabled
} }
@ -61,7 +57,7 @@ class TileInfoTable(private val worldScreen: WorldScreen, internal val civInfo:
worldScreen.update() worldScreen.update()
return@addClickListener return@addClickListener
} }
worldScreen.tileMapHolder.unitTile = selectedTile worldScreen.tileMapHolder.unitTile = tile
// Set all tiles transparent except those in unit range // Set all tiles transparent except those in unit range
for (TG in worldScreen.tileGroups.linqValues()) TG.setColor(0f, 0f, 0f, 0.3f) for (TG in worldScreen.tileGroups.linqValues()) TG.setColor(0f, 0f, 0f, 0.3f)
@ -79,137 +75,16 @@ class TileInfoTable(private val worldScreen: WorldScreen, internal val civInfo:
} }
add(moveUnitButton).colspan(2) add(moveUnitButton).colspan(2)
.size(moveUnitButton.width * worldScreen.buttonScale, moveUnitButton.height * worldScreen.buttonScale) .size(moveUnitButton.width * worldScreen.buttonScale, moveUnitButton.height * worldScreen.buttonScale).row()
if (selectedTile.unit!!.name == "Settler") { for (button in UnitActions().getUnitActions(tile.unit!!,tile,civInfo))
addUnitAction("Found City", add(button).colspan(2)
!civInfo.gameInfo.tileMap.getTilesInDistance(selectedTile.position, 2).any { it.isCityCenter }, .size(button.width * worldScreen.buttonScale, button.height * worldScreen.buttonScale).row()
{
val tutorial = Linq<String>()
tutorial.add("You have founded a city!" +
"\r\nCities are the lifeblood of your empire," +
"\r\n providing gold and science empire-wide," +
"\r\n which are displayed on the top bar.")
tutorial.add("Science is used to research technologies." +
"\r\nYou can enter the technology screen by clicking" +
"\r\n on the button on the top-left, underneath the bar")
tutorial.add("You can click the city name to enter" +
"\r\n the city screen to assign population," +
"\r\n choose production, and see information on the city")
worldScreen.displayTutorials("CityFounded", tutorial) pack()
civInfo.addCity(selectedTile.position) setPosition(worldScreen.stage.width - 10f - width, 10f)
if (worldScreen.tileMapHolder.unitTile == selectedTile)
worldScreen.tileMapHolder.unitTile = null // The settler was in the middle of moving and we then founded a city with it
selectedTile.unit = null // Remove settler!
worldScreen.update()
})
}
if (selectedTile.unit!!.name == "Worker") {
val improvementButtonText = if (selectedTile.improvementInProgress == null)
"Construct\r\nimprovement"
else
selectedTile.improvementInProgress!! + "\r\nin progress"
addUnitAction(improvementButtonText, !selectedTile.isCityCenter || GameBasics.TileImprovements.linqValues().any { arg0 -> selectedTile.canBuildImprovement(arg0, civInfo) },
{ worldScreen.game.screen = com.unciv.ui.pickerscreens.ImprovementPickerScreen(selectedTile) })
addUnitAction(if ("automation" == selectedTile.unit!!.action) "Stop automation" else "Automate", true, {
if ("automation" == selectedTile.unit!!.action)
selectedTile.unit!!.action = null
else {
selectedTile.unit!!.action = "automation"
selectedTile.unit!!.doAutomatedAction(selectedTile)
}
worldScreen.update()
})
}
if (selectedTile.unit!!.name == "Great Scientist") {
addUnitAction("Discover Technology", true,
{
civInfo.tech.freeTechs += 1
selectedTile.unit = null// destroy!
worldScreen.game.screen = TechPickerScreen(true, civInfo)
})
addUnitAction("Construct Academy", true, {
selectedTile.improvement = "Academy"
selectedTile.unit = null// destroy!
worldScreen.update()
}
)
}
if (selectedTile.unit!!.name == "Great Artist") {
addUnitAction("Start Golden Age", true,
{
civInfo.goldenAges.enterGoldenAge()
selectedTile.unit = null// destroy!
worldScreen.update()
}
)
addUnitAction("Construct Landmark", true,
{
selectedTile.improvement = "Landmark"
selectedTile.unit = null// destroy!
worldScreen.update()
}
)
}
if (selectedTile.unit!!.name == "Great Engineer") {
addUnitAction("Hurry Wonder", selectedTile.isCityCenter &&
selectedTile.city!!.cityConstructions.getCurrentConstruction() is Building &&
(selectedTile.city!!.cityConstructions.getCurrentConstruction() as Building).isWonder,
{
selectedTile.city!!.cityConstructions.addConstruction(300 + 30 * selectedTile.city!!.population.population) //http://civilization.wikia.com/wiki/Great_engineer_(Civ5)
selectedTile.unit = null // destroy!
worldScreen.update()
})
addUnitAction("Construct Manufactory", true,
{
selectedTile.improvement = "Manufactory"
selectedTile.unit = null// destroy!
worldScreen.update()
})
}
if (selectedTile.unit!!.name == "Great Merchant") {
addUnitAction("Conduct Trade Mission", true,
{
civInfo.gold += 350 // + 50 * era_number - todo!
selectedTile.unit = null // destroy!
worldScreen.update()
})
addUnitAction("Construct Customs House", true,
{
selectedTile.improvement = "Customs House"
selectedTile.unit = null// destroy!
worldScreen.update()
})
}
} }
pack()
setPosition(worldScreen.stage.width - 10f - width, 10f)
}
private fun addUnitAction(actionText: String, canAct: Boolean, action: ()->Unit) {
val actionButton = TextButton(actionText, CameraStageBaseScreen.skin)
actionButton.label.setFontScale(worldScreen.buttonScale)
actionButton.addClickListener(action)
if (worldScreen.tileMapHolder.selectedTile!!.unit!!.currentMovement == 0f || !canAct) {
actionButton.color = Color.GRAY
actionButton.touchable = Touchable.disabled
}
row()
add(actionButton).colspan(2)
.size(actionButton.width * worldScreen.buttonScale, actionButton.height * worldScreen.buttonScale)
} }
} }

View file

@ -0,0 +1,135 @@
package com.unciv.ui.worldscreen
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.unciv.logic.civilization.CivilizationInfo
import com.unciv.logic.map.MapUnit
import com.unciv.logic.map.TileInfo
import com.unciv.models.gamebasics.Building
import com.unciv.models.gamebasics.GameBasics
import com.unciv.models.linq.Linq
import com.unciv.ui.UnCivGame
import com.unciv.ui.cityscreen.addClickListener
import com.unciv.ui.pickerscreens.TechPickerScreen
import com.unciv.ui.utils.CameraStageBaseScreen
import java.util.ArrayList
public class UnitActions {
private fun constructImprovementAndDestroyUnit(tileInfo: TileInfo, improvementName: String): () -> Unit {
return {
tileInfo.improvement = improvementName
tileInfo.unit = null// destroy!
}
}
fun getUnitActions(unit: MapUnit, tile: TileInfo, civInfo: CivilizationInfo): List<TextButton> {
val worldScreen = UnCivGame.Current.worldScreen!!
val actionList = ArrayList<TextButton>()
if (unit.name == "Settler") {
actionList += getUnitActionButton(unit, "Found City",
!civInfo.gameInfo.tileMap.getTilesInDistance(tile.position, 2).any { it.isCityCenter },
{
val tutorial = Linq<String>()
tutorial.add("You have founded a city!" +
"\r\nCities are the lifeblood of your empire," +
"\r\n providing gold and science empire-wide," +
"\r\n which are displayed on the top bar.")
tutorial.add("Science is used to research technologies." +
"\r\nYou can enter the technology screen by clicking" +
"\r\n on the button on the top-left, underneath the bar")
tutorial.add("You can click the city name to enter" +
"\r\n the city screen to assign population," +
"\r\n choose production, and see information on the city")
worldScreen.displayTutorials("CityFounded", tutorial)
civInfo.addCity(tile.position)
if (worldScreen.tileMapHolder.unitTile == tile)
worldScreen.tileMapHolder.unitTile = null // The settler was in the middle of moving and we then founded a city with it
tile.unit = null // Remove settler!
worldScreen.update()
})
}
if (unit.name == "Worker") {
val improvementButtonText =
if (tile.improvementInProgress == null) "Construct\r\nimprovement"
else tile.improvementInProgress!! + "\r\nin progress"
actionList += getUnitActionButton(unit, improvementButtonText, !tile.isCityCenter || GameBasics.TileImprovements.linqValues().any { arg0 -> tile.canBuildImprovement(arg0, civInfo) },
{ worldScreen.game.screen = com.unciv.ui.pickerscreens.ImprovementPickerScreen(tile) })
actionList += getUnitActionButton(unit, if ("automation" == tile.unit!!.action) "Stop automation" else "Automate", true, {
if ("automation" == tile.unit!!.action)
tile.unit!!.action = null
else {
tile.unit!!.action = "automation"
tile.unit!!.doAutomatedAction(tile)
}
worldScreen.update()
})
}
if (unit.name == "Great Scientist") {
actionList += getUnitActionButton(unit, "Discover Technology", true,
{
civInfo.tech.freeTechs += 1
tile.unit = null// destroy!
worldScreen.game.screen = TechPickerScreen(true, civInfo)
})
actionList += getUnitActionButton(unit, "Construct Academy", true,
constructImprovementAndDestroyUnit(tile, "Academy"))
}
if (unit.name == "Great Artist") {
actionList += getUnitActionButton(unit, "Start Golden Age", true,
{
civInfo.goldenAges.enterGoldenAge()
tile.unit = null// destroy!
worldScreen.update()
}
)
actionList += getUnitActionButton(unit, "Construct Landmark", true,
constructImprovementAndDestroyUnit(tile, "Landmark"))
}
if (unit.name == "Great Engineer") {
actionList += getUnitActionButton(unit, "Hurry Wonder", tile.isCityCenter &&
tile.city!!.cityConstructions.getCurrentConstruction() is Building &&
(tile.city!!.cityConstructions.getCurrentConstruction() as Building).isWonder,
{
tile.city!!.cityConstructions.addConstruction(300 + 30 * tile.city!!.population.population) //http://civilization.wikia.com/wiki/Great_engineer_(Civ5)
tile.unit = null // destroy!
})
actionList += getUnitActionButton(unit, "Construct Manufactory", true,
constructImprovementAndDestroyUnit(tile, "Manufactory"))
}
if (unit.name == "Great Merchant") {
actionList += getUnitActionButton(unit, "Conduct Trade Mission", true,
{
civInfo.gold += 350 // + 50 * era_number - todo!
tile.unit = null // destroy!
})
actionList += getUnitActionButton(unit, "Construct Customs House", true,
constructImprovementAndDestroyUnit(tile, "Customs House"))
}
return actionList
}
private fun getUnitActionButton(unit: MapUnit, actionText: String, canAct: Boolean, action: () -> Unit): TextButton {
val actionButton = TextButton(actionText, CameraStageBaseScreen.skin)
actionButton.addClickListener({ action(); UnCivGame.Current.worldScreen!!.update() })
if (unit.currentMovement == 0f || !canAct) {
actionButton.color = Color.GRAY
actionButton.touchable = Touchable.disabled
}
return actionButton
}
}