Added CenterX and CenterY functions for actors

This commit is contained in:
Yair Morgenstern 2018-05-16 21:38:09 +03:00
parent 2c2a4e5da3
commit 9c576d6bdc
14 changed files with 67 additions and 63 deletions

View file

@ -1,6 +1,6 @@
buildscript {
ext.kotlinVersion = '1.2.31'
ext.kotlinVersion = '1.2.41'
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"

View file

@ -69,7 +69,7 @@ class Battle(val gameInfo:GameInfo) {
private fun getHealthDependantDamageRatio(combatant: ICombatant): Float {
if (combatant.getUnitType() == UnitType.City) return 1f
return 1 / 2f + combatant.getHealth() / 200f // Each point of health reduces damage dealt by 0.5%
return 1/2f + combatant.getHealth()/200f // Each point of health reduces damage dealt by 0.5%
}

View file

@ -39,7 +39,7 @@ class CityCombatant(val city: CityInfo) : ICombatant {
// 10% bonus foreach pop
val strengthWithPop = (baseStrength + strengthFromTechs) * (1 + 0.1*city.population.population)
return strengthWithPop.toInt() * 100 // *100 because a city is always at 100% strength
return strengthWithPop.toInt()
}
override fun toString(): String {return city.name} // for debug

View file

@ -18,15 +18,12 @@ class MapUnitCombatant(val unit: MapUnit) : ICombatant {
}
override fun getAttackingStrength(defender: ICombatant): Int {
val attackerStrength: Int
if (isRanged()) attackerStrength = unit.getBaseUnit().rangedStrength
else attackerStrength = unit.getBaseUnit().strength
return attackerStrength*unit.health
if (isRanged()) return unit.getBaseUnit().rangedStrength
else return unit.getBaseUnit().strength
}
override fun getDefendingStrength(attacker: ICombatant): Int {
// too: if ranged units get ranged attacked, they use their ranged str to defend!
return unit.getBaseUnit().strength*unit.health
return unit.getBaseUnit().strength
}
override fun getUnitType(): UnitType {

View file

@ -2,13 +2,13 @@ package com.unciv.models.gamebasics
import com.badlogic.gdx.graphics.Color
import com.unciv.models.stats.INamed
import com.unciv.ui.utils.fromRGB
import com.unciv.ui.utils.colorFromRGB
class Civilization : INamed {
override lateinit var name: String
lateinit var RGB: List<Int>
fun getColor(): Color {
return Color().fromRGB(RGB[0],RGB[1],RGB[2])
return colorFromRGB(RGB[0], RGB[1], RGB[2])
}
lateinit var cities: List<String>
}

View file

@ -11,6 +11,7 @@ import com.unciv.logic.map.TileInfo
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.HexMath
import com.unciv.ui.utils.ImageGetter
import com.unciv.ui.utils.centerX
import java.util.*
class CityScreen(internal val city: CityInfo) : CameraStageBaseScreen() {
@ -137,7 +138,7 @@ class CityScreen(internal val city: CityInfo) : CameraStageBaseScreen() {
cityPickerTable.add(nextCityButton)
}
cityPickerTable.pack()
cityPickerTable.setPosition(stage.width / 2 - cityPickerTable.width / 2, 0f)
cityPickerTable.centerX(stage)
stage.addActor(cityPickerTable)
}

View file

@ -6,6 +6,8 @@ import com.unciv.logic.city.CityInfo
import com.unciv.logic.map.TileInfo
import com.unciv.ui.tilegroups.TileGroup
import com.unciv.ui.utils.ImageGetter
import com.unciv.ui.utils.center
import com.unciv.ui.utils.centerX
class CityTileGroup(private val city: CityInfo, tileInfo: TileInfo) : TileGroup(tileInfo) {
@ -25,8 +27,8 @@ class CityTileGroup(private val city: CityInfo, tileInfo: TileInfo) : TileGroup(
updateUnitImage(true)
if(unitImage!=null) {
unitImage!!.setPosition(width / 2 - unitImage!!.width / 2 + 20,
height / 2 - unitImage!!.height / 2 + 20) // top
unitImage!!.center(this)
unitImage!!.y += 20 // top
}
updatePopulationImage()
@ -40,7 +42,8 @@ class CityTileGroup(private val city: CityInfo, tileInfo: TileInfo) : TileGroup(
yieldGroup.setOrigin(Align.center)
yieldGroup.setScale(0.7f)
yieldGroup.toFront()
yieldGroup.setPosition(width / 2 - yieldGroup.width / 2, height * 0.25f - yieldGroup.height / 2)
yieldGroup.centerX(this)
yieldGroup.y= height * 0.25f - yieldGroup.height / 2
}
private fun updatePopulationImage() {

View file

@ -8,7 +8,8 @@ import com.unciv.logic.map.RoadStatus
import com.unciv.logic.map.TileInfo
import com.unciv.ui.utils.HexMath
import com.unciv.ui.utils.ImageGetter
import com.unciv.ui.utils.fromRGB
import com.unciv.ui.utils.center
import com.unciv.ui.utils.colorFromRGB
open class TileGroup(var tileInfo: TileInfo) : Group() {
@ -34,8 +35,7 @@ open class TileGroup(var tileInfo: TileInfo) : Group() {
private fun addCircleImage() {
circleImage.width = 50f
circleImage.height = 50f
circleImage.setPosition(width / 2 - circleImage.width / 2,
height / 2 - circleImage.height / 2)
circleImage.center(this)
addActor(circleImage)
circleImage.isVisible = false
}
@ -44,8 +44,7 @@ open class TileGroup(var tileInfo: TileInfo) : Group() {
val imageScale = groupSize * 1.5f / hexagon.width
hexagon.setScale(imageScale)
hexagon.setOrigin(Align.center)
hexagon.setPosition((width - hexagon.width) / 2,
(height - hexagon.height) / 2)
hexagon.center(this)
hexagon.zIndex = 0
addActor(hexagon)
}
@ -54,8 +53,8 @@ open class TileGroup(var tileInfo: TileInfo) : Group() {
populationImage = ImageGetter.getImage("StatIcons/populationGreen.png")
populationImage!!.run {
setSize(20f, 20f)
setPosition(this@TileGroup.width/2 - width/2,
this@TileGroup.height/2 - height/2 - 20)
center(this@TileGroup)
y -= 20
} // top left
addActor(populationImage)
}
@ -98,8 +97,7 @@ open class TileGroup(var tileInfo: TileInfo) : Group() {
for(i in -2..2) {
val image = ImageGetter.getImage("UnitIcons/Circle.png")
image.setSize(5f, 5f)
image.moveBy(width / 2 - image.width / 2, // center
height / 2 - image.height / 2)
image.center(this)
// in addTiles, we set the position of groups by relative world position *0.8*groupSize, filter groupSize = 50
// Here, we want to have the borders start HALFWAY THERE and extend towards the tiles, so we give them a position of 0.8*25.
// BUT, we don't actually want it all the way out there, because we want to display the borders of 2 different civs!
@ -113,8 +111,6 @@ open class TileGroup(var tileInfo: TileInfo) : Group() {
image.moveBy(relativeWorldPosition.y*i * 4, -relativeWorldPosition.x*i * 4)
image.color = tileInfo.getOwner()!!.getCivilization().getColor()
//image.setOrigin(image.width / 2, image.height / 2) // This is so that the rotation is calculated from the middle of the road and not the edge
//image.rotation = (90 + 180 / Math.PI * Math.atan2(relativeWorldPosition.y.toDouble(), relativeWorldPosition.x.toDouble())).toFloat()
addActor(image)
borderImages.add(image)
}
@ -157,7 +153,7 @@ open class TileGroup(var tileInfo: TileInfo) : Group() {
private fun updateTileColor(isViewable: Boolean) {
val RGB = tileInfo.getBaseTerrain().RGB!!
hexagon.color = Color().fromRGB(RGB[0], RGB[1],RGB[2])
hexagon.color = colorFromRGB(RGB[0], RGB[1], RGB[2])
if (!isViewable) hexagon.color = hexagon.color.lerp(Color.BLACK, 0.6f)
}
@ -168,8 +164,7 @@ open class TileGroup(var tileInfo: TileInfo) : Group() {
terrainFeatureImage!!.run {
setSize(30f, 30f)
setColor(1f, 1f, 1f, 0.5f)
setPosition(this@TileGroup.width / 2 - width / 2,
this@TileGroup.height / 2 - height / 2)
center(this@TileGroup)
}
}
@ -185,9 +180,8 @@ open class TileGroup(var tileInfo: TileInfo) : Group() {
addActor(improvementImage)
improvementImage!!.run {
setSize(20f, 20f)
setPosition(this@TileGroup.width / 2 - width / 2 + 20f,
this@TileGroup.height / 2 - height / 2) // right
center(this@TileGroup)
this.x+=20 // right
}
improvementType = tileInfo.improvement
}
@ -198,8 +192,8 @@ open class TileGroup(var tileInfo: TileInfo) : Group() {
val fileName = "ResourceIcons/" + tileInfo.resource + "_(Civ5).png"
resourceImage = ImageGetter.getImage(fileName)
resourceImage!!.setSize(20f, 20f)
resourceImage!!.setPosition(width / 2 - resourceImage!!.width / 2 - 20f,
height / 2 - resourceImage!!.height / 2) // left
resourceImage!!.center(this)
resourceImage!!.x -= 20 // left
addActor(resourceImage!!)
}
}
@ -239,8 +233,7 @@ open class TileGroup(var tileInfo: TileInfo) : Group() {
setSize(background.width,background.height)
addActor(background)
}
unitBaseImage.setPosition(group.width/2-unitBaseImage.width/2,
group.height/2-unitBaseImage.height/2)
unitBaseImage.center(group)
group.addActor(unitBaseImage)
return group
}

View file

@ -10,6 +10,7 @@ import com.unciv.ui.cityscreen.CityScreen
import com.unciv.ui.cityscreen.addClickListener
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.ImageGetter
import com.unciv.ui.utils.center
class WorldTileGroup(tileInfo: TileInfo) : TileGroup(tileInfo) {
@ -18,8 +19,7 @@ class WorldTileGroup(tileInfo: TileInfo) : TileGroup(tileInfo) {
fun addWhiteCircleAroundUnit(){
val whiteCircle = ImageGetter.getImage("UnitIcons/Circle.png")
whiteCircle.setSize(25f,25f)
whiteCircle.setPosition(unitImage!!.width/2 - whiteCircle.width/2,
unitImage!!.height/2 - whiteCircle.height/2)
whiteCircle.center(unitImage!!)
unitImage!!.addActor(whiteCircle)
whiteCircle.toBack()
}
@ -38,8 +38,8 @@ class WorldTileGroup(tileInfo: TileInfo) : TileGroup(tileInfo) {
updateCityButton(city)
updateUnitImage(isViewable)
if(unitImage!=null) {
unitImage!!.setPosition(width / 2 - unitImage!!.width / 2,
height / 2 - unitImage!!.height / 2 + 20) // top
unitImage!!.center(this)
unitImage!!.y += 20 // top
}
}
@ -72,8 +72,7 @@ class WorldTileGroup(tileInfo: TileInfo) : TileGroup(tileInfo) {
toFront()
}
cityButton!!.setPosition(width / 2 - cityButton!!.width / 2,
height / 2 - cityButton!!.height / 2)
cityButton!!.center(this)
}
}

View file

@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.graphics.g2d.Batch
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.scenes.scene2d.Actor
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.ui.*
@ -81,8 +82,7 @@ open class CameraStageBaseScreen : Screen {
}
tutorialTable.add(button).pad(10f)
tutorialTable.pack()
tutorialTable.setPosition(stage.width / 2 - tutorialTable.width / 2,
stage.height / 2 - tutorialTable.height / 2)
tutorialTable.center(stage)
stage.addActor(tutorialTable)
}
@ -105,6 +105,14 @@ fun Button.enable() {
fun <E> List<E>.getRandom(): E = if (size == 0) throw Exception() else get((Math.random() * size).toInt())
fun Color.fromRGB(r:Int,g:Int,b:Int): Color {
fun colorFromRGB(r: Int, g: Int, b: Int): Color {
return Color(r/255f, g/255f, b/255f, 1f)
}
fun Actor.centerX(parent:Actor){ x = parent.width/2 - width/2 }
fun Actor.centerY(parent:Actor){ y = parent.height/2- height/2}
fun Actor.center(parent:Actor){ centerX(parent); centerY(parent)}
fun Actor.centerX(parent:Stage){ x = parent.width/2 - width/2 }
fun Actor.centerY(parent:Stage){ y = parent.height/2- height/2}
fun Actor.center(parent:Stage){ centerX(parent); centerY(parent)}

View file

@ -13,6 +13,7 @@ import com.unciv.logic.map.UnitType
import com.unciv.ui.cityscreen.addClickListener
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.ImageGetter
import com.unciv.ui.utils.centerX
import com.unciv.ui.utils.disable
import kotlin.math.max
@ -70,8 +71,8 @@ class BattleTable(val worldScreen: WorldScreen): Table() {
row().pad(5f)
add("Strength: "+attacker.getAttackingStrength(defender)/100f)
add("Strength: "+defender.getDefendingStrength(attacker)/100f)
add("Strength: "+attacker.getAttackingStrength(defender))
add("Strength: "+defender.getDefendingStrength(attacker))
row().pad(5f)
val attackerModifiers = battle.getAttackModifiers(attacker,defender) .map { it.key+": "+(if(it.value>0)"+" else "")+(it.value*100).toInt()+"%" }
@ -82,8 +83,8 @@ class BattleTable(val worldScreen: WorldScreen): Table() {
if (defenderModifiers.size > i) add(defenderModifiers[i]) else add()
row().pad(5f)
}
add((battle.getAttackingStrength(attacker,defender)/100f).toString())
add((battle.getDefendingStrength(attacker,defender)/100f).toString())
add(battle.getAttackingStrength(attacker,defender).toString())
add(battle.getDefendingStrength(attacker,defender).toString())
row().pad(5f)
var damageToDefender = battle.calculateDamageToDefender(attacker,defender)

View file

@ -9,7 +9,8 @@ import com.unciv.models.gamebasics.ResourceType
import com.unciv.ui.cityscreen.addClickListener
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.ImageGetter
import com.unciv.ui.utils.fromRGB
import com.unciv.ui.utils.centerY
import com.unciv.ui.utils.colorFromRGB
import kotlin.math.ceil
class CivStatsTable(val screen: WorldScreen) : Table() {
@ -18,10 +19,10 @@ class CivStatsTable(val screen: WorldScreen) : Table() {
.apply { fontColor = Color.valueOf("f5f5f5ff") }
private val turnsLabel = Label("Turns: 0/400", labelStyle)
private val goldLabel = Label("Gold:",labelStyle).apply { color = Color().fromRGB(225,217,71) }
private val scienceLabel = Label("Science:",labelStyle).apply { color = Color().fromRGB(78,140,151) }
private val goldLabel = Label("Gold:",labelStyle).apply { color = colorFromRGB(225, 217, 71) }
private val scienceLabel = Label("Science:",labelStyle).apply { color = colorFromRGB(78, 140, 151) }
private val happinessLabel = Label("Happiness:",labelStyle)
private val cultureLabel = Label("Culture:",labelStyle).apply { color = Color().fromRGB(210,94,210) }
private val cultureLabel = Label("Culture:",labelStyle).apply { color = colorFromRGB(210, 94, 210) }
private val resourceLabels = HashMap<String, Label>()
private val resourceImages = HashMap<String, Image>()
private val happinessImage = ImageGetter.getStatIcon("Happiness")
@ -84,7 +85,7 @@ class CivStatsTable(val screen: WorldScreen) : Table() {
menuButton.addClickListener {
screen.optionsTable.isVisible = !screen.optionsTable.isVisible
}
menuButton.y = this.height/2-menuButton.height/2
menuButton.centerY(this)
menuButton.x = menuButton.y
return menuButton
}
@ -126,7 +127,7 @@ class CivStatsTable(val screen: WorldScreen) : Table() {
happinessImage.drawable = ImageGetter.getStatIcon("Malcontent").drawable
}
else{
happinessLabel.color = Color().fromRGB(92,194,77)
happinessLabel.color = colorFromRGB(92, 194, 77)
happinessImage.drawable = ImageGetter.getStatIcon("Happiness").drawable
}

View file

@ -6,6 +6,7 @@ import com.badlogic.gdx.scenes.scene2d.Group
import com.badlogic.gdx.scenes.scene2d.InputEvent
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane
import com.badlogic.gdx.scenes.scene2d.utils.ActorGestureListener
import com.badlogic.gdx.utils.Align
import com.unciv.logic.civilization.CivilizationInfo
import com.unciv.logic.map.TileInfo
import com.unciv.logic.map.TileMap
@ -13,7 +14,7 @@ import com.unciv.logic.map.UnitType
import com.unciv.ui.cityscreen.addClickListener
import com.unciv.ui.tilegroups.WorldTileGroup
import com.unciv.ui.utils.HexMath
import com.unciv.ui.utils.fromRGB
import com.unciv.ui.utils.colorFromRGB
class TileMapHolder(internal val worldScreen: WorldScreen, internal val tileMap: TileMap, internal val civInfo: CivilizationInfo) : ScrollPane(null) {
internal var selectedTile: TileInfo? = null
@ -63,7 +64,7 @@ class TileMapHolder(internal val worldScreen: WorldScreen, internal val tileMap:
widget = allTiles
setFillParent(true)
setOrigin(worldScreen.stage.width / 2, worldScreen.stage.height / 2)
setOrigin(Align.center)
setSize(worldScreen.stage.width, worldScreen.stage.height)
addListener(object : ActorGestureListener() {
var lastScale = 1f
@ -110,10 +111,10 @@ class TileMapHolder(internal val worldScreen: WorldScreen, internal val tileMap:
}
for(tile: TileInfo in unit.getDistanceToTiles().keys)
tileGroups[tile]!!.showCircle(Color().fromRGB(0,120,215))
tileGroups[tile]!!.showCircle(colorFromRGB(0, 120, 215))
for (tile in attackableTiles.filter { it.unit!=null && it.unit!!.owner != unit.owner && civViewableTiles.contains(it)})
tileGroups[tile]!!.showCircle(Color().fromRGB(237,41,57))
tileGroups[tile]!!.showCircle(colorFromRGB(237, 41, 57))
}
if(selectedTile!=null)

View file

@ -12,6 +12,7 @@ import com.unciv.ui.cityscreen.addClickListener
import com.unciv.ui.pickerscreens.PolicyPickerScreen
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.ImageGetter
import com.unciv.ui.utils.center
class WorldScreenOptionsTable internal constructor(worldScreen: WorldScreen, private val civInfo: CivilizationInfo) : Table() {
@ -67,7 +68,6 @@ class WorldScreenOptionsTable internal constructor(worldScreen: WorldScreen, pri
add(closeButton)
pack() // Needed to show the background.
setPosition(worldScreen.stage.width / 2 - width / 2,
worldScreen.stage.height / 2 - height / 2)
center(worldScreen.stage)
}
}