Class: SRSGame::Location

Inherits:
Object show all
Defined in:
lib/srs_game.rb,
lib/srs_game.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, &block) ⇒ 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

#descriptionObject

Returns the value of attribute description



207
208
209
# File 'lib/srs_game.rb', line 207

def description
  @description
end

#itemsObject

Returns the value of attribute items



207
208
209
# File 'lib/srs_game.rb', line 207

def items
  @items
end

#nameObject

Returns the value of attribute name



207
208
209
# File 'lib/srs_game.rb', line 207

def name
  @name
end

Class Method Details

.direction_relationshipsObject



193
194
195
# File 'lib/srs_game.rb', line 193

def self.direction_relationships
  [["north", "south"], ["east", "west"], ["up", "down"], ["in", "out"]]
end

.directionsObject

All directions available



202
203
204
# File 'lib/srs_game.rb', line 202

def self.directions
  direction_relationships.flatten
end

.mirrored_directionsObject



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

#exitsArray

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

#infoObject

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_hereObject



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_sObject



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