From 97960ec734ff62b23e188f295a5b6fc5aa70e076 Mon Sep 17 00:00:00 2001 From: Matt Soucy Date: Sat, 7 Dec 2019 13:07:03 -0500 Subject: [PATCH] Report damage & handle selection --- .../src/me/msoucy/ptures/controller/Engine.kt | 6 +-- core/src/me/msoucy/ptures/model/Creature.kt | 4 +- core/src/me/msoucy/ptures/view/TextView.kt | 53 +++++++++++++++---- core/src/me/msoucy/ptures/view/ViewBase.kt | 3 +- 4 files changed, 51 insertions(+), 15 deletions(-) diff --git a/core/src/me/msoucy/ptures/controller/Engine.kt b/core/src/me/msoucy/ptures/controller/Engine.kt index 3409e35..54eedac 100644 --- a/core/src/me/msoucy/ptures/controller/Engine.kt +++ b/core/src/me/msoucy/ptures/controller/Engine.kt @@ -69,7 +69,7 @@ class Engine(private val battle: BattleType) { // Get the moves each creature will use this turn activeCreatures.forEach { - it.chooseSkill(activeCreatures.map { c -> c.creature }) + it.chooseSkill(activeCreatures) } // Resolve each move @@ -83,8 +83,8 @@ class Engine(private val battle: BattleType) { for (step in skill.damageSteps) { for (targetCreature in targets) { if (attackingCreature.creature.hits(targetCreature, step)) { - targetCreature.apply(step, attackingCreature.creature) - step.applyStatus(targetCreature) + val dmg = targetCreature.apply(step, attackingCreature.creature) + view.reportDamage(dmg) } } } diff --git a/core/src/me/msoucy/ptures/model/Creature.kt b/core/src/me/msoucy/ptures/model/Creature.kt index f74f075..1e2cb8b 100644 --- a/core/src/me/msoucy/ptures/model/Creature.kt +++ b/core/src/me/msoucy/ptures/model/Creature.kt @@ -22,7 +22,7 @@ class Creature { val spd : Int get() = statFormula(genes.spd, growth.spd) val maxHp : Int get() = hpStatFormula(genes.hp, growth.hp) - fun apply(dmg : Damage, attacker : Creature) { + fun apply(dmg : Damage, attacker : Creature) : Int { val critical = if (random(32) < attacker.genes.spd) { 1.5 } else { 1.0 } val randVal = random(0.85f, 1.0f) val atkAttribute = attacker.attributes.getOrElse(dmg.attribute) {1.0f} @@ -39,6 +39,8 @@ class Creature { } else { dmg.applyStatus(this) } + + return total } fun addStatus(status : Status) { diff --git a/core/src/me/msoucy/ptures/view/TextView.kt b/core/src/me/msoucy/ptures/view/TextView.kt index 801572f..4d7a20a 100644 --- a/core/src/me/msoucy/ptures/view/TextView.kt +++ b/core/src/me/msoucy/ptures/view/TextView.kt @@ -3,6 +3,7 @@ package me.msoucy.ptures.view import me.msoucy.ptures.model.Creature import me.msoucy.ptures.model.Skill import me.msoucy.ptures.model.SkillChoice +import me.msoucy.ptures.model.Target import me.msoucy.ptures.model.VisibleStatus class SkillViewText(skill: Skill) : SkillView(skill) { @@ -10,7 +11,7 @@ class SkillViewText(skill: Skill) : SkillView(skill) { println(skill.name) } - override fun displayEnumerated(idx : Int) { + override fun displayEnumerated(idx: Int) { println("- ${skill.name}") } } @@ -25,28 +26,52 @@ class CreatureViewText(playerId: Int, creature: Creature) : CreatureView(playerI skillViews.forEachIndexed { index, skillView -> skillView.displayEnumerated(index) } - var chosenSkill : Skill? = null + var chosenSkill: Skill? = null while (chosenSkill == null) { print("> ") val tmpName = readLine() ?: "" val possibleSkills = creature.skills.filter { it.name == tmpName } - if(possibleSkills.isNotEmpty()) - { + if (possibleSkills.isNotEmpty()) { chosenSkill = possibleSkills.first() } } return chosenSkill } - private fun chooseTarget(skill: Skill, possibleTargets: List): List { - return possibleTargets + private fun selectSingleTarget(possibleTargets: List): Creature { + println("Targets:") + println("========") + possibleTargets.forEach { c -> + println("- ${c.creature.name}") + } + var chosen: Creature? = null + while (chosen == null) { + print("> ") + val tmpName = readLine() ?: "" + val possible = possibleTargets.filter { it.creature.name == tmpName } + if (possible.isNotEmpty()) { + chosen = possible.first().creature + } + } + return chosen } - override fun chooseSkill(possibleTargets: List) { + private fun chooseTargets(skill: Skill, possibleTargets: List): List { + val target = skill.damageSteps[0].target + return when (target) { + Target.Self -> listOf(creature) + Target.Selected -> listOf(selectSingleTarget(possibleTargets)) + Target.Others -> possibleTargets.filter { it.creature != creature }.map { it.creature } + Target.Opponents -> possibleTargets.filter { it.playerId != playerId }.map { it.creature } + Target.All -> possibleTargets.map { it.creature } + } + } + + override fun chooseSkill(possibleTargets: List) { if (creature.activeSkill == null) { val skill = chooseSkillName() - val targets = chooseTarget(skill, possibleTargets) + val targets = chooseTargets(skill, possibleTargets) creature.activeSkill = SkillChoice(skill, targets) } } @@ -67,7 +92,7 @@ class CreatureViewText(playerId: Int, creature: Creature) : CreatureView(playerI override fun displayStatuses() { val displayables = creature.statuses.filterIsInstance() - if(displayables.isNotEmpty()) { + if (displayables.isNotEmpty()) { println("Statuses") println("--------") displayables.forEach { @@ -83,8 +108,16 @@ class PlayerViewText(playerId: Int) : PlayerView(playerId) { class BattleViewText : BattleView() { - override fun useSkill(skillChoice: SkillChoice, attacker : Creature) { + override fun useSkill(skillChoice: SkillChoice, attacker: Creature) { println("${attacker.name} used ${skillChoice.skill.name}!") } + override fun reportDamage(dmg: Int) { + if (dmg > 0) { + println("Dealt $dmg damage!") + } else if (dmg < 0) { + println("Healed ${-dmg} health!") + } + } + } \ No newline at end of file diff --git a/core/src/me/msoucy/ptures/view/ViewBase.kt b/core/src/me/msoucy/ptures/view/ViewBase.kt index 0d76373..ad4c301 100644 --- a/core/src/me/msoucy/ptures/view/ViewBase.kt +++ b/core/src/me/msoucy/ptures/view/ViewBase.kt @@ -11,7 +11,7 @@ abstract class SkillView(val skill : Skill) { abstract class CreatureView(val playerId : Int, val creature: Creature) { - abstract fun chooseSkill(possibleTargets : List) + abstract fun chooseSkill(possibleTargets : List) abstract fun displayHeader() abstract fun displayName() @@ -22,6 +22,7 @@ abstract class CreatureView(val playerId : Int, val creature: Creature) { abstract class BattleView { abstract fun useSkill(skillChoice: SkillChoice, attacker : Creature) + abstract fun reportDamage(dmg : Int) } abstract class PlayerView(val playerId : Int) {