Module: Goby::WorldCommand

Included in:
Goby, Player
Defined in:
lib/goby/world_command.rb

Overview

Functions that handle commands on the “world map.”

Constant Summary collapse

DEFAULT_COMMANDS =

String literal providing default commands.

%{     Command          Purpose

      w (↑)
a (←) s (↓) d (→)       Movement

        help      Show the help menu
       map       Print the map
        inv         Check inventory
      status         Show player status
    use [item]      Use the specified item
    drop [item]        Drop the specified item
   equip [item]      Equip the specified item
  unequip [item]    Unequip the specified item
       save              Save the game
       quit               Quit the game

}
SPECIAL_COMMANDS_HEADER =

String literal for the special commands header.

"* Special commands: "
NO_ITEM_DROP_ERROR =

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

"You can't drop what you don't have!\n\n"

Instance Method Summary collapse

Instance Method Details

#describe_tile(player) ⇒ Object

Describes the tile to the player after each move.

Parameters:

  • player (Player)

    the player who needs the tile description.



61
62
63
64
65
66
67
68
# 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_commandsObject

Prints the commands that are available everywhere.



32
33
34
# File 'lib/goby/world_command.rb', line 32

def display_default_commands
  print DEFAULT_COMMANDS
end

#display_special_commands(player) ⇒ Object

Prints the commands that are tile-specific.

Parameters:

  • player (Player)

    the player who wants to see the special commands.



39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object

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

Parameters:

  • player (Player)

    the player who needs help.



53
54
55
56
# File 'lib/goby/world_command.rb', line 53

def help(player)
  display_default_commands
  display_special_commands(player)
end

#interpret_command(command, player) ⇒ Object

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

Parameters:

  • command (String)

    the player’s entire command input.

  • player (Player)

    the player using the command.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# 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