class Oakdex::Battle::Turn

Represents one Turn within the battle

Attributes

battle[R]

Public Class Methods

new(battle, actions) click to toggle source
# File lib/oakdex/battle/turn.rb, line 12
def initialize(battle, actions)
  @battle = battle
  @actions = actions
end

Public Instance Methods

execute() click to toggle source
# File lib/oakdex/battle/turn.rb, line 17
def execute
  execute_status_conditions(:before_turn)

  @actions.each { |a| a.turn = self }
  ordered_actions.each do |action|
    next unless valid_target?(action)
    next if action.pokemon && action.pokemon.fainted?
    action.execute
  end

  execute_status_conditions(:after_turn)
end

Private Instance Methods

compare_actions(a, b) click to toggle source
# File lib/oakdex/battle/turn.rb, line 56
def compare_actions(a, b)
  a_prio = a.priority
  b_prio = b.priority
  if a_prio == b_prio && a_prio < 6
    if a.pokemon.speed == b.pokemon.speed
      [1, -1].sample
    else
      b.pokemon.speed <=> a.pokemon.speed
    end
  else
    b_prio <=> a_prio
  end
end
execute_status_conditions(method) click to toggle source
# File lib/oakdex/battle/turn.rb, line 32
def execute_status_conditions(method)
  status_conditions.each do |status_condition|
    status_condition.public_send(method, self)
  end
  battle.remove_fainted
end
ordered_actions() click to toggle source
# File lib/oakdex/battle/turn.rb, line 52
def ordered_actions
  @ordered_actions ||= @actions.sort { |a, b| compare_actions(a, b) }
end
status_conditions() click to toggle source
# File lib/oakdex/battle/turn.rb, line 39
def status_conditions
  sides.flat_map(&:active_in_battle_pokemon)
    .map(&:pokemon)
    .flat_map(&:status_conditions)
end
valid_target?(action) click to toggle source
# File lib/oakdex/battle/turn.rb, line 45
def valid_target?(action)
  targets = action.target.is_a?(Array) ? action.target : [action.target]
  targets.all? do |target|
    !target.nil? && !target.fainted?
  end
end