class Oakdex::Battle::ActiveInBattlePokemon

Represents a pokemon that is actively fighting in battle

Attributes

pokemon[R]
position[R]
side[R]

Public Class Methods

new(pokemon, side, position = 0) click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 14
def initialize(pokemon, side, position = 0)
  @pokemon = pokemon
  @side = side
  @position = position
end

Public Instance Methods

action_added?() click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 20
def action_added?
  actions.any? { |a| a.pokemon_id == id }
end
to_h() click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 40
def to_h
  {
    id: id
  }
end
valid_move_actions() click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 24
def valid_move_actions
  return [] if action_added?
  moves = moves_with_pp
  moves = [struggle_move] if moves_with_pp.empty?
  moves.flat_map do |move|
    targets_in_battle(move).map do |target|
      {
        'action' => 'move',
        'pokemon' => pokemon.id,
        'move' => move.name,
        'target' => target
      }
    end
  end
end

Private Instance Methods

actions() click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 157
def actions
  battle.actions
end
adjacent() click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 114
def adjacent
  adjacent_foes + adjacent_users
end
adjacent_foes() click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 122
def adjacent_foes
  [
    [other_side.id, position - 1],
    [other_side.id, position],
    [other_side.id, position + 1]
  ].select { |t| t[1] >= 0 && t[1] < pokemon_per_side }
end
adjacent_users() click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 130
def adjacent_users
  [
    [@side.id, position - 1],
    [@side.id, position + 1]
  ].select { |t| t[1] >= 0 && t[1] < pokemon_per_side }
end
all_foes() click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 141
def all_foes
  pokemon_per_side.times.map { |i| [other_side.id, i] }
end
all_targets() click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 106
def all_targets
  all_foes + all_users
end
all_users() click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 137
def all_users
  pokemon_per_side.times.map { |i| [@side.id, i] }
end
available_targets(move) click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 75
def available_targets(move)
  with_target(move) || multiple_targets_adjacent(move) ||
    multiple_targets(move) || []
end
multiple_targets(move) click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 80
def multiple_targets(move)
  case move.target
  when 'all_users' then [all_users]
  when 'all_except_user' then [all_targets - [self_target]]
  when 'all' then [all_targets]
  when 'all_foes' then [all_foes]
  end
end
multiple_targets_adjacent(move) click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 89
def multiple_targets_adjacent(move)
  case move.target
  when 'all_adjacent' then [adjacent]
  when 'adjacent_foes_all' then [adjacent_foes]
  end
end
other_side() click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 149
def other_side
  other_sides.first
end
other_sides() click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 153
def other_sides
  battle.sides - [@side]
end
pokemon_per_side() click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 145
def pokemon_per_side
  battle.pokemon_per_side
end
self_target() click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 118
def self_target
  [@side.id, position]
end
struggle_move() click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 71
def struggle_move
  @struggle_move ||= Oakdex::Pokemon::Move.create('Struggle')
end
target_adjacent_single() click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 110
def target_adjacent_single
  adjacent_foes + adjacent_users
end
target_in_battle?(target) click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 65
def target_in_battle?(target)
  side = battle.side_by_id(target[0])
  side.pokemon_in_battle?(target[1]) ||
    (!side.pokemon_left? && target[1] == 0)
end
targets_in_battle(move) click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 48
def targets_in_battle(move)
  available_targets(move).map do |targets|
    if targets.last.is_a?(Array)
      targets if targets_in_battle?(targets)
    elsif target_in_battle?(targets)
      targets
    end
  end.compact.reject(&:empty?)
end
targets_in_battle?(targets) click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 58
def targets_in_battle?(targets)
  targets.any? do |target|
    side = battle.side_by_id(target[0])
    side.pokemon_in_battle?(target[1])
  end
end
with_target(move) click to toggle source
# File lib/oakdex/battle/active_in_battle_pokemon.rb, line 96
def with_target(move)
  case move.target
  when 'user', 'user_and_random_adjacent_foe' then [self_target]
  when 'target_adjacent_user_single' then adjacent_users
  when 'target_adjacent_single' then adjacent
  when 'target_user_or_adjacent_user'
    [self_target] + adjacent_users
  end
end