Skip to content

Instantly share code, notes, and snippets.

@Jire
Created August 11, 2017 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jire/93989e1066fba00a3de27619de451caf to your computer and use it in GitHub Desktop.
Save Jire/93989e1066fba00a3de27619de451caf to your computer and use it in GitHub Desktop.
Eden's Stages manager
package modules.dialogue
/**
* @author Jire
*/
class Stages(val dialogue: Dialogue) {
companion object {
const val START_STAGE = 1
const val END_STAGE = -1
}
val START_STAGE = Companion.START_STAGE // for no-import use in handles
val END_STAGE = Companion.END_STAGE // for no-import use in handles
private val idToStage = HashMap<Int, Stage>()
fun defineStage(id: Int, stage: Stage) {
idToStage.put(id, stage)
}
fun stageFor(id: Int) = idToStage[id]
operator inline fun <R> Int.invoke(crossinline handle: Stage.() -> R) {
if (this < 1) throw IllegalArgumentException("You can only define stages one through infinity!")
if (stageFor(this) != null) throw IllegalStateException("Stage $this is already defined!")
val stage = Stage(this)
stage.next = {
val result = stage.handle()
if (result is Int) result
else dialogue.stage + 1 // the new stage is our current stage + 1
}
defineStage(this, stage)
}
// MISC MACROS
operator fun String.minus(next: String) = ArrayList<String>(5).apply { add(this@minus); add(next) }
operator fun MutableList<String>.minus(next: String) = apply { add(next) }
inner class StageMacro(val strings: List<String>, val stage: Int)
operator fun MutableList<String>.minus(stage: Int) = StageMacro(this, stage)
operator fun String.minus(stage: Int) = StageMacro(listOf(this), stage)
// NPC MACROS
fun Int.npc(vararg lines: String) = invoke { dialogue.npc(*lines) }
fun Int.npc(vararg lines: String, nextStage: Int) = invoke { dialogue.npc(*lines); nextStage }
infix fun Int.npc(lines: List<String>) = npc(*lines.toTypedArray())
infix fun Int.npc(message: String) = npc(message.split("\n"))
infix fun Int.npc(macro: StageMacro) = npc(*macro.strings.toTypedArray(), nextStage = macro.stage)
// PLAYER MACROS
fun Int.player(vararg lines: String) = invoke { dialogue.player(*lines) }
fun Int.player(vararg lines: String, nextStage: Int) = invoke { dialogue.player(*lines); nextStage }
infix fun Int.player(lines: List<String>) = player(*lines.toTypedArray())
infix fun Int.player(message: String) = player(message.split("\n"))
infix fun Int.player(macro: StageMacro) = player(*macro.strings.toTypedArray(), nextStage = macro.stage)
// OPTION MACROS
fun Int.options(vararg options: String) = invoke { dialogue.options(*options) }
fun Int.options(vararg options: String, nextStage: Int) = invoke { dialogue.options(*options); nextStage }
infix fun Int.options(options: List<String>) = options(*options.toTypedArray())
infix fun Int.options(options: String) = options(options.split("\n"))
infix fun Int.options(macro: StageMacro) = options(*macro.strings.toTypedArray(), nextStage = macro.stage)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment