Pre and post conditions

This commit is contained in:
Matt Soucy 2019-11-24 16:22:07 -05:00
parent a51a6485ef
commit 80a5203c4b
2 changed files with 34 additions and 6 deletions

View File

@ -58,7 +58,7 @@ class Creature {
fun hits(creature : Creature, damage : Damage) =
random(100) < damage.accuracy + spd - creature.spd
inline fun <reified S : Status> hasStatus() = statuses.filter { it is S }.isNotEmpty()
inline fun <reified S : Status> hasStatus() = statuses.filterIsInstance<S>().isNotEmpty()
private fun statFormula(gene : Int, growth : Int) = (2 * gene + growth / 4) * level / 100 + 5
}

View File

@ -11,13 +11,22 @@ class Engine(private vararg val creatures : Pair<Creature, Int>) {
return creatures[currentCreature].first
}
val activeCreatures get() = creatures.withIndex()
.filter { (_, c) -> !c.first.hasStatus<KnockedOut>() }
.sortedBy { (_, c) -> c.first.spd }
.map { (i, _) -> i}
fun resolveTurn() {
val activeCreatures = creatures.withIndex()
.filter { (_, c) -> !c.first.hasStatus<KnockedOut>() }
.sortedBy { (_, c) -> c.first.spd }
// All preconditions
for (i in activeCreatures) {
val creature = creatures[i]
for (status in creature.first.statuses) {
status.onTurnStart(this)
}
}
// Get the moves each creature will use this turn
val moves = activeCreatures.map { (_, _) -> Pair(Ignore, 0) }
for ((i, c) in activeCreatures) {
val moves = activeCreatures.map { i -> Pair(Ignore, nextOpponent(i)) }
for (i in activeCreatures) {
// Resolve move
val (skill, target) = moves[i]
currentCreature = i
@ -37,6 +46,13 @@ class Engine(private vararg val creatures : Pair<Creature, Int>) {
}
}
}
// All post conditions
for (i in activeCreatures) {
val creature = creatures[i]
for (status in creature.first.statuses) {
status.onTurnEnd(this)
}
}
}
private fun getTargetList(target : Target, selected : Int) : List<Int> {
@ -48,4 +64,16 @@ class Engine(private vararg val creatures : Pair<Creature, Int>) {
Target.All -> creatures.indices.toList()
}
}
private fun nextOpponent(idx : Int) : Int {
var nextIdx = idx + 1
while (nextIdx != idx) {
if (creatures[nextIdx].second != creatures[idx].second) {
return nextIdx
}
nextIdx = (nextIdx + 1) % creatures.size
}
// There are no opponents... so use the "none" index
return -1
}
}