71 lines
2.0 KiB
Kotlin
71 lines
2.0 KiB
Kotlin
package simulation.dice
|
|
|
|
import java.util.*
|
|
|
|
/**
|
|
* Interface for objects that can modify a value with a bonus.
|
|
*
|
|
* Implementations will generate a bonus value based on their internal logic.
|
|
*
|
|
* A [Random] object is provided if the bonus is variable.
|
|
*/
|
|
interface DiceModifier<T> {
|
|
|
|
/**
|
|
* Generates a bonus integer, potentially using the provided Random instance if needs be.
|
|
*
|
|
* @param r Random instance to use for random number generation
|
|
* @return a generated bonus integer
|
|
*/
|
|
fun getBonus(r: Random, crit: Boolean): T
|
|
}
|
|
|
|
/**
|
|
* A [DiceModifier] that generates a random bonus integer based on a provided [DiceRoller].
|
|
*
|
|
* On each call to [getBonus], it will roll the given [DiceRoller] using the passed [Random]
|
|
* instance and return the result as a positive bonus amount.
|
|
*
|
|
* @param dice The [DiceRoller] instance to use for generating bonus values.
|
|
*/
|
|
class DiceBonusModifier(private val dice: DiceRoller) : DiceModifier<Int> {
|
|
|
|
constructor(diceString: String):this(Dice.regular(diceString))
|
|
|
|
override fun getBonus(r: Random, crit: Boolean): Int {
|
|
return if (crit){
|
|
dice.roll(r).result + dice.roll(r).result
|
|
}else{
|
|
dice.roll(r).result
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* A [DiceModifier] that applies a random penalty based on a [DiceRoller].
|
|
*
|
|
* On each call to [getBonus], it will roll the provided [DiceRoller] object and return the
|
|
* result as a negative number.
|
|
*
|
|
* @param dice The [DiceRoller] to use for generating penalty values.
|
|
*/
|
|
class DicePenaltyModifier(private val dice: DiceRoller): DiceModifier<Int> {
|
|
override fun getBonus(r: Random, crit: Boolean): Int {//can penalties ever crit?
|
|
return -dice.roll(r).result
|
|
}
|
|
}
|
|
|
|
/**
|
|
* A [DiceModifier] that applies a fixed bonus amount.
|
|
*
|
|
* The bonus value is set on construction and does not vary.
|
|
*
|
|
* @param bonus The fixed bonus amount to apply.
|
|
*/
|
|
class FlatModifier(private val bonus: Int) : DiceModifier<Int> {
|
|
override fun getBonus(r: Random, crit: Boolean): Int {
|
|
return bonus
|
|
}
|
|
}
|