Class: Oakdex::Battle::Side

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#battleObject (readonly)

Returns the value of attribute battle.



11
12
13
# File 'lib/oakdex/battle/side.rb', line 11

def battle
  @battle
end

#trainersObject (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_pokemonObject



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

Returns:

  • (Boolean)


54
55
56
# File 'lib/oakdex/battle/side.rb', line 54

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

#idObject



58
59
60
# File 'lib/oakdex/battle/side.rb', line 58

def id
  @id ||= trainers.map(&:name).join(',')
end

#next_positionObject



19
20
21
# File 'lib/oakdex/battle/side.rb', line 19

def next_position
  left_position.first
end

#pokemon_in_battle?(position) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


46
47
48
# File 'lib/oakdex/battle/side.rb', line 46

def pokemon_left?
  !active_in_battle_pokemon.empty?
end

#remove_faintedObject



32
33
34
# File 'lib/oakdex/battle/side.rb', line 32

def remove_fainted
  @trainers.each(&:remove_fainted)
end

#send_to_battleObject



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_hObject



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

Returns:

  • (Boolean)


36
37
38
# File 'lib/oakdex/battle/side.rb', line 36

def trainer_on_side?(trainer)
  @trainers.include?(trainer)
end