Battle table indicates damage with health bars

This commit is contained in:
Yair Morgenstern
2019-02-15 13:48:29 +02:00
parent b38c615056
commit 0cd5426440
7 changed files with 43 additions and 11 deletions

View File

@ -7,6 +7,10 @@ import com.unciv.models.gamebasics.GameBasics
import com.unciv.models.gamebasics.unit.UnitType
class CityCombatant(val city: CityInfo) : ICombatant {
override fun getMaxHealth(): Int {
return city.getMaxHealth()
}
override fun getHealth(): Int = city.health
override fun getCivilization(): CivilizationInfo = city.civInfo
override fun getTile(): TileInfo = city.getCenterTile()

View File

@ -1,13 +1,13 @@
package com.unciv.logic.battle
import com.unciv.logic.civilization.CivilizationInfo
import com.unciv.logic.map.MapUnit
import com.unciv.logic.map.TileInfo
import com.unciv.models.gamebasics.unit.UnitType
interface ICombatant{
fun getName(): String
fun getHealth():Int
fun getMaxHealth():Int
fun getUnitType(): UnitType
fun getAttackingStrength(): Int
fun getDefendingStrength(): Int

View File

@ -7,6 +7,7 @@ import com.unciv.models.gamebasics.unit.UnitType
class MapUnitCombatant(val unit: MapUnit) : ICombatant {
override fun getHealth(): Int = unit.health
override fun getMaxHealth() = 100
override fun getCivilization(): CivilizationInfo = unit.civInfo
override fun getTile(): TileInfo = unit.getTile()
override fun getName(): String = unit.name

View File

@ -267,7 +267,7 @@ open class TileGroup(var tileInfo: TileInfo) : Group() {
}
if (roadStatus == RoadStatus.None) continue // no road image
val image = if (roadStatus == RoadStatus.Road) ImageGetter.getWhiteDot().apply { color = Color.BROWN }
val image = if (roadStatus == RoadStatus.Road) ImageGetter.getDot(Color.BROWN)
else ImageGetter.getImage("OtherIcons/Railroad.png")
roadImage.image = image

View File

@ -24,7 +24,7 @@ object ImageGetter {
// and the atlas is what tells us what was packed where.
var atlas = TextureAtlas("game.atlas")
fun getWhiteDot() = getImage(whiteDotLocation)
fun getDot(dotColor: Color) = getWhiteDot().apply { color = dotColor}
fun getExternalImage(fileName:String): Image {
return Image(TextureRegion(Texture("ExtraImages/$fileName.png")))
@ -158,7 +158,7 @@ object ImageGetter {
healthPercent > 1 / 3f -> Color.ORANGE
else -> Color.RED
}
val emptyPartOfBar = ImageGetter.getWhiteDot().apply { color = Color.BLACK }
val emptyPartOfBar = ImageGetter.getDot(Color.BLACK)
healthBar.add(healthPartOfBar).width(healthBarSize * healthPercent).height(5f)
healthBar.add(emptyPartOfBar).width(healthBarSize * (1 - healthPercent)).height(5f)
healthBar.pack()

View File

@ -1,6 +1,9 @@
package com.unciv.ui.worldscreen.bottombar
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.actions.Actions
import com.badlogic.gdx.scenes.scene2d.actions.RepeatAction
import com.badlogic.gdx.scenes.scene2d.ui.Image
import com.badlogic.gdx.scenes.scene2d.ui.Label
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
@ -118,12 +121,10 @@ class BattleTable(val worldScreen: WorldScreen): Table() {
add("{Captured!}".tr())
}
else {
add("{Health}: ".tr() + attacker.getHealth().toString() + " -> "
+ (attacker.getHealth() - damageToAttacker))
add("{Health}: ".tr() + defender.getHealth().toString() + " -> "
+ (defender.getHealth() - damageToDefender))
else {
add(getHealthBar(attacker.getHealth(), attacker.getMaxHealth(), damageToAttacker))
add(getHealthBar(defender.getHealth(), defender.getMaxHealth(), damageToDefender))
}
row().pad(5f)
@ -171,4 +172,32 @@ class BattleTable(val worldScreen: WorldScreen): Table() {
setPosition(worldScreen.stage.width/2-width/2, 5f)
}
fun getHealthBar(currentHealth: Int, maxHealth: Int, expectedDamage:Int): Table {
val healthBar = Table()
val totalWidth = 100f
fun addHealthToBar(image: Image, amount:Int){
healthBar.add(image).size(amount*totalWidth/maxHealth,3f)
}
addHealthToBar(ImageGetter.getDot(Color.BLACK), maxHealth-currentHealth)
val damagedHealth = ImageGetter.getDot(Color.RED)
damagedHealth.addAction(Actions.repeat(RepeatAction.FOREVER, Actions.sequence(
Actions.color(Color.BLACK,0.7f),
Actions.color(Color.RED,0.7f)
)))
addHealthToBar(damagedHealth,expectedDamage)
val remainingHealth = currentHealth-expectedDamage
val remainingHealthDot = ImageGetter.getWhiteDot()
remainingHealthDot.color = when {
remainingHealth / maxHealth.toFloat() > 2 / 3f -> Color.GREEN
remainingHealth / maxHealth.toFloat() > 1 / 3f -> Color.ORANGE
else -> Color.RED
}
addHealthToBar(remainingHealthDot ,remainingHealth)
healthBar.pack()
return healthBar
}
}

View File

@ -1,6 +1,5 @@
package com.unciv.ui.worldscreen.bottombar
import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.unciv.logic.map.TileInfo
import com.unciv.ui.worldscreen.WorldScreen
@ -11,7 +10,6 @@ class WorldScreenBottomBar(val worldScreen: WorldScreen) : Table(){
val tileInfoTable = TileInfoTable(worldScreen)
init {
touchable= Touchable.enabled
add(unitTable).width(worldScreen.stage.width/3).fill()
add().width(worldScreen.stage.width/3) // empty space for the battle table
add(tileInfoTable).width(worldScreen.stage.width/3).fill()