class Player

Public Instance Methods

command(words) click to toggle source

Main command interpreter

# File lib/mr_holmes/player.rb, line 8
def command(words)
  verb, *words = words.split(" ")
  verb = "do_#{verb}"
  if respond_to?(verb)
    send(verb, *words)
    puts
  else
    puts "I don't know how to do that."
  end
end
do_cab(destination, *a) click to toggle source

Move the player around between locations

# File lib/mr_holmes/player.rb, line 33
def do_cab(destination, *a)
  location_string = destination
  a.each { |x| location_string = location_string + "_#{x.to_str.downcase}" }
  location = get_root.find(location_string.to_sym)
  travel_to = location.children.first
  get_location.move(self, travel_to)
  get_location.describe
  get_scene.describe
end
do_close(*thing) click to toggle source

Close an item in the current scene

# File lib/mr_holmes/player.rb, line 112
def do_close(*thing)
  open_close(thing, false)
end
do_drop(*thing) click to toggle source

Move an item from your inventory to the current scene

# File lib/mr_holmes/player.rb, line 91
def do_drop(*thing)
  move(thing.join(" "), get_scene)
end
do_examine(*thing) click to toggle source

Examine an item closely

# File lib/mr_holmes/player.rb, line 150
def do_examine(*thing)
  item = get_scene.find(thing)
  return if item.nil?
  item.described = false
  item.describe
end
do_get(*thing)

—————————————————————– Alias methods

Alias for: do_take
do_go(direction, *a) click to toggle source

Move the player around between scenes

# File lib/mr_holmes/player.rb, line 44
def do_go(direction, *a)
  dest = get_scene.send("exit_#{direction}")
  if dest.nil?
    puts "You can't go that way."
  else
    dest = get_location.find(dest)
    if dest.script("enter", direction)
      get_location.move(self, dest)
    end
    get_location.describe
    get_scene.describe
  end
end
do_i(*a)
Alias for: do_inventory
do_inv(*a)
Alias for: do_inventory
do_inventory(*a) click to toggle source

Display the items currently in the player’s inventory

# File lib/mr_holmes/player.rb, line 65
def do_inventory(*a)
  if children.empty?
    puts
    puts "INVENTORY"
    puts
    puts "You have nothing in your inventory..."
  else
    puts
    puts "INVENTORY"
    puts
    children.each do |c|
      puts " - #{c.name}"
    end
  end
end
Also aliased as: do_i, do_inv
do_look(*a) click to toggle source

Display current scene information

# File lib/mr_holmes/player.rb, line 59
def do_look(*a)
  puts
  puts get_scene.long_description
end
do_map(*a) click to toggle source

Display the complete node structure

# File lib/mr_holmes/player.rb, line 170
def do_map(*a)
  STDOUT.puts get_root
end
do_map_detailed(*a) click to toggle source

Display the complete node structure in detail

# File lib/mr_holmes/player.rb, line 175
def do_map_detailed(*a)
  STDOUT.puts get_root.to_s(true)
end
do_open(*thing) click to toggle source

Open an item in the current scene

# File lib/mr_holmes/player.rb, line 107
def do_open(*thing)
  open_close(thing, true)
end
do_put(*words) click to toggle source

Put an item in or on another item

# File lib/mr_holmes/player.rb, line 117
def do_put(*words)
  prepositions = [' in ', ' on ']
  prep_regex = Regexp.new("(#{prepositions.join('|')})")
  item_words, _, cont_words = words.join(' ').split(prep_regex)
  if cont_words.nil?
    puts "You want to put what where?"
    return
  end
  item = get_scene.find(item_words)
  container = get_scene.find(cont_words)
  return if item.nil? || container.nil?
  if container.script("accept", item)
    get_scene.move(item, container)
  end
end
do_root(*a) click to toggle source

—————————————————————– Debugging

# File lib/mr_holmes/player.rb, line 165
def do_root(*a)
  STDOUT.puts get_root
end
do_take(*thing) click to toggle source

Move an item from the current scene to your inventory

# File lib/mr_holmes/player.rb, line 82
def do_take(*thing)
  thing = get_scene.find(thing)
  return if thing.nil?
  if thing.script("take")
    puts "Taken. " if get_location.move(thing, self)
  end
end
Also aliased as: do_get
do_use(*words) click to toggle source

Use an item on another item

# File lib/mr_holmes/player.rb, line 134
def do_use(*words)
  prepositions = %w{ in on with }
  prepositions.map!{|p| " #{p} " }
  prep_regex = Regexp.new("(#{prepositions.join('|')})")
  item1_words, _, item2_words = words.join(' ').split(prep_regex)
  if item2_words.nil?
    puts "I don't quite understand you."
    return
  end
  item1 = get_scene.find(item1_words)
  item2 = get_scene.find(item2_words)
  return if item1.nil? || item2.nil?
  item1.script('use', item2)
end
open_close(thing, state) click to toggle source

Set the value of an item’s “open” attribute

# File lib/mr_holmes/player.rb, line 96
def open_close(thing, state)
  container = get_scene.find(thing)
  return if container.nil?
  if container.open == state
    puts "It's already #{state ? 'open' : 'closed'}"
  else
    container.open = state
  end
end