Class: SRSGame::Location
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#items ⇒ Object
Returns the value of attribute items.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
- .direction_relationships ⇒ Object
-
.directions ⇒ Object
All directions available.
- .mirrored_directions ⇒ Object
Instance Method Summary collapse
-
#exits ⇒ Array
Available exits.
- #go(direction) ⇒ Object
-
#info ⇒ Object
Information displayed when a room is entered.
-
#initialize(params = {}, &block) ⇒ Location
constructor
A new instance of Location.
- #item_grep(str) ⇒ Object
- #items_here ⇒ Object
- #to_s ⇒ Object
- #use_item(s, &block) ⇒ Object
Constructor Details
#initialize(params = {}, &block) ⇒ Location
Returns a new instance of Location.
209 210 211 212 213 214 215 |
# File 'lib/srs_game.rb', line 209 def initialize(params = {}, &block) @name = params[:name] || "a room" @description = params[:description].to_s @items = params[:items].to_a block.call(self) if block.respond_to? :call end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
207 208 209 |
# File 'lib/srs_game.rb', line 207 def description @description end |
#items ⇒ Object
Returns the value of attribute items.
207 208 209 |
# File 'lib/srs_game.rb', line 207 def items @items end |
#name ⇒ Object
Returns the value of attribute name.
207 208 209 |
# File 'lib/srs_game.rb', line 207 def name @name end |
Class Method Details
.direction_relationships ⇒ Object
193 194 195 |
# File 'lib/srs_game.rb', line 193 def self.direction_relationships [["north", "south"], ["east", "west"], ["up", "down"], ["in", "out"]] end |
.directions ⇒ Object
All directions available
202 203 204 |
# File 'lib/srs_game.rb', line 202 def self.directions direction_relationships.flatten end |
.mirrored_directions ⇒ Object
197 198 199 |
# File 'lib/srs_game.rb', line 197 def self.mirrored_directions direction_relationships + direction_relationships.map { |a| a.reverse } end |
Instance Method Details
#exits ⇒ Array
Available exits
239 240 241 |
# File 'lib/srs_game.rb', line 239 def exits L.directions.select { |dir| __send__(dir) } end |
#go(direction) ⇒ Object
252 253 254 255 256 257 258 |
# File 'lib/srs_game.rb', line 252 def go(direction) if exits.include?(direction.to_s) __send__(direction) else raise DirectionError, "Can't go #{direction} from #{@name}." end end |
#info ⇒ Object
Information displayed when a room is entered.
244 245 246 247 248 249 250 |
# File 'lib/srs_game.rb', line 244 def info o = "You find yourself #{@name.bold}." o << " #{@description}" if @description.unblank? o << "\n" << items_here if @items.unblank? o << "\nExits are #{exits.to_sentence(:bold => true)}." if exits.unblank? o end |
#item_grep(str) ⇒ Object
217 218 219 |
# File 'lib/srs_game.rb', line 217 def item_grep(str) @items.select { |item| str == item.interactable_as.to_s.downcase } end |
#items_here ⇒ Object
221 222 223 |
# File 'lib/srs_game.rb', line 221 def items_here "Items here are #{@items.map(&:to_s).to_sentence(:bold => true)}." end |
#to_s ⇒ Object
268 269 270 |
# File 'lib/srs_game.rb', line 268 def to_s "#<SRSGame::Location #{@name.inspect} @items=#{@items.inspect} exits=#{exits.inspect}>" end |
#use_item(s, &block) ⇒ Object
260 261 262 263 264 265 266 |
# File 'lib/srs_game.rb', line 260 def use_item(s, &block) if (found = item_grep(s)).length == 1 block.call found[0] else items_here end end |