module Goby::Fighter

Methods and variables for something that can battle with another Fighter.

Public Instance Methods

add_battle_command(command) click to toggle source

Adds the specified battle command to the Fighter's collection.

@param [BattleCommand] command the command being added.

# File lib/goby/entity/fighter.rb, line 43
def add_battle_command(command)
  battle_commands.push(command)

  # Maintain sorted battle commands.
  battle_commands.sort! { |x, y| x.name <=> y.name }
end
add_battle_commands(battle_commands) click to toggle source

Adds the specified battle commands to the Fighter's collection.

@param [Array] battle_commands the commands being added.

# File lib/goby/entity/fighter.rb, line 53
def add_battle_commands(battle_commands)
  battle_commands.each { |command| add_battle_command(command) }
end
battle(entity) click to toggle source

Engages in battle with the specified Entity.

@param [Entity] entity the opponent of the battle.

# File lib/goby/entity/fighter.rb, line 60
def battle(entity)
  unless entity.class.included_modules.include?(Fighter)
    raise(UnfightableException, "You can't start a battle with an Entity of type #{entity.class} as it doesn't implement the Fighter module")
  end
  system("clear") unless ENV['TEST']

  battle = Battle.new(self, entity)
  winner = battle.determine_winner

  if winner.equal?(self)
    handle_victory(entity)
    entity.die
  elsif winner.equal?(entity)
    entity.handle_victory(self)
    die
  end
end
battle_commands() click to toggle source

Returns the Array for BattleCommands available for the Fighter. Sets @battle_commands to an empty Array if it's the first time it's called.

@return [Array] array of the available BattleCommands for the Fighter.

# File lib/goby/entity/fighter.rb, line 82
def battle_commands
  @battle_commands ||= []
  @battle_commands
end
choose_attack() click to toggle source

Determines how the Fighter should select an attack in battle. Override this method for control over this functionality.

@return [BattleCommand] the chosen battle command.

# File lib/goby/entity/fighter.rb, line 91
def choose_attack
  battle_commands[Random.rand(@battle_commands.length)]
end
choose_item_and_on_whom(enemy) click to toggle source

Determines how the Fighter should select the item and on whom during battle (Use command). Return nil on error.

@param [Fighter] enemy the opponent in battle. @return [C(Item, Fighter)] the item and on whom it is to be used.

# File lib/goby/entity/fighter.rb, line 100
def choose_item_and_on_whom(enemy)
  item = @inventory[Random.rand(@inventory.length)].first
  whom = [self, enemy].sample
  return C[item, whom]
end
die() click to toggle source

The function that handles how an Fighter behaves after losing a battle. Subclasses must override this function.

# File lib/goby/entity/fighter.rb, line 14
def die
  raise(NotImplementedError, 'A Fighter must know how to die.')
end
handle_victory(fighter) click to toggle source

Handles how a Fighter behaves after winning a battle. Subclasses must override this function.

@param [Fighter] fighter the Fighter who lost the battle.

# File lib/goby/entity/fighter.rb, line 22
def handle_victory(fighter)
  raise(NotImplementedError, 'A Fighter must know how to handle victory.')
end
has_battle_command(cmd) click to toggle source

Returns the index of the specified command, if it exists.

@param [BattleCommand, String] cmd the battle command (or its name). @return [Integer] the index of an existing command. Otherwise nil.

# File lib/goby/entity/fighter.rb, line 110
def has_battle_command(cmd)
  battle_commands.each_with_index do |command, index|
    return index if command.name.casecmp(cmd.to_s).zero?
  end
  return
end
print_battle_commands(header = "Battle Commands:") click to toggle source

Prints the available battle commands.

@param [String] header the text to output as a heading for the list of battle commands.

print_status() click to toggle source

Appends battle commands to the end of the Fighter print_status output.

Calls superclass method
remove_battle_command(command) click to toggle source

Removes the battle command, if it exists, from the Fighter's collection.

@param [BattleCommand, String] command the command being removed.

# File lib/goby/entity/fighter.rb, line 120
def remove_battle_command(command)
  index = has_battle_command(command)
  battle_commands.delete_at(index) if index
end
sample_agilities(fighter) click to toggle source

Uses the agility levels of the two Fighters to determine who should go first.

@param [Fighter] fighter the opponent with whom the calling Fighter is competing. @return [Boolean] true when calling Fighter should go first. Otherwise, false.

# File lib/goby/entity/fighter.rb, line 146
def sample_agilities(fighter)
  sum = fighter.stats[:agility] + stats[:agility]
  Random.rand(sum) < stats[:agility]
end
sample_gold() click to toggle source

The function that returns the gold given by a Fighter after losing a battle.

@return the amount of gold to award the victorious Fighter

# File lib/goby/entity/fighter.rb, line 36
def sample_gold
  raise(NotImplementedError, 'A Fighter must return some gold after losing a battle.')
end
sample_treasures() click to toggle source

The function that returns the treasure given by a Fighter after losing a battle.

@return [Item] the reward for the victor of the battle (or nil - no treasure).

# File lib/goby/entity/fighter.rb, line 29
def sample_treasures
  raise(NotImplementedError, 'A Fighter must know whether it returns treasure or not after losing a battle.')
end