Class: Oakdex::Battle::Trainer

Inherits:
Object
  • Object
show all
Defined in:
lib/oakdex/battle/trainer.rb

Overview

Represents a Pokemon Trainer. Owns Pokemon and has a name

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, pokemon, items = [], options = {}) ⇒ Trainer

Returns a new instance of Trainer.



8
9
10
11
12
13
14
15
# 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

Instance Attribute Details

#active_in_battle_pokemonObject (readonly)

Returns the value of attribute active_in_battle_pokemon.



5
6
7
# File 'lib/oakdex/battle/trainer.rb', line 5

def active_in_battle_pokemon
  @active_in_battle_pokemon
end

#itemsObject (readonly)

Returns the value of attribute items.



5
6
7
# File 'lib/oakdex/battle/trainer.rb', line 5

def items
  @items
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/oakdex/battle/trainer.rb', line 5

def name
  @name
end

#sideObject

Returns the value of attribute side.



6
7
8
# File 'lib/oakdex/battle/trainer.rb', line 6

def side
  @side
end

#teamObject (readonly)

Returns the value of attribute team.



5
6
7
# File 'lib/oakdex/battle/trainer.rb', line 5

def team
  @team
end

Instance Method Details

#consume_item(item_id) ⇒ Object



36
37
38
39
# 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?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/oakdex/battle/trainer.rb', line 17

def fainted?
  @team.all?(&:fainted?)
end

#grow(defeated_pokemon) ⇒ Object



75
76
77
78
79
80
81
82
# 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_eventObject



25
26
27
# File 'lib/oakdex/battle/trainer.rb', line 25

def growth_event
  growth_events.first
end

#growth_event?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/oakdex/battle/trainer.rb', line 21

def growth_event?
  !growth_events.empty?
end

#left_pokemon_in_teamObject



70
71
72
73
# 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_faintedObject



59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# 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_eventObject



29
30
31
32
33
34
# 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) ⇒ Object



41
42
43
44
45
46
# 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_hObject



84
85
86
87
88
89
90
91
# 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