Class: Adventure::Room

Inherits:
Object
  • Object
show all
Defined in:
lib/adventure/room.rb

Overview

Gem's name.

Constant Summary collapse

DIRECTIONS =

List of valid directions.

%i[north northeast east southeast south southwest west northwest up down].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, **opts) ⇒ Room

Create a new instance of Room.

Parameters:

  • name (String)

    The room's name.

  • description (String)

    The room's long description.

  • opts (Hash)

    A list of optional parameters.

Options Hash (**opts):

  • lit (Boolean)

    Whether or not this room is alight, true by default.

  • exits (Hash)

    A Hash of exits (the index to other Adventure::Rooms), keyed by direction's Symbol.

  • contents (Inventory)

    The room's Item contents, empty Inventory by default.

  • challenges (Array<{Challenge}>)

    The room's challenges, if any.

  • npcs (Array<{NPC}>)

    The room's NPCs, if any.

See Also:



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/adventure/room.rb', line 35

def initialize(name, description, **opts)
  # Obligatory parameters
  @name        = name
  @description = description
  # Other, optional, parameters
  @lit         = opts.key?(:lit) ? (opts[:lit] == true) : true
  @exits       = opts.key?(:exits) ? opts[:exits].to_h : {}
  @inventory   = opts.key?(:inventory) ? opts[:inventory] : Inventory.new
  @challenges  = opts.key?(:challenges) ? opts[:challenges] : []
  @npcs        = opts.key?(:npcs) ? opts[:npcs] : []
  # TODO: implement battles and/or monsters.
end

Instance Attribute Details

#challengesObject (readonly)

Returns the value of attribute challenges.



19
20
21
# File 'lib/adventure/room.rb', line 19

def challenges
  @challenges
end

#exitsObject (readonly)

Returns the value of attribute exits.



13
14
15
# File 'lib/adventure/room.rb', line 13

def exits
  @exits
end

#inventoryObject (readonly)

Returns the value of attribute inventory.



16
17
18
# File 'lib/adventure/room.rb', line 16

def inventory
  @inventory
end

#npcsObject (readonly)

Returns the value of attribute npcs.



22
23
24
# File 'lib/adventure/room.rb', line 22

def npcs
  @npcs
end

Instance Method Details

#contents(**opts) ⇒ Object

Returns a description of the contents of this Adventure::Room, or a filler text if it is dark/unlit.

The default text is:

:name contains :items.

Where :name is replaced by this room's name, and :items by a list of its contents.

The default text for a dark room is:

You can see no contents.

Parameters:

  • opts (Hash)

    A list of optional parameters.

Options Hash (**opts):

  • :base_text (String)

    The text to describe the contents of a lit room, where you can use :name and :items as placeholders as described above.

  • :dark_text (String)

    The text (not) to describe the contents of a unlit room, this text accepts no placeholders.

  • :separator (String)

    The separator to join the list of this room's items by. This text has no whitespaces trimmed as to not remove necessary spaces, newlines, and the like --- so beware of extra, unintended spaces!

  • :no_items (String)

    A text to use in place of :items if the room has no Items within.



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/adventure/room.rb', line 97

def contents(**opts)
  # Fetch optional parameters
  base_text = opts.key?(:base_text) ? opts[:base_text].trim : ':name contains :items.'.dup
  dark_text = opts.key?(:dark_text) ? opts[:dark_text].trim : 'You can see no contents.'
  separator = opts.key?(:separator) ? opts[:separator] : ', '
  no_items  = opts.key?(:no_items) ? opts[:no_items].trim : 'no items'
  # Process texts into a room's contents description
  items = @inventory.to_i.positive? ? @inventory.join(separator) : no_items
  base_text.gsub!(':name', @name).gsub!(':items', items)

  # Return description accordingly
  @lit ? base_text : dark_text
end

#description(dark_text = 'You see nothing in the darkness.') ⇒ String

Returns this room's description, or a non-description of seeing nothing, if unlit.

The non-decription for a dark room can be customized, but defaults to:

You see nothing in the darkness.

Parameters:

  • dark_text (String) (defaults to: 'You see nothing in the darkness.')

    The placeholder non-description if this room is unlit.

