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

- (Location) initialize(params = {}, &block)



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

- (Object) description

Returns the value of attribute description



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

def description
  @description
end

- (Object) items

Returns the value of attribute items



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

def items
  @items
end

- (Object) name

Returns the value of attribute name



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

def name
  @name
end

Class Method Details

+ (Object) direction_relationships



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

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

+ (Object) directions

All directions available



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

def self.directions
  direction_relationships.flatten
end

+ (Object) mirrored_directions



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

- (Array) exits

Available exits



239
240
241
# File 'lib/srs_game.rb', line 239

def exits
  L.directions.select { |dir| __send__(dir) }
end

- (Object) go(direction)



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

- (Object) info

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

- (Object) item_grep(str)



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

- (Object) items_here



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

- (Object) to_s



268
269
270
# File 'lib/srs_game.rb', line 268

def to_s
  "#<SRSGame::Location #{@name.inspect} @items=#{@items.inspect} exits=#{exits.inspect}>"
end

- (Object) use_item(s, &block)



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