Class: Adventure::Room
- Inherits:
-
Object
- Object
- Adventure::Room
- 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
-
#exits ⇒ Object
readonly
Room's exit.
-
#inventory ⇒ Object
readonly
Room's Inventory.
Instance Method Summary collapse
-
#contents(**opts) ⇒ Object
Returns a description of the contents of this Room, or a filler text if it is dark/unlit.
-
#description(dark_text = 'You see nothing in the darkness.') ⇒ String
Returns this room's description, or a non-description of seeing nothing, if unlit.
-
#down ⇒ Room?
Shorthand for #exit with
:downas direction. -
#east ⇒ Room?
Shorthand for #exit with
:eastas direction. -
#exit(direction) ⇒ Room?
Returns the corresponding exit.
-
#initialize(name, description, **opts) ⇒ Room
constructor
Create a new instance of Room.
-
#name ⇒ String
Returns this room's name, or "Dark Room" if unlit.
-
#north ⇒ Room?
Shorthand for #exit with
:northas direction. -
#northeast ⇒ Room?
Shorthand for #exit with
:northeastas direction. -
#northwest ⇒ Room?
Shorthand for #exit with
:northwestas direction. -
#south ⇒ Room?
Shorthand for #exit with
:southas direction. -
#southeast ⇒ Room?
Shorthand for #exit with
:southeastas direction. -
#southwest ⇒ Room?
Shorthand for #exit with
:southwestas direction. -
#to_s ⇒ String
Returns this room's name, or "Dark Room" if unlit.
-
#up ⇒ Room?
Shorthand for #exit with
:upas direction. -
#west ⇒ Room?
Shorthand for #exit with
:westas direction.
Constructor Details
#initialize(name, description, **opts) ⇒ Room
Create a new instance of Room.
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
#exits ⇒ Object (readonly)
Room's exit.
12 13 14 |
# File 'lib/adventure/room.rb', line 12 def exits @exits end |
#inventory ⇒ Object (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:
:namecontains: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.
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.
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 |
#down ⇒ Room?
Shorthand for #exit with :down as direction.
174 175 176 |
# File 'lib/adventure/room.rb', line 174 def down exit(:down) end |
#east ⇒ Room?
Shorthand for #exit with :east as direction.
125 126 127 |
# File 'lib/adventure/room.rb', line 125 def east exit(:east) end |
#exit(direction) ⇒ Room?
Returns the corresponding exit.
103 104 105 106 |
# File 'lib/adventure/room.rb', line 103 def exit(direction) direction = direction.downcase.to_sym @exits[direction.to_sym] end |
#name ⇒ String
Returns this room's name, or "Dark Room" if unlit.
Runs the #to_s method, under the hood.
48 49 50 |
# File 'lib/adventure/room.rb', line 48 def name to_s end |
#north ⇒ Room?
Shorthand for #exit with :north as direction.
111 112 113 |
# File 'lib/adventure/room.rb', line 111 def north exit(:north) end |
#northeast ⇒ Room?
Shorthand for #exit with :northeast as direction.
118 119 120 |
# File 'lib/adventure/room.rb', line 118 def northeast exit(:northeast) end |
#northwest ⇒ Room?
Shorthand for #exit with :northwest as direction.
160 161 162 |
# File 'lib/adventure/room.rb', line 160 def northwest exit(:northwest) end |
#south ⇒ Room?
Shorthand for #exit with :south as direction.
139 140 141 |
# File 'lib/adventure/room.rb', line 139 def south exit(:south) end |
#southeast ⇒ Room?
Shorthand for #exit with :southeast as direction.
132 133 134 |
# File 'lib/adventure/room.rb', line 132 def southeast exit(:southeast) end |
#southwest ⇒ Room?
Shorthand for #exit with :southwest as direction.
146 147 148 |
# File 'lib/adventure/room.rb', line 146 def southwest exit(:southwest) end |
#to_s ⇒ String
Returns this 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 |