...

/

Dungeon Rooms and Actions

Dungeon Rooms and Actions

Build upon the game by adding rooms and actions for the characters.

Building structs that use structs

When the hero is in a room, the player can choose an action and face the consequences. We have two new structs to build, and one will reference the other. The Room struct will have many Action structs.

Let’s define the room action module in lib/dungeon_crawl/room. Then we’ll add the following module to the file action.ex:

Press + to interact
defmodule DungeonCrawl.Room.Action do
alias DungeonCrawl.Room.Action
defstruct label: nil, id: nil
def forward, do: %Action{id: :forward, label: "Move forward."}
def rest, do: %Action{id: :rest, label: "Take a better look and rest."}
def search, do: %Action{id: :search, label: "Search the room."}
end

The DungeonCrawl.Room.Action struct has id and label attributes. We also created helper functions to build common actions that we’ll need to build the rooms. The next step is to create the room module. ...