Created
June 13, 2018 01:41
-
-
Save Jire/607cc3ab613ed8ee4b3f3d2e880ec0c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ps.eden.server.game.content.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[id] = stage | |
} | |
fun stageFor(id: Int) = idToStage[id] | |
inline operator fun <reified 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) | |
} | |
inline infix fun <reified R> Int.actions(crossinline defineHandlers: ActionStage.() -> R) { | |
this { | |
ActionStage(id).run { | |
defineHandlers() | |
(onButtonHandlers[buttonID] ?: defaultHandler)?.invoke() | |
} | |
} | |
} | |
// 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