59 lines
1.6 KiB
Scala
59 lines
1.6 KiB
Scala
package fmon.battle
|
|
|
|
import scala.util.Random
|
|
|
|
import fmon.stat._
|
|
import fmon.stat.Statistic._
|
|
import fmon.util.Fraction
|
|
|
|
class BattleEngine(val player: Party, val enemy: Party)(implicit val rng: Random) {
|
|
|
|
def play() = {
|
|
println(player.lead.level)
|
|
while (player.canFight && enemy.canFight) {
|
|
playTurn()
|
|
}
|
|
}
|
|
|
|
def playTurn() = {
|
|
val playerAction = player.pollAction(enemy)
|
|
val enemyAction = enemy.pollAction(player)
|
|
|
|
val actions = Seq(playerAction, enemyAction)
|
|
val queue = rng.shuffle(actions).sorted // Shuffle to randomize in the event of a tie
|
|
queue.foreach(useMove)
|
|
val eotQueue = Seq(player.lead, enemy.lead).sortBy(_(Speed))
|
|
eotQueue.foreach(mon => mon.statuses.map(_.onResidual(mon)))
|
|
|
|
if (!player.lead.isAlive) {
|
|
if (player.canFight) {
|
|
val replace = player.pollReplacement(enemy)
|
|
player.switchIn(replace)
|
|
} else {
|
|
println(s"${player.trainer} has lost!")
|
|
}
|
|
}
|
|
if (!enemy.lead.isAlive) {
|
|
if (enemy.canFight) {
|
|
val replace = enemy.pollReplacement(player)
|
|
enemy.switchIn(replace)
|
|
} else {
|
|
println(s"${enemy.trainer} has lost!")
|
|
}
|
|
}
|
|
|
|
println(s"${player.lead}(${player.lead.hp}/${player.lead(Hp)})")
|
|
println(s"${enemy.lead}(${enemy.lead.hp}/${enemy.lead(Hp)})")
|
|
}
|
|
|
|
def useMove(action: Action) = {
|
|
val user = action.user
|
|
val move = action.move
|
|
val target = action.target
|
|
if (user.isAlive) {
|
|
if (user.statuses.forall(_.onBeforeMove(user, move, target))) {
|
|
move.useMove(user, target)
|
|
}
|
|
}
|
|
}
|
|
} |