class Goby::Battle

Representation of a fight between two Fighters.

Attributes

entity_a[R]
entity_b[R]

Public Class Methods

new(entity_a, entity_b) click to toggle source

@param [Entity] entity_a the first entity in the battle @param [Entity] entity_b the second entity in the battle

# File lib/goby/battle/battle.rb, line 10
def initialize(entity_a, entity_b)
  @entity_a = entity_a
  @entity_b = entity_b
end

Public Instance Methods

determine_winner() click to toggle source

Determine the winner of the battle

@return [Entity] the winner of the battle

# File lib/goby/battle/battle.rb, line 18
def determine_winner
  type("#{entity_a.name} enters a battle with #{entity_b.name}!\n\n")
  until someone_dead?
    #Determine order of attacks
    attackers = determine_order

    # Both choose an attack.
    attacks = attackers.map { |attacker| attacker.choose_attack }

    2.times do |i|
      # The attacker runs its attack on the other attacker.
      attacks[i].run(attackers[i], attackers[(i + 1) % 2])

      if (attackers[i].escaped)
        attackers[i].escaped = false
        return
      end

      break if someone_dead?
    end
  end

  #If @entity_a is dead return @entity_b, otherwise return @entity_a
  entity_a.stats[:hp] <=0 ? entity_b : entity_a
end

Private Instance Methods

determine_order() click to toggle source

Determine the order of attack based on the entitys' agilities

@return [Array] the entities in the order of attack

# File lib/goby/battle/battle.rb, line 49
def determine_order
  sum = entity_a.stats[:agility] + entity_b.stats[:agility]
  random_number = Random.rand(0..sum - 1)

  if random_number < entity_a.stats[:agility]
    [entity_a, entity_b]
  else
    [entity_b, entity_a]
  end
end
someone_dead?() click to toggle source

Check if either entity is is dead

@return [Boolean] whether an entity is dead or not

# File lib/goby/battle/battle.rb, line 63
def someone_dead?
  entity_a.stats[:hp] <= 0 || entity_b.stats[:hp] <= 0
end