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

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

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

- (Object) block

def self.directions



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

def block
  @block
end

- (Object) description

def self.directions



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

def description
  @description
end

- (Object) items

def self.directions



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

def items
  @items
end

- (Object) name

def self.directions



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

def name
  @name
end

Class Method Details

+ (Object) direction_relationships



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

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

+ (Object) directions

All directions available



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

def self.directions
  direction_relationships.flatten
end

+ (Object) mirrored_directions

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

- (Object) enter

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

- (Object) exits

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

- (Object) info

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

- (Object) leave

Call @on_leave



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

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

- (Object) on_enter(&b)

Set @on_enter to &b



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

def on_enter(&b)
  @on_enter = b
end

- (Object) on_leave(&b)

Set @on_leave to &b



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

def on_leave(&b)
  @on_leave = b
end

- (Object) to_s

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