Module: Goby::Fighter
Overview
Methods and variables for something that can battle with another Fighter.
Defined Under Namespace
Classes: UnfightableException
Instance Method Summary collapse
-
#add_battle_command(command) ⇒ Object
Adds the specified battle command to the Fighter’s collection.
-
#add_battle_commands(battle_commands) ⇒ Object
Adds the specified battle commands to the Fighter’s collection.
-
#battle(entity) ⇒ Object
Engages in battle with the specified Entity.
-
#battle_commands ⇒ Array
Returns the Array for BattleCommands available for the Fighter.
-
#choose_attack ⇒ BattleCommand
Determines how the Fighter should select an attack in battle.
-
#choose_item_and_on_whom(enemy) ⇒ C(Item, Fighter)
Determines how the Fighter should select the item and on whom during battle (Use command).
-
#die ⇒ Object
The function that handles how an Fighter behaves after losing a battle.
-
#handle_victory(fighter) ⇒ Object
Handles how a Fighter behaves after winning a battle.
-
#has_battle_command(cmd) ⇒ Integer
Returns the index of the specified command, if it exists.
-
#print_battle_commands(header = "Battle Commands:") ⇒ Object
Prints the available battle commands.
-
#print_status ⇒ Object
Appends battle commands to the end of the Fighter print_status output.
-
#remove_battle_command(command) ⇒ Object
Removes the battle command, if it exists, from the Fighter’s collection.
-
#sample_agilities(fighter) ⇒ Boolean
Uses the agility levels of the two Fighters to determine who should go first.
-
#sample_gold ⇒ Object
The function that returns the gold given by a Fighter after losing a battle.
-
#sample_treasures ⇒ Item
The function that returns the treasure given by a Fighter after losing a battle.
Instance Method Details
#add_battle_command(command) ⇒ Object
Adds the specified battle command to the Fighter’s collection.
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.
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.
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_commands ⇒ Array
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.
82 83 84 85 |
# File 'lib/goby/entity/fighter.rb', line 82 def battle_commands @battle_commands ||= [] @battle_commands end |
#choose_attack ⇒ BattleCommand
Determines how the Fighter should select an attack in battle. Override this method for control over this functionality.
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.
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 |
#die ⇒ Object
The function that handles how an Fighter behaves after losing a battle. Subclasses must override this function.
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.
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.
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 |
#print_battle_commands(header = "Battle Commands:") ⇒ Object
Prints the available 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 |
#print_status ⇒ Object
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.
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.
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_gold ⇒ Object
The function that returns the gold given by a Fighter after losing a battle.
@return the amount of gold to award the victorious Fighter
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_treasures ⇒ Item
The function that returns the treasure given by a Fighter after losing a battle.
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 |