Class: SRSGame::Location

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

Overview

:doc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, &block) ⇒ Location

params:

  • :name

    Title of the room (default: "a room")

  • :description

    Displayed when the name is regarded.

  • :items

    Items available in the room. Stored in @items.

  • :on_enter

    Block called every time room is entered.

&block

called on initialization



205
206
207
208
209
210
211
212
213
# File 'lib/srs_game.rb', line 205

def initialize(params = {}, &block)
  @name = params[:name] || "a room"
  @description = params[:description].to_s
  @items = params[:items].to_a or []
  @block = block # @block is called on initialization
  @on_enter, @on_leave = nil

  @block.call(self) if block
end

Instance Attribute Details

#blockObject

def self.directions



195
196
197
# File 'lib/srs_game.rb', line 195

def block
  @block
end

#descriptionObject

def self.directions



195
196
197
# File 'lib/srs_game.rb', line 195

def description
  @description
end

#itemsObject

def self.directions



195
196
197
# File 'lib/srs_game.rb', line 195

def items
  @items
end

#nameObject

def self.directions



195
196
197
# File 'lib/srs_game.rb', line 195

def name
  @name
end

Class Method Details

.direction_relationshipsObject



182
183
184
# File 'lib/srs_game.rb', line 182

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

.directionsObject

All directions available



191
192
193
# File 'lib/srs_game.rb', line 191

def self.directions
  direction_relationships.flatten
end

.mirrored_directionsObject

def self.direction_relationships



186
187
188
# File 'lib/srs_game.rb', line 186

def self.mirrored_directions
  direction_relationships + direction_relationships.map { |a| a.reverse }
end

Instance Method Details

#enterObject

Call @on_enter



243
244
245
246
# File 'lib/srs_game.rb', line 243

def enter
  puts info
  @on_enter.call(self) if @on_enter
end

#exitsObject

Available directions (where __send__(dir) is truthy)



228
229
230
# File 'lib/srs_game.rb', line 228

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

#infoObject

Information displayed when a room is entered.



254
255
256
257
258
259
260
# File 'lib/srs_game.rb', line 254

def info
  o = "You find yourself #{@name}. "
  o << "#{@description}. " if @description.unblank?
  o << "\nItems here are #{@items.map(&:to_s).to_sentence(:bold => true)}." if @items.unblank?
  o << "\nExits are #{exits.to_sentence(:bold => true)}." if exits.unblank?
  o
end

#leaveObject

Call @on_leave



249
250
251
# File 'lib/srs_game.rb', line 249

def leave
  @on_leave.call(self) if @on_leave
end

#on_enter(&b) ⇒ Object

Set @on_enter to &b



233
234
235
# File 'lib/srs_game.rb', line 233

def on_enter(&b)
  @on_enter = b
end

#on_leave(&b) ⇒ Object

Set @on_leave to &b



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

def on_leave(&b)
  @on_leave = b
end

#to_sObject

def info



262
263
264
# File 'lib/srs_game.rb', line 262

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