package views import korlibs.image.color.* import korlibs.image.vector.* import korlibs.korge.ui.* import korlibs.korge.view.* import korlibs.math.geom.* import model.* import kotlin.math.* class BattlerView (val battler: Battler.Readonly) : UIContainer(Size(200.0, 40.0)) { private val staminaBar = graphics { drawStaminaBar() } private val healthBar = graphics { drawHealthBar() } private val staminaText = uiText("${battler.energy.roundToInt()}/${battler.stamina.roundToInt()}/${battler.maxStamina.roundToInt()}").xy(100, 25) private val healthText = uiText("${battler.composure.roundToInt()}/${battler.health.roundToInt()}/${battler.maxHealth.roundToInt()}").xy(100, 5) private val battlerName = uiText(battler.name) private var composure: Double = battler.composure private var health: Double = battler.health private var energy: Double = battler.energy private var stamina: Double = battler.stamina init { addUpdater { updateBattler() } } private fun ShapeBuilder.drawHealthBar() { if (battler.health > -battler.maxHealth) { fillRect(0, 10, 200 * (1 + battler.health.coerceAtMost(0.0) / battler.maxHealth), 20) fill(RGBA(0x5f, 0x08, 0x08, 0xff)) } if (battler.health > 0) { fillRect(0.0, 10.0, battler.health * 200.0 / battler.maxHealth, 20.0) fill(RGBA(0x8f, 0x10, 0x10, 0xff)) } if (battler.composure > 0) { fillRect(0.0, 10.0, battler.composure * 200.0 / battler.maxHealth, 20.0) fill(RGBA(0x10, 0xcf, 0x5f, 0xff)) } } private fun ShapeBuilder.drawStaminaBar() { fillRect(50, 30, 150, 10) fill(RGBA(0x10, 0x10, 0x10, 0xff)) if (battler.stamina > 0) { fillRect(50.0, 30.0, battler.stamina * 150.0 / battler.maxStamina, 10.0) fill(RGBA(0x10, 0x5f, 0x7f, 0xff)) } if (battler.energy > 0) { fillRect(50.0, 30.0, battler.energy * 150.0 / battler.maxStamina, 10.0) fill(RGBA(0x10, 0x9f, 0xaf, 0xff)) } } private fun updateHealthBar() { healthBar.updateShape { drawHealthBar() } healthText.text = "${battler.composure.roundToInt()}/${battler.health.roundToInt()}/${battler.maxHealth.roundToInt()}" } private fun updateStaminaBar() { staminaBar.updateShape { drawStaminaBar() } staminaText.text = "${battler.energy.roundToInt()}/${battler.stamina.roundToInt()}/${battler.maxStamina.roundToInt()}" } private fun updateBattler() { battlerName.text = battler.name if (composure != battler.composure || health != battler.health) { composure = battler.composure health = battler.health updateHealthBar() } if (energy != battler.energy || stamina != battler.stamina) { energy = battler.energy stamina = battler.stamina updateStaminaBar() } } }