class Oakdex::Battle::Trainer

Represents a Pokemon Trainer. Owns Pokemon and has a name

Attributes

active_in_battle_pokemon[R]
items[R]
name[R]
side[RW]
team[R]

Public Class Methods

new(name, pokemon, items = [], options = {}) click to toggle source
# File lib/oakdex/battle/trainer.rb, line 8
def initialize(name, pokemon, items = [], options = {})
  @name = name
  pokemon.each { |p| p.trainer = self }
  @team = pokemon.map { |p| Oakdex::Battle::InBattlePokemon.new(p) }
  @active_in_battle_pokemon = []
  @items = items
  @options = options
end

Public Instance Methods

consume_item(item_id) click to toggle source
# File lib/oakdex/battle/trainer.rb, line 36
def consume_item(item_id)
  first_index = @items.index(item_id)
  @items.delete_at(first_index)
end
fainted?() click to toggle source
# File lib/oakdex/battle/trainer.rb, line 17
def fainted?
  @team.all?(&:fainted?)
end
grow(defeated_pokemon) click to toggle source
# File lib/oakdex/battle/trainer.rb, line 75
def grow(defeated_pokemon)
  return unless @options[:enable_grow]
  active_in_battle_pokemon.each do |ibp|
    next if ibp.fainted?
    execute_grow_for_pokemon(ibp.pokemon, defeated_pokemon)
  end
  grow_team_pokemon(defeated_pokemon)
end
growth_event() click to toggle source
# File lib/oakdex/battle/trainer.rb, line 25
def growth_event
  growth_events.first
end
growth_event?() click to toggle source
# File lib/oakdex/battle/trainer.rb, line 21
def growth_event?
  !growth_events.empty?
end
left_pokemon_in_team() click to toggle source
# File lib/oakdex/battle/trainer.rb, line 70
def left_pokemon_in_team
  @team.select { |p| !p.fainted? } -
    @active_in_battle_pokemon.map(&:pokemon)
end
remove_fainted() click to toggle source
# File lib/oakdex/battle/trainer.rb, line 59
def remove_fainted
  @active_in_battle_pokemon.each do |ibp|
    next unless ibp.fainted?
    ibp.battle.add_to_log('pokemon_fainted', name, ibp.pokemon.name)
    ibp.pokemon.status_conditions
      .each { |s| s.after_fainted(ibp.battle) }
    other_side_gains(ibp)
  end
  @active_in_battle_pokemon = @active_in_battle_pokemon.reject(&:fainted?)
end
remove_from_battle(pokemon, side) click to toggle source
# File lib/oakdex/battle/trainer.rb, line 48
def remove_from_battle(pokemon, side)
  ibp_to_remove = @active_in_battle_pokemon
    .find { |ibp| ibp.pokemon == pokemon }
  pokemon.reset_stats
  pokemon.status_conditions.each do |s|
    s.after_switched_out(ibp_to_remove.battle)
  end
  @active_in_battle_pokemon -= [ibp_to_remove]
  side.add_to_log 'removes_from_battle', name, pokemon.name
end
remove_growth_event() click to toggle source
# File lib/oakdex/battle/trainer.rb, line 29
def remove_growth_event
  remove_growth_event = growth_event
  return unless remove_growth_event
  pokemon = team.find { |p| p.growth_event == remove_growth_event }
  pokemon.remove_growth_event
end
send_to_battle(pokemon, side) click to toggle source
# File lib/oakdex/battle/trainer.rb, line 41
def send_to_battle(pokemon, side)
  @active_in_battle_pokemon << ActiveInBattlePokemon.new(
    pokemon,
    side, side.next_position)
  side.add_to_log 'sends_to_battle', name, pokemon.name
end
to_h() click to toggle source
# File lib/oakdex/battle/trainer.rb, line 84
def to_h
  {
    name: name,
    items: items,
    team: team.map(&:to_h),
    active_in_battle_pokemon: active_in_battle_pokemon.map(&:to_h)
  }
end

Private Instance Methods

add_choice_to_log(bp) click to toggle source
# File lib/oakdex/battle/trainer.rb, line 123
def add_choice_to_log(bp)
  battle.add_to_log(
    'choice_for',
    name,
    bp.growth_event.message,
    bp.growth_event.possible_actions.join(',')
  )
end
battle() click to toggle source
# File lib/oakdex/battle/trainer.rb, line 95
def battle
  side.battle
end
execute_grow_for_pokemon(bp, defeated_pokemon) click to toggle source
# File lib/oakdex/battle/trainer.rb, line 109
def execute_grow_for_pokemon(bp, defeated_pokemon)
  bp.grow_from_battle(defeated_pokemon.pokemon.pokemon)
  execute_read_only_events(bp)
  return unless bp.growth_event?
  add_choice_to_log(bp)
end
execute_read_only_events(bp) click to toggle source
# File lib/oakdex/battle/trainer.rb, line 116
def execute_read_only_events(bp)
  while bp.growth_event? && bp.growth_event.read_only?
    battle.add_to_log(bp.growth_event.message)
    bp.growth_event.execute
  end
end
grow_team_pokemon(defeated_pokemon) click to toggle source
# File lib/oakdex/battle/trainer.rb, line 99
def grow_team_pokemon(defeated_pokemon)
  return unless @options[:using_exp_share]
  exclude_pokemon = active_in_battle_pokemon.map(&:pokemon)

  (team - exclude_pokemon).each do |pok|
    next if pok.fainted?
    execute_grow_for_pokemon(pok, defeated_pokemon)
  end
end
growth_events() click to toggle source
# File lib/oakdex/battle/trainer.rb, line 145
def growth_events
  team.map(&:growth_event).compact
end
other_side_gains(ibp) click to toggle source
# File lib/oakdex/battle/trainer.rb, line 132
def other_side_gains(ibp)
  winner_sides = ibp.battle.sides - side_of_trainer(ibp.battle.sides)
  winner_sides.each do |side|
    side.trainers.each do |trainer|
      trainer.grow(ibp)
    end
  end
end
side_of_trainer(sides) click to toggle source
# File lib/oakdex/battle/trainer.rb, line 141
def side_of_trainer(sides)
  sides.select { |s| s.trainer_on_side?(self) }
end