Class: SRSGame::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod = SRSGame::Basic, options = {}) ⇒ Game

Returns a new instance of Game.

Raises:

  • (ArgumentError)


339
340
341
342
343
344
345
346
347
# File 'lib/srs_game.rb', line 339

def initialize(mod = SRSGame::Basic, options = {})
  raise ArgumentError, "Can't use #{mod} for SRSGame module" unless mod.is_a? Module
  extend mod

  @options = options
  @room = main_room
  #@travel_path = [@room]
  @command = mod.const_get(:Commands).new
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



337
338
339
# File 'lib/srs_game.rb', line 337

def command
  @command
end

#roomObject

Returns the value of attribute room.



337
338
339
# File 'lib/srs_game.rb', line 337

def room
  @room
end

Instance Method Details

#go!(*directions) ⇒ Object



349
350
351
352
353
354
355
356
# File 'lib/srs_game.rb', line 349

def go!(*directions)
  if directions.size >= 2
    directions.each { |dir| go! dir }
  else
    new_room = @room.go(directions[0])
    @room = new_room
  end
end

#playObject



380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/srs_game.rb', line 380

def play
  Readline.completion_append_character = " "

  begin
    loop do
      Readline.completion_proc = proc { |match| @command.matching_methods(match).map(&:command_pp) }
      input = Readline.readline(prompt, true)
      puts send(input) unless input.blank?
    end
  rescue DONE_WITH_SRS_GAME
    puts "\nHave a nice day!"
  end
end

#promptObject

def same_room?

current_room.eql? last_room

end



370
371
372
373
# File 'lib/srs_game.rb', line 370

def prompt
  pr = "$ ".bold.blue
  @options[:color] ? pr : pr.uncolored
end

#send(input) ⇒ Object



375
376
377
378
# File 'lib/srs_game.rb', line 375

def send(input)
  output = @command.parse(input, self)
  @options[:color] ? output : output.uncolored
end