module Goby::WorldCommand

Functions that handle commands on the “world map.”

Constants

DEFAULT_COMMANDS

String literal providing default commands.

NO_ITEM_DROP_ERROR

Message for when the player tries to drop a non-existent item.

SPECIAL_COMMANDS_HEADER

String literal for the special commands header.

Public Instance Methods

describe_tile(player) click to toggle source

Describes the tile to the player after each move.

@param [Player] player the player who needs the tile description.

# File lib/goby/world_command.rb, line 61
def describe_tile(player)
  tile = player.location.map.tiles[player.location.coords.first][player.location.coords.second]
  events = tile.events

  player.print_minimap
  print "#{tile.description}\n\n"
  display_special_commands(player)
end
display_default_commands() click to toggle source

Prints the commands that are available everywhere.

# File lib/goby/world_command.rb, line 32
def display_default_commands
  print DEFAULT_COMMANDS
end
display_special_commands(player) click to toggle source

Prints the commands that are tile-specific.

@param [Player] player the player who wants to see the special commands.

# File lib/goby/world_command.rb, line 39
def display_special_commands(player)
  events = player.location.map.tiles[player.location.coords.first][player.location.coords.second].events
  if events.nonempty? && events.any? { |event| event.visible }

    print SPECIAL_COMMANDS_HEADER + (events.reduce([]) do |commands, event|
      commands << event.command if event.visible
      commands
    end.join(', ')) + "\n\n"
  end
end
help(player) click to toggle source

Prints the default and special (tile-specific) commands.

@param [Player] player the player who needs help.

# File lib/goby/world_command.rb, line 53
def help(player)
  display_default_commands
  display_special_commands(player)
end
interpret_command(command, player) click to toggle source

Handles the player's input and executes the appropriate action.

@param [String] command the player's entire command input. @param [Player] player the player using the command.

# File lib/goby/world_command.rb, line 74
def interpret_command(command, player)
  return if command.eql?("quit")

  words = command.split()

  # Default commands that take multiple "arguments" (words).
  if (words.size > 1)

    # TODO: this chunk could be a private function.
    # Determine the name of the second "argument."
    name = words[1]
    for i in 2..(words.size - 1) do
      name << " " << words[i]
    end

    # Determine the appropriate command to use.
    # TODO: some of those help messages should be string literals.
    if words[0].casecmp("drop").zero?
      index = player.has_item(name)
      if index && !player.inventory[index].first.disposable
        print "You cannot drop that item.\n\n"
      elsif index
        # TODO: Perhaps the player should be allowed to specify
        #       how many of the Item to drop.
        item = player.inventory[index].first
        player.remove_item(item, 1)
        print "You have dropped #{item}.\n\n"
      else
        print NO_ITEM_DROP_ERROR
      end
      return
    elsif words[0].casecmp("equip").zero?
      player.equip_item(name); return
    elsif words[0].casecmp("unequip").zero?
      player.unequip_item(name); return
    elsif words[0].casecmp("use").zero?
      player.use_item(name, player); return
    end
  end

  # TODO: map command input to functions? Maybe this can
  #       also be done with the multiple-word commands?
  if command.casecmp("w").zero?
    player.move_up; return
  elsif command.casecmp("a").zero?
    player.move_left; return
  elsif command.casecmp("s").zero?
    player.move_down; return
  elsif command.casecmp("d").zero?
    player.move_right; return
  elsif command.casecmp("help").zero?
    help(player); return
  elsif command.casecmp("map").zero?
    player.print_map; return
  elsif command.casecmp("inv").zero?
    player.print_inventory; return
  elsif command.casecmp("status").zero?
    player.print_status; return
  elsif command.casecmp("save").zero?
    save_game(player, "player.yaml"); return
  end

  # Other commands.
  events = player.location.map.tiles[player.location.coords.first][player.location.coords.second].events
  events.each do |event|
    if (event.visible && words[0] && words[0].casecmp(event.command).zero?)
      event.run(player)
      return
    end
  end

  # Text for incorrect commands.
  # TODO: Add string literal for this.
  puts "That isn't an available command at this time."
  print "Type 'help' for a list of available commands.\n\n"

end