Module: Goby::Fighter

Included in:
Monster, Player
Defined in:
lib/goby/entity/fighter.rb

Overview

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

Defined Under Namespace

Classes: UnfightableException

Instance Method Summary collapse

Instance Method Details

#add_battle_command(command) ⇒ Object

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

Parameters:



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

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

Parameters:

  • battle_commands (Array)

    the commands being added.



53
54
55
# 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) ⇒ Object

Engages in battle with the specified Entity.

Parameters:

  • entity (Entity)

    the opponent of the battle.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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_commandsArray

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.

Returns:

  • (Array)

    array of the available BattleCommands for the Fighter.



82
83
84
85
# File 'lib/goby/entity/fighter.rb', line 82

def battle_commands
  @battle_commands ||= []
  @battle_commands
end

#choose_attackBattleCommand

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

Returns:



91
92
93
# 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) ⇒ C(Item, Fighter)

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

Parameters:

  • enemy (Fighter)

    the opponent in battle.

Returns:

  • (C(Item, Fighter))

    the item and on whom it is to be used.



100
101
102
103
104
# 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

#dieObject

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

Raises:

  • (NotImplementedError)


14
15
16
# File 'lib/goby/entity/fighter.rb', line 14

def die
  raise(NotImplementedError, 'A Fighter must know how to die.')
end

#handle_victory(fighter) ⇒ Object

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

Parameters:

  • fighter (Fighter)

    the Fighter who lost the battle.

Raises:

  • (NotImplementedError)


22
23
24
# 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) ⇒ Integer

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

Parameters:

Returns:

  • (Integer)

    the index of an existing command. Otherwise nil.



110
111
112
113
114
115
# 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

Prints the available battle commands.

Parameters:

  • header (String) (defaults to: "Battle Commands:")

    the text to output as a heading for the list of battle commands.



128
129
130
131
132
133
134
# File 'lib/goby/entity/fighter.rb', line 128

def print_battle_commands(header = "Battle Commands:")
  puts header
  battle_commands.each do |command|
    print "#{command.name}\n"
  end
  print "\n"
end

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



137
138
139
140
# File 'lib/goby/entity/fighter.rb', line 137

def print_status
  super
  print_battle_commands unless battle_commands.empty?
end

#remove_battle_command(command) ⇒ Object

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

Parameters:



120
121
122
123
# 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) ⇒ Boolean

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

Parameters:

  • fighter (Fighter)

    the opponent with whom the calling Fighter is competing.

Returns:

  • (Boolean)

    true when calling Fighter should go first. Otherwise, false.



146
147
148
149
# 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_goldObject

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

@return the amount of gold to award the victorious Fighter

Raises:

  • (NotImplementedError)


36
37
38
# 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_treasuresItem

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

Returns:

  • (Item)

    the reward for the victor of the battle (or nil - no treasure).

Raises:

  • (NotImplementedError)


29
30
31
# 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