Improve status capability
This commit is contained in:
		@@ -1,11 +1,11 @@
 | 
			
		||||
package me.msoucy.ptures.model
 | 
			
		||||
 | 
			
		||||
class Engine(vararg val creatures : Pair<Creature, Int>) {
 | 
			
		||||
class Engine(private vararg val creatures : Pair<Creature, Int>) {
 | 
			
		||||
    init {
 | 
			
		||||
        assert(creatures.isNotEmpty())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    var currentCreature = 0
 | 
			
		||||
    private var currentCreature = 0
 | 
			
		||||
 | 
			
		||||
    val attacker : Creature get() {
 | 
			
		||||
        return creatures[currentCreature].first
 | 
			
		||||
@@ -17,35 +17,39 @@ class Engine(vararg val creatures : Pair<Creature, Int>) {
 | 
			
		||||
                .sortedBy { (_, c) -> c.first.spd }
 | 
			
		||||
        // Get the moves each creature will use this turn
 | 
			
		||||
        val moves = activeCreatures.map { (_, c) -> Pair(c.first.skills[0], 0) }
 | 
			
		||||
        val wasHit = mutableListOf<Creature>()
 | 
			
		||||
        val wasHit = mutableListOf<Pair<Creature, Int>>()
 | 
			
		||||
        for ((i, c) in activeCreatures) {
 | 
			
		||||
            // Resolve move
 | 
			
		||||
            val (skill, target) = moves[i]
 | 
			
		||||
            currentCreature = i
 | 
			
		||||
            for (step in skill.damageSteps) {
 | 
			
		||||
                val targets = getTargetList(step.target, currentCreature, target)
 | 
			
		||||
                val targets = getTargetList(step.target, target)
 | 
			
		||||
                for (t in targets) {
 | 
			
		||||
                    val (targetCreature, _) = creatures[t]
 | 
			
		||||
                    if (attacker.hits(targetCreature, step)) {
 | 
			
		||||
                        targetCreature.apply(step, c.first)
 | 
			
		||||
                        wasHit.add(targetCreature)
 | 
			
		||||
                        if (wasHit.any { it.first == targetCreature }) {
 | 
			
		||||
                            wasHit.add(Pair(targetCreature, t))
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            for(creature in wasHit) {
 | 
			
		||||
            for(step in skill.statusSteps) {
 | 
			
		||||
                    step(creature)
 | 
			
		||||
                for ((targetCreature, t) in wasHit) {
 | 
			
		||||
                    if (t in getTargetList(step.target, t)) {
 | 
			
		||||
                        step.applier(targetCreature)
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun getTargetList(target : Target, active : Int, selected : Int) : List<Int> {
 | 
			
		||||
    private fun getTargetList(target : Target, selected : Int) : List<Int> {
 | 
			
		||||
        return when(target) {
 | 
			
		||||
            Target.Self -> listOf(active)
 | 
			
		||||
            Target.Self -> listOf(currentCreature)
 | 
			
		||||
            Target.Selected -> listOf(selected)
 | 
			
		||||
            Target.Others -> creatures.indices.filter { it != active }
 | 
			
		||||
            Target.Opponents -> creatures.indices.filter { creatures[it].second != creatures[active].second }
 | 
			
		||||
            Target.Others -> creatures.indices.filter { it != currentCreature }
 | 
			
		||||
            Target.Opponents -> creatures.indices.filter { creatures[it].second != creatures[currentCreature].second }
 | 
			
		||||
            Target.All -> creatures.indices.toList()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -23,16 +23,14 @@ enum class Attribute {
 | 
			
		||||
class Damage(val power : Int) {
 | 
			
		||||
    var target = Target.Selected
 | 
			
		||||
    var accuracy = 100
 | 
			
		||||
 | 
			
		||||
    infix fun hits (target : Creature) {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class StatusApplier(val target : Target, val applier : (Creature) -> Unit)
 | 
			
		||||
 | 
			
		||||
@SkillMarker
 | 
			
		||||
class Skill(val name : String, val attribute : Attribute) {
 | 
			
		||||
    val damageSteps = mutableListOf<Damage>()
 | 
			
		||||
    val statusSteps = mutableListOf<(Creature) -> Unit>()
 | 
			
		||||
    val statusSteps = mutableListOf<StatusApplier>()
 | 
			
		||||
 | 
			
		||||
    fun damage(power : Int, block : Damage.() -> Unit = {}) {
 | 
			
		||||
        val d = Damage(power)
 | 
			
		||||
@@ -41,11 +39,11 @@ class Skill(val name : String, val attribute : Attribute) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun addStatus(status : Status, target : Target = Target.Selected) {
 | 
			
		||||
        statusSteps.add { c -> c.addStatus(status) }
 | 
			
		||||
        statusSteps.add (StatusApplier(target) { c -> c.addStatus(status) })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun removeStatus(status : Status, target : Target = Target.Selected) {
 | 
			
		||||
        statusSteps.add { c -> c.removeStatus(status) }
 | 
			
		||||
        statusSteps.add (StatusApplier(target) { c -> c.removeStatus(status) })
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user