Adding Loot to NyetHack

With the loot scaffolding in place, you are ready to scatter precious items throughout NyetHack for your player to collect and sell. Start out by giving the player pockets to store their valuables.

Listing 18.15  Adding an inventory (Player.kt)

class Player(
    initialName: String,
    val hometown: String = "Neversummer",
    override var healthPoints: Int,
    val isImmortal: Boolean
) : Fightable {
    ...
    val prophecy by lazy {
        ...
    }

    val inventory = mutableListOf<Loot>()

    var gold = 0
    ...
}

Next, add a companion object to LootBox and create a random function to randomly generate a variety of loot boxes for the player to discover.

Listing 18.16  Randomizing loot (Loot.kt)

class LootBox<out T : Loot>(val contents: ...

Get Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.