class Oakdex::Breeding

Pokemon Breeding

Constants

VERSION

Public Class Methods

breed(pokemon1, pokemon2) click to toggle source
# File lib/oakdex/breeding.rb, line 16
def breed(pokemon1, pokemon2)
  new(pokemon1, pokemon2).child
end
chance_in_percentage(pokemon1, pokemon2) click to toggle source
# File lib/oakdex/breeding.rb, line 12
def chance_in_percentage(pokemon1, pokemon2)
  new(pokemon1, pokemon2).chance_in_percentage
end
compatible?(pokemon1, pokemon2) click to toggle source
# File lib/oakdex/breeding.rb, line 8
def compatible?(pokemon1, pokemon2)
  new(pokemon1, pokemon2).compatible?
end
new(pokemon1, pokemon2) click to toggle source
# File lib/oakdex/breeding.rb, line 21
def initialize(pokemon1, pokemon2)
  @female = pokemon1.gender == 'female' ? pokemon1 : pokemon2
  @male = ([pokemon1, pokemon2] - [@female]).first
end

Public Instance Methods

chance_in_percentage() click to toggle source
# File lib/oakdex/breeding.rb, line 31
def chance_in_percentage
  return 0 unless compatible?
  return 50 if same_species?
  20
end
child() click to toggle source
# File lib/oakdex/breeding.rb, line 37
def child
  return unless compatible?
  Oakdex::Pokemon.create(child_species.name,
                         level: 1,
                         additional_moves: enabled_egg_moves
                        )
end
compatible?() click to toggle source
# File lib/oakdex/breeding.rb, line 26
def compatible?
  (opposite_gender? && same_egg_group? && !any_undiscovered?) ||
    (exactly_one_is_ditto? && non_ditto_is_discovered?)
end

Private Instance Methods

any_undiscovered?() click to toggle source
# File lib/oakdex/breeding.rb, line 101
def any_undiscovered?
  (@female.species.egg_groups + @male.species.egg_groups)
    .include?('Undiscovered')
end
child_species() click to toggle source
# File lib/oakdex/breeding.rb, line 47
def child_species
  @child_species ||=
    lowest_in_evolutionary_chain(non_ditto_or_female.species)
end
egg_moves() click to toggle source
# File lib/oakdex/breeding.rb, line 60
def egg_moves
  child_species.learnset
    .map { |l| l['egg_move'] ? l['move'] : nil }
    .compact
end
enabled_egg_moves() click to toggle source
# File lib/oakdex/breeding.rb, line 52
def enabled_egg_moves
  egg_moves & parent_moves
end
exactly_one_is_ditto?() click to toggle source
# File lib/oakdex/breeding.rb, line 76
def exactly_one_is_ditto?
  (@female.name == 'Ditto') ^ (@male.name == 'Ditto')
end
lowest_in_evolutionary_chain(species) click to toggle source
# File lib/oakdex/breeding.rb, line 66
def lowest_in_evolutionary_chain(species)
  # TODO: take incenses into account
  lowest_species = species
  while lowest_species.evolution_from
    lowest_species = Oakdex::Pokedex::Pokemon
      .find!(lowest_species.evolution_from)
  end
  lowest_species
end
non_ditto() click to toggle source
# File lib/oakdex/breeding.rb, line 84
def non_ditto
  return unless exactly_one_is_ditto?
  @female.name == 'Ditto' ? @male : @female
end
non_ditto_is_discovered?() click to toggle source
# File lib/oakdex/breeding.rb, line 80
def non_ditto_is_discovered?
  !non_ditto.species.egg_groups.include?('Undiscovered')
end
non_ditto_or_female() click to toggle source
# File lib/oakdex/breeding.rb, line 89
def non_ditto_or_female
  non_ditto || @female
end
opposite_gender?() click to toggle source
# File lib/oakdex/breeding.rb, line 93
def opposite_gender?
  @female.gender == 'female' && @male.gender == 'male'
end
parent_moves() click to toggle source
# File lib/oakdex/breeding.rb, line 56
def parent_moves
  @female.moves.map(&:name) + @male.moves.map(&:name)
end
same_egg_group?() click to toggle source
# File lib/oakdex/breeding.rb, line 97
def same_egg_group?
  !(@female.species.egg_groups & @male.species.egg_groups).empty?
end
same_species?() click to toggle source
# File lib/oakdex/breeding.rb, line 106
def same_species?
  @female.name == @male.name
end