You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
3.1 KiB
89 lines
3.1 KiB
package views
|
|
|
|
import korlibs.image.paint.*
|
|
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) : 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 {
|
|
battler.advanceTo(battler.currentUpdate + it)
|
|
updateBattler()
|
|
}
|
|
}
|
|
|
|
private fun ShapeBuilder.drawHealthBar() {
|
|
if (battler.health > -battler.maxHealth) {
|
|
fillRect(0, 10, 200 * (1 + battler.health.coerceAtMost(0.0) / battler.maxHealth), 20)
|
|
fill(ColorPaint(0x5f, 0x08, 0x08, 0xff))
|
|
}
|
|
if (battler.health > 0) {
|
|
fillRect(0.0, 10.0, battler.health * 200.0 / battler.maxHealth, 20.0)
|
|
fill(ColorPaint(0x8f, 0x10, 0x10, 0xff))
|
|
}
|
|
if (battler.composure > 0) {
|
|
fillRect(0.0, 10.0, battler.composure * 200.0 / battler.maxHealth, 20.0)
|
|
fill(ColorPaint(0x10, 0xcf, 0x5f, 0xff))
|
|
}
|
|
}
|
|
|
|
private fun ShapeBuilder.drawStaminaBar() {
|
|
fillRect(50, 30, 150, 10)
|
|
fill(ColorPaint(0x10, 0x10, 0x10, 0xff))
|
|
if (battler.stamina > 0) {
|
|
fillRect(50.0, 30.0, battler.stamina * 150.0 / battler.maxStamina, 10.0)
|
|
fill(ColorPaint(0x10, 0x5f, 0x7f, 0xff))
|
|
}
|
|
if (battler.energy > 0) {
|
|
fillRect(50.0, 30.0, battler.energy * 150.0 / battler.maxStamina, 10.0)
|
|
fill(ColorPaint(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()
|
|
}
|
|
}
|
|
}
|
|
|