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 (other Adventure::Rooms), keyed by direction's Symbol.

  • contents (Inventory)

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

See Also:



25
26
27
28
29
30
31
32
33
34
# File 'lib/adventure/room.rb', line 25

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
  # TODO: implement battles and/or monsters.
end

Instance Attribute Details

#exitsObject (readonly)

Room's exit.



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

def exits
  @exits
end

#inventoryObject (readonly)

Room's Inventory.



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

def inventory
  @inventory
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.



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/adventure/room.rb', line 85

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.



62
63
64
# File 'lib/adventure/room.rb', line 62

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

#downRoom?

Shorthand for #exit with :down as direction.

Returns:

  • (Room, nil)

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



174
175
176
# File 'lib/adventure/room.rb', line 174

def down
  exit(:down)
end

#eastRoom?

Shorthand for #exit with :east as direction.

Returns:

  • (Room, nil)

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



125
126
127
# File 'lib/adventure/room.rb', line 125

def east
  exit(:east)
end

#exit(direction) ⇒ Room?

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:

  • (Room, nil)

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



103
104
105
106
# File 'lib/adventure/room.rb', line 103

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.



48
49
50
# File 'lib/adventure/room.rb', line 48

def name
  to_s
end

#northRoom?

Shorthand for #exit with :north as direction.

Returns:

  • (Room, nil)

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



111
112
113
# File 'lib/adventure/room.rb', line 111

def north
  exit(:north)
end

#northeastRoom?

Shorthand for #exit with :northeast as direction.

Returns:

  • (Room, nil)

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



118
119
120
# File 'lib/adventure/room.rb', line 118

def northeast
  exit(:northeast)
end

#northwestRoom?

Shorthand for #exit with :northwest as direction.

Returns:

  • (Room, nil)

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



160
161
162
# File 'lib/adventure/room.rb', line 160

def northwest
  exit(:northwest)
end

#southRoom?

Shorthand for #exit with :south as direction.

Returns:

  • (Room, nil)

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



139
140
141
# File 'lib/adventure/room.rb', line 139

def south
  exit(:south)
end

#southeastRoom?

Shorthand for #exit with :southeast as direction.

Returns:

  • (Room, nil)

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



132
133
134
# File 'lib/adventure/room.rb', line 132

def southeast
  exit(:southeast)
end

#southwestRoom?

Shorthand for #exit with :southwest as direction.

Returns:

  • (Room, nil)

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



146
147
148
# File 'lib/adventure/room.rb', line 146

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.



39
40
41
# File 'lib/adventure/room.rb', line 39

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

#upRoom?

Shorthand for #exit with :up as direction.

Returns:

  • (Room, nil)

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



167
168
169
# File 'lib/adventure/room.rb', line 167

def up
  exit(:up)
end

#westRoom?

Shorthand for #exit with :west as direction.

Returns:

  • (Room, nil)

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



153
154
155
# File 'lib/adventure/room.rb', line 153

def west
  exit(:west)
end