Inching forward. Pretty sure I can make a TextField work.

I am not even mistaken. Here’s my new code:

var myText: TextArea by singleAssign()
var myCommand: TextField by singleAssign()
var count = 1
var textContents = "Welcome to Tiny Adventure!"

class MainView: View() {
    override val root: Parent = vbox {
        minWidth = 400.0
        minHeight = 200.0
        myText = textarea(textContents) {
            isEditable = false
        }
        myCommand = textfield ("> ") {
            action { someoneTyped() }
        }
    }
}

fun someoneTyped() {
    var newLine = "\nThat was command $count"
    myText.appendText("\n" + myCommand.text)
    myText.appendText(newLine)
    myCommand.text = "> "
    myCommand.appendText("")
    count++
}

I just changed my button to a TextField, and to get access to it, defined a var for it. I think both those vars could be vals. Remind me to try that.

For fun, I put a prompt in the field of “> “. You can cursor into that or backspace over it. I’m not worried about that. When you hit enter, whatever is in the text field is appended to the text area, and then a generic message saying “That was command “ and the number of the command.

We now have a playable game, albeit a very boring one.

Ship It!

There are some improvements to be made:

  • Connect it to a game so that it can do something;
  • Decide what to do about the “> “ in the text field;

First, I’ll commit this, then I’ll try to connect to a game.

Just a bit of hammering, and I have this code:


var myText: TextArea by singleAssign()
var myCommand: TextField by singleAssign()
var count = 1
var textContents = "Welcome to Tiny Adventure!"

class MainView: View() {
    val world = world {
        room("wellhouse") {
            desc("You're at wellhouse", "You're in a charming wellhouse")
            go("n", "wellhouse")
            go("s", "clearing")
        }
        room("clearing") {
            desc("You're in a clearing", "You're in a charming clearing")
            go("n", "wellhouse")
            go("s","clearing")
        }
    }
    val game = Game(world, "wellhouse")

    override val root: Parent = vbox {
        minWidth = 400.0
        minHeight = 200.0
        myText = textarea(textContents + "\n"+game.currentRoom.longDesc) {
            isEditable = false
        }
        myCommand = textfield ("") {
            action { someoneTyped() }
        }
    }
    fun someoneTyped() {
        val cmd = myCommand.text
        game.command(cmd)
        val newLine = "\n"+game.currentRoom.longDesc
        myText.appendText("\n" + cmd)
        myText.appendText(newLine)
        myCommand.text = ""
        myCommand.appendText("")
        count++
    }
}

And the game actually runs!

game window showing moving south and north

Not very interesting, but after each command it displays the long description of wherever you are, and that can in fact either be in the wellhouse or the clearing.

Commit: initial game plays as intended.

Let’s sum up and back away slowly.

Summary

Well, I’ve caused my View to create a defined game, to send it commands, and to display the long description of the room you’re in after doing the command. I’m sure that every bit of this could be improved, and that many people will tell me what it needs.

But the game is actually being played and its results are being displayed.

This is good. I will of course need to learn some things, and to improve the capabilities of the game, and so on, but it’s easy to see that most of my future efforts could go into improving the game. We have a viable “GUI”, and an actual Kotlin program that does something somewhat credible.

Woot!