Class Functions
Class definitions can specify two types of content: behavior and data. In NyetHack, a player should be able to take various actions: perform combat, move, cast a spell, or check their inventory, for example. You define behavior for a class by adding function definitions to its class body. Functions defined within a class are called class functions.
You already have some player data defined in NyetHack. You will be moving that into the Player class and introducing a few behaviors and new pieces of data.
Define the first Player behavior by adding a function to allow the player to cast a spell:
Listing 13.3 Defining a class function (Player.kt)
class Player {
fun castFireball(numFireballs: Int = 2) {
narrate("A ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access