Exit Full View

Cavern Quest 2 / src / commonMain / kotlin / Soil.kt

import uk.co.nickthecoder.kyd.Actor
import uk.co.nickthecoder.kyd.Ticks
import uk.co.nickthecoder.kyd.appearance.*

/**
 * A semi-solid object that [Player] must blast through.
 * Blasts are finite, and if the counter reaches zero, no more blasting!
 * [Rock]s and [Monster]s cannot move through soil.
 */
class Soil : Item() {

    override var canBlast = true

    override fun tick(actor: Actor, seconds: Float) {
        (actor.appearance as? Ticks)?.tick(actor, seconds)
    }

    fun blast(actor: Actor) {
        canBlast = false
        val role = actor.role ?: return
        val a = actor.appearance
        val b = role.find("/graphics/empty") as WithAppearance

        actor.appearance = flipBookAnimation(actor) {
            repetitions = 3
            val pageCount = 2
            page(a, soilBlastTime / repetitions / pageCount)
            page(b, soilBlastTime / repetitions / pageCount)
            onFinished {
                // Tell rock to pause before falling.
                val actorAbove = look(0, 1)
                (actorAbove?.behaviour as? Rock)?.soilRemoved()

                // Destroy me.
                actor.parent.removeActor(actor)
                PlayDirector.instance.grid[gridX][gridY] = null
            }
        }
    }

}