Returns:

  • (String)

    Either this room's description, or a non-description if unlit.



74
75
76
# File 'lib/adventure/room.rb', line 74

def description(dark_text = 'You see nothing in the darkness.')
  @lit ? @description : dark_text
end

#downInteger, ...

Shorthand for #exit with :down as direction.

Returns:

  • (Integer, Symbol, nil)

    Either the down exit, or nil if no exit to that direction.



186
187
188
# File 'lib/adventure/room.rb', line 186

def down
  exit(:down)
end

#eastInteger, ...

Shorthand for #exit with :east as direction.

Returns:

  • (Integer, Symbol, nil)

    Either the east exit, or nil if no exit to that direction.



137
138
139
# File 'lib/adventure/room.rb', line 137

def east
  exit(:east)
end

#exit(direction) ⇒ Integer, ...

Returns the corresponding exit.

Parameters:

  • direction (Symbol, String)

    The direction to which fetch the exit. Either a one-word cardinal direction, or a corresponding Symbol.

Returns:

  • (Integer, Symbol, nil)

    Either the corresponding exit, or nil if no exit to that direction.



115
116
117
118
# File 'lib/adventure/room.rb', line 115

def exit(direction)
  direction = direction.downcase.to_sym
  @exits[direction.to_sym]
end

#nameString

Returns this room's name, or "Dark Room" if unlit.

Runs the #to_s method, under the hood.

Returns:

  • (String)

    Either the room's name, or 'Dark Room' if unlit.



60
61
62
# File 'lib/adventure/room.rb', line 60

def name
  to_s
end

#northInteger, ...

Shorthand for #exit with :north as direction.

Returns:

  • (Integer, Symbol, nil)

    Either the north exit, or nil if no exit to that direction.



123
124
125
# File 'lib/adventure/room.rb', line 123

def north
  exit(:north)
end

#northeastInteger, ...

Shorthand for #exit with :northeast as direction.

Returns:

  • (Integer, Symbol, nil)

    Either the northeast exit, or nil if no exit to that direction.



130
131
132
# File 'lib/adventure/room.rb', line 130

def northeast
  exit(:northeast)
end

#northwestInteger, ...

Shorthand for #exit with :northwest as direction.

Returns:

  • (Integer, Symbol, nil)

    Either the northwest exit, or nil if no exit to that direction.



172
173
174
# File 'lib/adventure/room.rb', line 172

def northwest
  exit(:northwest)
end

#southInteger, ...

Shorthand for #exit with :south as direction.

Returns:

  • (Integer, Symbol, nil)

    Either the south exit, or nil if no exit to that direction.



151
152
153
# File 'lib/adventure/room.rb', line 151

def south
  exit(:south)
end

#southeastInteger, ...

Shorthand for #exit with :southeast as direction.

Returns:

  • (Integer, Symbol, nil)

    Either the southeast exit, or nil if no exit to that direction.



144
145
146
# File 'lib/adventure/room.rb', line 144

def southeast
  exit(:southeast)
end

#southwestInteger, ...

Shorthand for #exit with :southwest as direction.

Returns:

  • (Integer, Symbol, nil)

    Either the southwest exit, or nil if no exit to that direction.



158
159
160
# File 'lib/adventure/room.rb', line 158

def southwest
  exit(:southwest)
end

#to_sString

Returns this room's name, or "Dark Room" if unlit.

Returns:

  • (String)

    Either the room's name, or 'Dark Room' if unlit.



51
52
53
# File 'lib/adventure/room.rb', line 51

def to_s
  @lit ? @name : 'Dark Room'
end

#upInteger, ...

Shorthand for #exit with :up as direction.

Returns:

  • (Integer, Symbol, nil)

    Either the up exit, or nil if no exit to that direction.



179
180
181
# File 'lib/adventure/room.rb', line 179

def up
  exit(:up)
end

#westInteger, ...

Shorthand for #exit with :west as direction.

Returns:

  • (Integer, Symbol, nil)

    Either the west exit, or nil if no exit to that direction.



165
166
167
# File 'lib/adventure/room.rb', line 165

def west
  exit(:west)
end