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
-
#challenges ⇒ Object
readonly
Returns the value of attribute challenges.
-
#exits ⇒ Object
readonly
Returns the value of attribute exits.
-
#inventory ⇒ Object
readonly
Returns the value of attribute inventory.
-
#npcs ⇒ Object
readonly
Returns the value of attribute npcs.
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 ⇒ Integer, ...
Shorthand for #exit with
:downas direction. -
#east ⇒ Integer, ...
Shorthand for #exit with
:eastas direction. -
#exit(direction) ⇒ Integer, ...
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 ⇒ Integer, ...
Shorthand for #exit with
:northas direction. -
#northeast ⇒ Integer, ...
Shorthand for #exit with
:northeastas direction. -
#northwest ⇒ Integer, ...
Shorthand for #exit with
:northwestas direction. -
#south ⇒ Integer, ...
Shorthand for #exit with
:southas direction. -
#southeast ⇒ Integer, ...
Shorthand for #exit with
:southeastas direction. -
#southwest ⇒ Integer, ...
Shorthand for #exit with
:southwestas direction. -
#to_s ⇒ String
Returns this room's name, or "Dark Room" if unlit.
-
#up ⇒ Integer, ...
Shorthand for #exit with
:upas direction. -
#west ⇒ Integer, ...
Shorthand for #exit with
:westas direction.
Constructor Details
#initialize(name, description, **opts) ⇒ Room
Create a new instance of Room.
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
#challenges ⇒ Object (readonly)
Returns the value of attribute challenges.
19 20 21 |
# File 'lib/adventure/room.rb', line 19 def challenges @challenges end |
#exits ⇒ Object (readonly)
Returns the value of attribute exits.
13 14 15 |
# File 'lib/adventure/room.rb', line 13 def exits @exits end |
#inventory ⇒ Object (readonly)
Returns the value of attribute inventory.
16 17 18 |
# File 'lib/adventure/room.rb', line 16 def inventory @inventory end |
#npcs ⇒ Object (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:
: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.
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.
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 |
#down ⇒ Integer, ...
Shorthand for #exit with :down as direction.
186 187 188 |
# File 'lib/adventure/room.rb', line 186 def down exit(:down) end |
#east ⇒ Integer, ...
Shorthand for #exit with :east as direction.
137 138 139 |
# File 'lib/adventure/room.rb', line 137 def east exit(:east) end |
#exit(direction) ⇒ Integer, ...
Returns the corresponding exit.
115 116 117 118 |
# File 'lib/adventure/room.rb', line 115 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.
60 61 62 |
# File 'lib/adventure/room.rb', line 60 def name to_s end |
#north ⇒ Integer, ...
Shorthand for #exit with :north as direction.
123 124 125 |
# File 'lib/adventure/room.rb', line 123 def north exit(:north) end |
#northeast ⇒ Integer, ...
Shorthand for #exit with :northeast as direction.
130 131 132 |
# File 'lib/adventure/room.rb', line 130 def northeast exit(:northeast) end |
#northwest ⇒ Integer, ...
Shorthand for #exit with :northwest as direction.
172 173 174 |
# File 'lib/adventure/room.rb', line 172 def northwest exit(:northwest) end |
#south ⇒ Integer, ...
Shorthand for #exit with :south as direction.
151 152 153 |
# File 'lib/adventure/room.rb', line 151 def south exit(:south) end |
#southeast ⇒ Integer, ...
Shorthand for #exit with :southeast as direction.
144 145 146 |
# File 'lib/adventure/room.rb', line 144 def southeast exit(:southeast) end |
#southwest ⇒ Integer, ...
Shorthand for #exit with :southwest as direction.
158 159 160 |
# File 'lib/adventure/room.rb', line 158 def southwest exit(:southwest) end |
#to_s ⇒ String
Returns this 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 |
#up ⇒ Integer, ...
Shorthand for #exit with :up as direction.
179 180 181 |
# File 'lib/adventure/room.rb', line 179 def up exit(:up) end |
#west ⇒ Integer, ...
Shorthand for #exit with :west as direction.
165 166 167 |
# File 'lib/adventure/room.rb', line 165 def west exit(:west) end |