Skip to content

Commit b625207

Browse files
author
adam-arold
committed
Add displaying stats
1 parent 7e16922 commit b625207

7 files changed

Lines changed: 108 additions & 5 deletions

File tree

src/main/kotlin/org/hexworks/cavesofzircon/attributes/CombatStats.kt

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,52 @@
11
package org.hexworks.cavesofzircon.attributes
22

3-
import org.hexworks.amethyst.api.Attribute
3+
import org.hexworks.cavesofzircon.extensions.toStringProperty
44
import org.hexworks.cobalt.databinding.api.createPropertyFrom
5+
import org.hexworks.cobalt.databinding.api.expression.concat
56
import org.hexworks.cobalt.databinding.api.property.Property
7+
import org.hexworks.zircon.api.Components
68

79
data class CombatStats(val maxHpProperty: Property<Int>,
810
val hpProperty: Property<Int> = createPropertyFrom(maxHpProperty.value),
911
val attackValueProperty: Property<Int>,
10-
val defenseValueProperty: Property<Int>) : Attribute {
12+
val defenseValueProperty: Property<Int>) : DisplayableAttribute {
1113
val maxHp: Int by maxHpProperty.asDelegate()
1214
var hp: Int by hpProperty.asDelegate()
1315
val attackValue: Int by attackValueProperty.asDelegate()
1416
val defenseValue: Int by defenseValueProperty.asDelegate()
1517

18+
override fun toComponent(width: Int) = Components.vbox()
19+
.withSize(width, 5)
20+
.build().apply {
21+
val hpLabel = Components.label()
22+
.withSize(width, 1)
23+
.build()
24+
val attackLabel = Components.label()
25+
.withSize(width, 1)
26+
.build()
27+
val defenseLabel = Components.label()
28+
.withSize(width, 1)
29+
.build()
30+
31+
hpLabel.textProperty.updateFrom(createPropertyFrom("HP: ")
32+
.concat(hpProperty.toStringProperty())
33+
.concat("/").concat(maxHpProperty))
34+
35+
attackLabel.textProperty.updateFrom(createPropertyFrom("Att: ")
36+
.concat(attackValueProperty.toStringProperty()))
37+
38+
defenseLabel.textProperty.updateFrom(createPropertyFrom("Def: ")
39+
.concat(defenseValueProperty.toStringProperty()))
40+
41+
addComponent(Components.textBox()
42+
.withContentWidth(width)
43+
.addHeader("Combat Stats"))
44+
addComponent(hpLabel)
45+
addComponent(attackLabel)
46+
addComponent(defenseLabel)
47+
48+
}
49+
1650
companion object {
1751

1852
fun create(maxHp: Int, hp: Int = maxHp, attackValue: Int, defenseValue: Int) =
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.hexworks.cavesofzircon.attributes
2+
3+
import org.hexworks.amethyst.api.Attribute
4+
import org.hexworks.zircon.api.component.Component
5+
6+
interface DisplayableAttribute : Attribute {
7+
8+
fun toComponent(width: Int): Component
9+
10+
}
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.hexworks.cavesofzircon.attributes
22

3-
import org.hexworks.amethyst.api.Attribute
3+
import org.hexworks.cavesofzircon.extensions.toStringProperty
44
import org.hexworks.cobalt.databinding.api.createPropertyFrom
5+
import org.hexworks.cobalt.databinding.api.expression.concat
6+
import org.hexworks.zircon.api.Components
57

68
class EnergyLevel(initialEnergy: Int,
7-
val maxEnergy: Int) : Attribute {
9+
val maxEnergy: Int) : DisplayableAttribute {
810

911
var currentEnergy: Int
1012
get() = currentValueProperty.value
@@ -14,4 +16,20 @@ class EnergyLevel(initialEnergy: Int,
1416

1517
private val currentValueProperty = createPropertyFrom(initialEnergy)
1618

19+
override fun toComponent(width: Int) = Components.vbox()
20+
.withSize(width, 5)
21+
.build().apply {
22+
val hungerLabel = Components.label()
23+
.withSize(width, 1)
24+
.build()
25+
26+
hungerLabel.textProperty.updateFrom(currentValueProperty.toStringProperty()
27+
.concat("/").concat(maxEnergy))
28+
29+
addComponent(Components.textBox()
30+
.withContentWidth(width)
31+
.addHeader("Hunger"))
32+
addComponent(hungerLabel)
33+
34+
}
1735
}

src/main/kotlin/org/hexworks/cavesofzircon/attributes/Vision.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package org.hexworks.cavesofzircon.attributes
22

33
import org.hexworks.amethyst.api.Attribute
44

5-
data class Vision(val radius: Int): Attribute
5+
data class Vision(val radius: Int) : Attribute
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.hexworks.cavesofzircon.extensions
2+
3+
import org.hexworks.cobalt.databinding.api.createPropertyFrom
4+
import org.hexworks.cobalt.databinding.api.property.Property
5+
6+
fun Property<Int>.toStringProperty(): Property<String> {
7+
val intProp = this
8+
return createPropertyFrom("").apply {
9+
this.updateFrom(intProp) {
10+
it.toString()
11+
}
12+
}
13+
}

src/main/kotlin/org/hexworks/cavesofzircon/view/PlayView.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.hexworks.cavesofzircon.view
33
import org.hexworks.cavesofzircon.GameConfig
44
import org.hexworks.cavesofzircon.blocks.GameBlock
55
import org.hexworks.cavesofzircon.events.GameLogEvent
6+
import org.hexworks.cavesofzircon.view.fragment.PlayerStatsFragment
67
import org.hexworks.cavesofzircon.world.Game
78
import org.hexworks.cavesofzircon.world.GameBuilder
89
import org.hexworks.cobalt.events.api.subscribe
@@ -33,6 +34,10 @@ class PlayView(private val game: Game = GameBuilder.defaultGame()) : BaseView()
3334
.wrapWithBox()
3435
.build()
3536

37+
sidebar.addFragment(PlayerStatsFragment(
38+
width = sidebar.contentSize.width,
39+
player = game.player))
40+
3641
screen.addComponent(sidebar)
3742

3843
val logArea = Components.logArea()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.hexworks.cavesofzircon.view.fragment
2+
3+
import org.hexworks.cavesofzircon.attributes.DisplayableAttribute
4+
import org.hexworks.cavesofzircon.attributes.types.Player
5+
import org.hexworks.cavesofzircon.extensions.GameEntity
6+
import org.hexworks.zircon.api.Components
7+
import org.hexworks.zircon.api.component.Fragment
8+
9+
class PlayerStatsFragment(
10+
width: Int,
11+
player: GameEntity<Player>) : Fragment {
12+
13+
override val root = Components.vbox()
14+
.withSize(width, 30)
15+
.withSpacing(1)
16+
.build().apply {
17+
addComponent(Components.header().withText("Player"))
18+
player.attributes.toList().filterIsInstance<DisplayableAttribute>()
19+
.forEach {
20+
addComponent(it.toComponent(width))
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)