Made as a utility for game development in scala-games. Simple solution for the pattern of storing values that come with subscription-based events that broadcasts when the values are changed.
import lib.property.Property
class Character(val name: String, val maxHp: Int) {
val hp = Property(
maxHp,
i =>
require(
i <= maxHp && i >= 0,
"Cannot set hp to higher than max or lower than 0"
)
i
)
}
val wolf = new Character("Wolf", 70)
wolf.hp.value // : Int = 70
wolf.hp.onChange += ((o, n) => println(s"hp changed from $o to $n"))
wolf.hp.value = 54 // hp changed from 70 to 54
wolf.hp.value // : Int = 54
wolf.hp.value = 71 // java.lang.IllegalArgumentException: requirement failed: Cannot set hp to higher than max or lower than 0