Unit can move around the map

This commit is contained in:
Yair Morgenstern 2020-02-03 15:57:09 +02:00
parent 51c3126a9a
commit ffccb75860
5 changed files with 56 additions and 17 deletions

View file

@ -288,10 +288,10 @@ open class TileInfo {
fun isRoughTerrain() = getBaseTerrain().rough || getTerrainFeature()?.rough == true
override fun toString(): String {
fun toString(viewingCiv: CivilizationInfo): String {
val lineList = ArrayList<String>() // more readable than StringBuilder, with same performance for our use-case
val isViewableToPlayer = UncivGame.Current.viewEntireMapForDebug
|| UncivGame.Current.gameInfo.getCurrentPlayerCivilization().viewableTiles.contains(this)
|| viewingCiv.viewableTiles.contains(this)
if (isCityCenter()) {
val city = getCity()!!

View file

@ -32,7 +32,7 @@ class CityScreenTileTable(val city: CityInfo): Table(){
val stats = selectedTile.getTileStats(city, city.civInfo)
innerTable.pad(20f)
innerTable.add(selectedTile.toString().toLabel()).colspan(2)
innerTable.add(selectedTile.toString(city.civInfo).toLabel()).colspan(2)
innerTable.row()
innerTable.add(getTileStatsTable(stats)).row()

View file

@ -26,8 +26,10 @@ class TileGroupIcons(val tileGroup: TileGroup){
updateResourceIcon(showResourcesAndImprovements,viewingCiv)
updateImprovementIcon(showResourcesAndImprovements)
civilianUnitIcon = newUnitIcon(tileGroup.tileInfo.civilianUnit, civilianUnitIcon, tileIsViewable, -20f)
militaryUnitIcon = newUnitIcon(tileGroup.tileInfo.militaryUnit, militaryUnitIcon, tileIsViewable && showMilitaryUnit, 20f)
civilianUnitIcon = newUnitIcon(tileGroup.tileInfo.civilianUnit, civilianUnitIcon,
tileIsViewable, -20f, viewingCiv)
militaryUnitIcon = newUnitIcon(tileGroup.tileInfo.militaryUnit, militaryUnitIcon,
tileIsViewable && showMilitaryUnit, 20f, viewingCiv)
}
fun addPopulationIcon() {
@ -47,7 +49,7 @@ class TileGroupIcons(val tileGroup: TileGroup){
}
fun newUnitIcon(unit: MapUnit?, oldUnitGroup: UnitGroup?, isViewable: Boolean, yFromCenter: Float): UnitGroup? {
fun newUnitIcon(unit: MapUnit?, oldUnitGroup: UnitGroup?, isViewable: Boolean, yFromCenter: Float, viewingCiv: CivilizationInfo?): UnitGroup? {
var newImage: UnitGroup? = null
// The unit can change within one update - for instance, when attacking, the attacker replaces the defender!
oldUnitGroup?.remove()
@ -81,7 +83,7 @@ class TileGroupIcons(val tileGroup: TileGroup){
// Instead of fading out the entire unit with its background, we just fade out its central icon,
// that way it remains much more visible on the map
if (!unit.isIdle() && unit.civInfo == UncivGame.Current.worldScreen.viewingCiv)
if (!unit.isIdle() && unit.civInfo == viewingCiv)
newImage.unitBaseImage.color.a = 0.5f
}
return newImage

View file

@ -20,6 +20,8 @@ import com.unciv.logic.civilization.PlayerType
import com.unciv.logic.civilization.diplomacy.DiplomaticStatus
import com.unciv.logic.map.MapGenerator
import com.unciv.logic.map.MapParameters
import com.unciv.logic.map.MapUnit
import com.unciv.logic.map.TileInfo
import com.unciv.models.Tutorial
import com.unciv.models.UncivSound
import com.unciv.models.ruleset.RulesetCache
@ -54,7 +56,7 @@ class WorldScreen(val viewingCiv:CivilizationInfo) : CameraStageBaseScreen() {
private val topBar = WorldScreenTopBar(this)
val bottomUnitTable = UnitTable(this)
val bottomTileInfoTable = TileInfoTable(this)
val bottomTileInfoTable = TileInfoTable(viewingCiv)
val battleTable = BattleTable(this)
val unitActionsTable = UnitActionsTable(this)
@ -547,14 +549,33 @@ class TotemWorldScreen : CameraStageBaseScreen(){
val tileSetStrings = TileSetStrings()
val tileMapHolder = TileGroupMap(tileMap.values.map { TileGroup(it,tileSetStrings) },500f)
val newCiv = CivilizationInfo("Babylon")
var selectedUnit : MapUnit? = null
val gameInfo = GameInfo()
val tileInfoTable = TileInfoTable(newCiv)
var selectedTile: TileInfo?=null
init{
newCiv.playerType=PlayerType.Human
val gameInfo = GameInfo()
gameInfo.tileMap = tileMap
gameInfo.civilizations.add(newCiv)
gameInfo.setTransients()
ImageGetter.ruleset = RulesetCache.getBaseRuleset()
val scrollPane = ScrollPane(tileMapHolder)
for(tileGroup in tileMapHolder.tileGroups)
tileGroup.onClick {
val tileInfo = tileGroup.tileInfo
selectedTile=tileInfo
if(tileInfo.civilianUnit!=null){
selectedUnit = tileInfo.civilianUnit
}
else if(selectedUnit!=null
&& selectedUnit!!.movement.getDistanceToTiles().containsKey(tileInfo)
&& selectedUnit!!.movement.canMoveTo(tileInfo)){
selectedUnit!!.movement.moveToTile(tileInfo)
}
update()
}
scrollPane.setSize(stage.width,stage.height)
scrollPane.center(stage)
scrollPane.scrollPercentX=0.5f
@ -562,10 +583,27 @@ class TotemWorldScreen : CameraStageBaseScreen(){
stage.addActor(scrollPane)
newCiv.placeUnitNearTile(Vector2.Zero, Constants.worker)
update()
stage.addActor(tileInfoTable)
}
fun update() {
for(tileGroup in tileMapHolder.tileGroups)
if(newCiv.getIdleUnits().none()) gameInfo.nextTurn()
for (tileGroup in tileMapHolder.tileGroups) {
tileGroup.update(newCiv)
tileGroup.hideCircle()
if (tileGroup.tileInfo.civilianUnit == selectedUnit) {
tileGroup.icons.civilianUnitIcon?.selectUnit()
}
}
if(selectedUnit!=null) {
for (tile in selectedUnit!!.movement.getDistanceToTiles().keys){
val tileGroup = tileMapHolder.tileGroups.first{it.tileInfo==tile}
tileGroup.showCircle(Color.WHITE)
}
}
if(selectedTile!=null) tileInfoTable.updateTileTable(selectedTile!!)
tileInfoTable.setPosition(stage.width,0f,Align.bottomRight)
}
}

View file

@ -4,24 +4,23 @@ import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.utils.Align
import com.unciv.UncivGame
import com.unciv.logic.civilization.CivilizationInfo
import com.unciv.logic.map.TileInfo
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.ImageGetter
import com.unciv.ui.utils.toLabel
import com.unciv.ui.worldscreen.WorldScreen
class TileInfoTable(private val worldScreen: WorldScreen) : Table(CameraStageBaseScreen.skin) {
class TileInfoTable(private val viewingCiv :CivilizationInfo) : Table(CameraStageBaseScreen.skin) {
init {
background = ImageGetter.getBackground(ImageGetter.getBlue().lerp(Color.BLACK, 0.5f))
}
internal fun updateTileTable(tile: TileInfo) {
clearChildren()
val civInfo = worldScreen.viewingCiv
if (UncivGame.Current.viewEntireMapForDebug || civInfo.exploredTiles.contains(tile.position)) {
if (UncivGame.Current.viewEntireMapForDebug || viewingCiv.exploredTiles.contains(tile.position)) {
add(getStatsTable(tile))
add(tile.toString().toLabel()).colspan(2).pad(10f)
add(tile.toString(viewingCiv).toLabel()).colspan(2).pad(10f)
}
pack()
@ -31,7 +30,7 @@ class TileInfoTable(private val worldScreen: WorldScreen) : Table(CameraStageBas
val table = Table()
table.defaults().pad(2f)
for (entry in tile.getTileStats(worldScreen.viewingCiv).toHashMap()
for (entry in tile.getTileStats(viewingCiv).toHashMap()
.filterNot { it.value == 0f || it.key.toString() == "" }) {
table.add(ImageGetter.getStatIcon(entry.key.toString())).size(20f).align(Align.right)
table.add(entry.value.toInt().toString().toLabel()).align(Align.left).padRight(10f)