Class: Oakdex::Battle::Side
- Inherits:
-
Object
- Object
- Oakdex::Battle::Side
- Extended by:
- Forwardable
- Defined in:
- lib/oakdex/battle/side.rb
Overview
Represents a side in an active battle, has n trainers
Instance Attribute Summary collapse
-
#battle ⇒ Object
readonly
Returns the value of attribute battle.
-
#trainers ⇒ Object
readonly
Returns the value of attribute trainers.
Instance Method Summary collapse
- #active_in_battle_pokemon ⇒ Object
- #fainted? ⇒ Boolean
- #id ⇒ Object
-
#initialize(battle, trainers) ⇒ Side
constructor
A new instance of Side.
- #next_position ⇒ Object
- #pokemon_in_battle?(position) ⇒ Boolean
- #pokemon_left? ⇒ Boolean
- #remove_fainted ⇒ Object
- #send_to_battle ⇒ Object
- #to_h ⇒ Object
- #trainer_on_side?(trainer) ⇒ Boolean
Constructor Details
#initialize(battle, trainers) ⇒ Side
Returns a new instance of Side.
13 14 15 16 17 |
# File 'lib/oakdex/battle/side.rb', line 13 def initialize(battle, trainers) @battle = battle @trainers = trainers @trainers.each { |t| t.side = self } end |
Instance Attribute Details
#battle ⇒ Object (readonly)
Returns the value of attribute battle.
11 12 13 |
# File 'lib/oakdex/battle/side.rb', line 11 def battle @battle end |
#trainers ⇒ Object (readonly)
Returns the value of attribute trainers.
11 12 13 |
# File 'lib/oakdex/battle/side.rb', line 11 def trainers @trainers end |
Instance Method Details
#active_in_battle_pokemon ⇒ Object
50 51 52 |
# File 'lib/oakdex/battle/side.rb', line 50 def active_in_battle_pokemon @trainers.map(&:active_in_battle_pokemon).flatten(1) end |
#fainted? ⇒ Boolean
54 55 56 |
# File 'lib/oakdex/battle/side.rb', line 54 def fainted? @trainers.all?(&:fainted?) end |
#id ⇒ Object
58 59 60 |
# File 'lib/oakdex/battle/side.rb', line 58 def id @id ||= trainers.map(&:name).join(',') end |
#next_position ⇒ Object
19 20 21 |
# File 'lib/oakdex/battle/side.rb', line 19 def next_position left_position.first end |
#pokemon_in_battle?(position) ⇒ Boolean
40 41 42 43 44 |
# File 'lib/oakdex/battle/side.rb', line 40 def pokemon_in_battle?(position) active_in_battle_pokemon.any? do |ibp| ibp.position == position end end |
#pokemon_left? ⇒ Boolean
46 47 48 |
# File 'lib/oakdex/battle/side.rb', line 46 def pokemon_left? !active_in_battle_pokemon.empty? end |
#remove_fainted ⇒ Object
32 33 34 |
# File 'lib/oakdex/battle/side.rb', line 32 def remove_fainted @trainers.each(&:remove_fainted) end |
#send_to_battle ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/oakdex/battle/side.rb', line 23 def send_to_battle @trainers.map do |trainer| pokemon_per_trainer.times do |i| break unless trainer.team[i] trainer.send_to_battle(trainer.team[i], self) end end end |
#to_h ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/oakdex/battle/side.rb', line 62 def to_h { id: id, fainted: fainted?, trainers: trainers.map(&:to_h) } end |
#trainer_on_side?(trainer) ⇒ Boolean
36 37 38 |
# File 'lib/oakdex/battle/side.rb', line 36 def trainer_on_side?(trainer) @trainers.include?(trainer) end |