From 2e2e4a02813834353b5acf6452392c7f4616af3e Mon Sep 17 00:00:00 2001 From: Song Minjae Date: Tue, 27 Dec 2016 22:43:30 +0900 Subject: [PATCH] buffed Teleport command Former-commit-id: adc06bc2b24f9ac3ef4dc48756a8e3aa006f620f Former-commit-id: be364044a1a790da69a4a5012f8d6b832f16abd9 --- .../torvald/terrarum/console/CommandDict.kt | 4 +- src/net/torvald/terrarum/console/Teleport.kt | 112 ++++++++++++++++++ .../terrarum/console/TeleportPlayer.kt | 36 ------ 3 files changed, 114 insertions(+), 38 deletions(-) create mode 100644 src/net/torvald/terrarum/console/Teleport.kt delete mode 100644 src/net/torvald/terrarum/console/TeleportPlayer.kt diff --git a/src/net/torvald/terrarum/console/CommandDict.kt b/src/net/torvald/terrarum/console/CommandDict.kt index bf442747b..7ff92c7a8 100644 --- a/src/net/torvald/terrarum/console/CommandDict.kt +++ b/src/net/torvald/terrarum/console/CommandDict.kt @@ -23,8 +23,8 @@ object CommandDict { Pair("nc", ToggleNoClip), Pair("setlocale", SetLocale), Pair("zoom", Zoom), - Pair("teleport", TeleportPlayer), - Pair("tp", TeleportPlayer), + Pair("teleport", Teleport), + Pair("tp", Teleport), Pair("cat", CatStdout), Pair("exportav", ExportAV), Pair("setgl", SetGlobalLightOverride), diff --git a/src/net/torvald/terrarum/console/Teleport.kt b/src/net/torvald/terrarum/console/Teleport.kt new file mode 100644 index 000000000..cba5e5e60 --- /dev/null +++ b/src/net/torvald/terrarum/console/Teleport.kt @@ -0,0 +1,112 @@ +package net.torvald.terrarum.console + +import net.torvald.terrarum.StateInGame +import net.torvald.terrarum.mapdrawer.MapDrawer +import net.torvald.terrarum.Terrarum +import net.torvald.terrarum.gameactors.ActorWithBody + +/** + * Created by minjaesong on 16-01-24. + */ +internal object Teleport : ConsoleCommand { + + override fun execute(args: Array) { + if (args.size == 3) { + + val x: Int + val y: Int + try { + x = args[1].toInt() * MapDrawer.TILE_SIZE + MapDrawer.TILE_SIZE / 2 + y = args[2].toInt() * MapDrawer.TILE_SIZE + MapDrawer.TILE_SIZE / 2 + } + catch (e: NumberFormatException) { + EchoError("Teleport: wrong number input.") + return + } + + Terrarum.ingame.player.setPosition(x.toDouble(), y.toDouble()) + } + else if (args.size == 4) { + if (args[2].toLowerCase() != "to") { + EchoError("missing 'to' on teleport command") + return + } + val fromActor: ActorWithBody + val targetActor: ActorWithBody + try { + val fromActorID = args[1].toInt() + val targetActorID = if (args[3].toLowerCase() == "player") + Terrarum.ingame.player.referenceID + else + args[3].toInt() + + // if from == target, ignore the action + if (fromActorID == targetActorID) return + + if (Terrarum.ingame.getActorByID(fromActorID) !is ActorWithBody || + Terrarum.ingame.getActorByID(targetActorID) !is ActorWithBody) { + throw IllegalArgumentException() + } + else { + fromActor = Terrarum.ingame.getActorByID(fromActorID) as ActorWithBody + targetActor = Terrarum.ingame.getActorByID(targetActorID) as ActorWithBody + } + } + catch (e: NumberFormatException) { + EchoError("Teleport: illegal number input") + return + } + catch (e1: IllegalArgumentException) { + EchoError("Teleport: operation not possible on specified actor(s)") + return + } + + fromActor.setPosition( + targetActor.feetPosition.x, + targetActor.feetPosition.y + ) + } + else if (args.size == 5) { + if (args[2].toLowerCase() != "to") { + EchoError("missing 'to' on teleport command") + return + } + + val actor: ActorWithBody + val x: Int + val y: Int + try { + x = args[3].toInt() * MapDrawer.TILE_SIZE + MapDrawer.TILE_SIZE / 2 + y = args[4].toInt() * MapDrawer.TILE_SIZE + MapDrawer.TILE_SIZE / 2 + val actorID = args[1].toInt() + + if (Terrarum.ingame.getActorByID(actorID) !is ActorWithBody) { + throw IllegalArgumentException() + } + else { + actor = Terrarum.ingame.getActorByID(actorID) as ActorWithBody + } + } + catch (e: NumberFormatException) { + EchoError("Teleport: illegal number input") + return + } + catch (e1: IllegalArgumentException) { + EchoError("Teleport: operation not possible on specified actor") + return + } + + actor.setPosition(x.toDouble(), y.toDouble()) + } + else { + printUsage() + } + } + + override fun printUsage() { + Echo("Usage: teleport x-tile y-tile") + Echo(" teleport actorid to x-tile y-tile") + Echo(" teleport actorid to actorid") + Echo(" teleport actorid to player") + } +} diff --git a/src/net/torvald/terrarum/console/TeleportPlayer.kt b/src/net/torvald/terrarum/console/TeleportPlayer.kt deleted file mode 100644 index c19d0e1af..000000000 --- a/src/net/torvald/terrarum/console/TeleportPlayer.kt +++ /dev/null @@ -1,36 +0,0 @@ -package net.torvald.terrarum.console - -import net.torvald.terrarum.StateInGame -import net.torvald.terrarum.mapdrawer.MapDrawer -import net.torvald.terrarum.Terrarum - -/** - * Created by minjaesong on 16-01-24. - */ -internal object TeleportPlayer : ConsoleCommand { - - override fun execute(args: Array) { - if (args.size != 3) { - printUsage() - } - else { - - val x: Int - val y: Int - try { - x = args[1].toInt() * MapDrawer.TILE_SIZE + MapDrawer.TILE_SIZE / 2 - y = args[2].toInt() * MapDrawer.TILE_SIZE + MapDrawer.TILE_SIZE / 2 - } - catch (e: NumberFormatException) { - Echo("Wrong number input.") - return - } - - Terrarum.ingame.player.setPosition(x.toDouble(), y.toDouble()) - } - } - - override fun printUsage() { - Echo("Usage: teleport [x-tile] [y-tile]") - } -}