From 0b52e91a3d41c314ed6e3fdd3086714fe636b41f Mon Sep 17 00:00:00 2001 From: James Daly Date: Thu, 13 Jun 2019 22:22:57 -0400 Subject: [PATCH] Connected the battle engine to the UI. Beginning to make messages appear in the UI --- FakeMon/src/fmon/Game.scala | 2 +- FakeMon/src/fmon/battle/BattleEngine.scala | 14 +++---- FakeMon/src/fmon/battle/BattleUI.scala | 27 ++++++++++++-- FakeMon/src/fmon/battle/MoveButton.scala | 7 ++-- FakeMon/src/fmon/battle/battle.fxml | 4 +- FakeMon/src/fmon/battle/msg/Message.scala | 11 ++++++ FakeMon/src/fmon/stat/Ability.scala | 18 +++++---- FakeMon/src/fmon/stat/Monster.scala | 3 +- FakeMon/src/fmon/stat/Move.scala | 43 +++++++++++----------- FakeMon/src/fmon/stat/Status.scala | 33 +++++++++++------ FakeMon/src/fmon/stat/data/statuses.yaml | 8 ++-- 11 files changed, 109 insertions(+), 61 deletions(-) create mode 100644 FakeMon/src/fmon/battle/msg/Message.scala diff --git a/FakeMon/src/fmon/Game.scala b/FakeMon/src/fmon/Game.scala index 3b0cfeb..d913120 100644 --- a/FakeMon/src/fmon/Game.scala +++ b/FakeMon/src/fmon/Game.scala @@ -40,7 +40,7 @@ class Game extends Application { val url = getClass.getResource("battle/battle.fxml") val loader = new FXMLLoader(url) val root: Parent = loader.load() - val controller = loader.getController[BattleUI]() + implicit val controller = loader.getController[BattleUI]() val scene: Scene = new Scene(root) implicit val rng = new scala.util.Random() diff --git a/FakeMon/src/fmon/battle/BattleEngine.scala b/FakeMon/src/fmon/battle/BattleEngine.scala index e89d458..dddd789 100644 --- a/FakeMon/src/fmon/battle/BattleEngine.scala +++ b/FakeMon/src/fmon/battle/BattleEngine.scala @@ -2,11 +2,12 @@ package fmon.battle import scala.util.Random +import fmon.battle.msg._ import fmon.stat._ import fmon.stat.Statistic._ import fmon.util.Fraction -class BattleEngine(val player: Party, val enemy: Party)(implicit val rng: Random) { +class BattleEngine(val player: Party, val enemy: Party)(implicit val reader: SignalConsumer, val rng: Random) extends SignalConsumer { def play() = { println(player.lead.level) @@ -15,8 +16,6 @@ class BattleEngine(val player: Party, val enemy: Party)(implicit val rng: Random } } - - def playTurn(): Unit = { val playerAction = player.pollAction(enemy) playTurn(playerAction) @@ -36,7 +35,7 @@ class BattleEngine(val player: Party, val enemy: Party)(implicit val rng: Random val replace = player.pollReplacement(enemy) player.switchIn(replace) } else { - println(s"${player.trainer} has lost!") + this ! Message(s"${player.trainer} has lost!") } } if (!enemy.lead.isAlive) { @@ -47,9 +46,6 @@ class BattleEngine(val player: Party, val enemy: Party)(implicit val rng: Random 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) = { @@ -62,4 +58,8 @@ class BattleEngine(val player: Party, val enemy: Party)(implicit val rng: Random } } } + + def !(msg: Signal) = { + reader ! msg + } } \ No newline at end of file diff --git a/FakeMon/src/fmon/battle/BattleUI.scala b/FakeMon/src/fmon/battle/BattleUI.scala index a4e5405..67ac895 100644 --- a/FakeMon/src/fmon/battle/BattleUI.scala +++ b/FakeMon/src/fmon/battle/BattleUI.scala @@ -10,10 +10,11 @@ import scalafx.scene.Parent import scalafx.scene.control._ import scalafx.scene.layout._ +import fmon.battle.msg._ import fmon.stat._ import fmon.stat.Statistic._ -class BattleUI { +class BattleUI extends SignalConsumer { @FXML var usName: jfxsc.Label = _ @FXML var usLv: jfxsc.Label = _ @FXML var usHealth: jfxsc.Label = _ @@ -23,32 +24,50 @@ class BattleUI { @FXML var themHealth: jfxsc.Label = _ @FXML var themHealthBar: jfxsc.ProgressBar = _ @FXML var buttonPane: javafx.scene.layout.VBox = _ + @FXML var messages: jfxsc.TextArea = _ private var engine: BattleEngine = _ def setEngine(engine: BattleEngine): Unit = { + this.engine = engine + updateUI() + } + + def updateUI(): Unit = { val player = engine.player.lead usName.text = player.name usLv.text = s"Lv. ${player.level}" usHealth.text = s"${player.hp}/${player.maxhp}" - usHealthBar.progress = player.hp / player.maxhp + usHealthBar.progress = player.hp.toDouble / player.maxhp val enemy = engine.enemy.lead themName.text = enemy.name themLv.text = s"Lv. ${enemy.level}" themHealth.text = s"${enemy.hp}/${enemy.maxhp}" - themHealthBar.progress = enemy.hp / enemy.maxhp + themHealthBar.progress = enemy.hp.toDouble / enemy.maxhp val buttons = player.base.moves.map(move => loadMoveButton(move)) buttonPane.children = buttons } + def onMove(move: Move): Unit = { + val action = Action(engine.player.lead, move, engine.enemy.lead) + engine.playTurn(action) + updateUI() + } + + def !(msg: Signal): Unit = { + msg match { + case Message(text) => messages.text = s"${messages.text()}${text}\n" + } + } + private def loadMoveButton(move: Move): Button = { val fxmlLoader = new FXMLLoader(getClass.getResource("moveButton.fxml")) fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory()) val button = fxmlLoader.load[jfxsc.Button]() val controller = fxmlLoader.getController[MoveButton]() - controller.setup(move) + controller.setup(move, onMove) button } } \ No newline at end of file diff --git a/FakeMon/src/fmon/battle/MoveButton.scala b/FakeMon/src/fmon/battle/MoveButton.scala index 8aa62c5..f4346b7 100644 --- a/FakeMon/src/fmon/battle/MoveButton.scala +++ b/FakeMon/src/fmon/battle/MoveButton.scala @@ -13,15 +13,16 @@ class MoveButton { @FXML var moveName: jfxsc.Label = _ @FXML var moveUses: jfxsc.Label = _ private var move: Move = _ + private var callback: Move => Unit = _ - def setup(mv: Move): Unit = { + def setup(mv: Move, callback: Move => Unit): Unit = { move = mv moveName.text = mv.name moveUses.text = s"${move.pp}/${move.pp}" + this.callback = callback } def moveActivate(): Unit = { - println("We should take a turn here") - println(s"${moveBtn.width}") + callback(move) } } \ No newline at end of file diff --git a/FakeMon/src/fmon/battle/battle.fxml b/FakeMon/src/fmon/battle/battle.fxml index dd88064..e37421d 100644 --- a/FakeMon/src/fmon/battle/battle.fxml +++ b/FakeMon/src/fmon/battle/battle.fxml @@ -7,6 +7,7 @@ + @@ -132,11 +133,12 @@ - + +