Class: Oakdex::Battle::Trainer
- Inherits:
-
Object
- Object
- Oakdex::Battle::Trainer
- Defined in:
- lib/oakdex/battle/trainer.rb
Overview
Represents a Pokemon Trainer. Owns Pokemon and has a name
Instance Attribute Summary collapse
-
#active_in_battle_pokemon ⇒ Object
readonly
Returns the value of attribute active_in_battle_pokemon.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#side ⇒ Object
Returns the value of attribute side.
-
#team ⇒ Object
readonly
Returns the value of attribute team.
Instance Method Summary collapse
- #consume_item(item_id) ⇒ Object
- #fainted? ⇒ Boolean
- #grow(defeated_pokemon) ⇒ Object
- #growth_event ⇒ Object
- #growth_event? ⇒ Boolean
-
#initialize(name, pokemon, items = [], options = {}) ⇒ Trainer
constructor
A new instance of Trainer.
- #left_pokemon_in_team ⇒ Object
- #remove_fainted ⇒ Object
- #remove_from_battle(pokemon, side) ⇒ Object
- #remove_growth_event ⇒ Object
- #send_to_battle(pokemon, side) ⇒ Object
- #to_h ⇒ Object
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 = [], = {}) @name = name pokemon.each { |p| p.trainer = self } @team = pokemon.map { |p| Oakdex::Battle::InBattlePokemon.new(p) } @active_in_battle_pokemon = [] @items = items @options = end |
Instance Attribute Details
#active_in_battle_pokemon ⇒ Object (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 |
#items ⇒ Object (readonly)
Returns the value of attribute items.
5 6 7 |
# File 'lib/oakdex/battle/trainer.rb', line 5 def items @items end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/oakdex/battle/trainer.rb', line 5 def name @name end |
#side ⇒ Object
Returns the value of attribute side.
6 7 8 |
# File 'lib/oakdex/battle/trainer.rb', line 6 def side @side end |
#team ⇒ Object (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
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_event ⇒ Object
25 26 27 |
# File 'lib/oakdex/battle/trainer.rb', line 25 def growth_event growth_events.first end |
#growth_event? ⇒ Boolean
21 22 23 |
# File 'lib/oakdex/battle/trainer.rb', line 21 def growth_event? !growth_events.empty? end |
#left_pokemon_in_team ⇒ Object
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_fainted ⇒ Object
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_event ⇒ Object
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_h ⇒ Object
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 |