Class: Goby::Use

Inherits:
BattleCommand show all
Defined in:
lib/goby/battle/use.rb

Overview

Allows an Entity to use an Item in battle.

Constant Summary

Constants inherited from BattleCommand

BattleCommand::NO_ACTION

Instance Attribute Summary

Attributes inherited from BattleCommand

#name

Instance Method Summary collapse

Methods inherited from BattleCommand

#==, #to_s

Constructor Details

#initializeUse

Initializes the Use command.



9
10
11
# File 'lib/goby/battle/use.rb', line 9

def initialize
  super(name: "Use")
end

Instance Method Details

#fails?(user) ⇒ Boolean

Returns true iff the user’s inventory is empty.

Parameters:

  • user (Entity)

    the one who is using the command.

Returns:

  • (Boolean)

    status of the user’s inventory.



17
18
19
20
21
22
23
# File 'lib/goby/battle/use.rb', line 17

def fails?(user)
  empty = user.inventory.empty?
  if empty
    print "#{user.name}'s inventory is empty!\n\n"
  end
  return empty
end

#run(user, enemy) ⇒ Object

Uses the specified Item on the specified Entity. Note that enemy is not necessarily on whom the Item is used.

Parameters:

  • user (Entity)

    the one who is using the command.

  • enemy (Entity)

    the one on whom the command is used.



30
31
32
33
34
35
# File 'lib/goby/battle/use.rb', line 30

def run(user, enemy)
  # Determine the item and on whom to use the item.
  pair = user.choose_item_and_on_whom(enemy)
  return if (!pair)
  user.use_item(pair.first, pair.second)
end