class Oakdex::Battle::MoveExecution

Represents one Action. One turn has many actions.

Constants

RECALL_PRIORITY

Attributes

action[R]
target[R]

Public Class Methods

new(action, target) click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 16
def initialize(action, target)
  @action = action
  @target = target
end

Public Instance Methods

execute() click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 30
def execute
  return if prevented_by_status_condition?
  pokemon.change_pp_by(move.name, -1)
  if hitting?
    add_uses_move_log
    execute_damage
    execute_stat_modifiers
    execute_status_conditions
    execute_status_condition_callbacks
  else
    add_move_does_not_hit_log
  end

  battle.remove_fainted
end
hitting?() click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 25
def hitting?
  @hitting = rand(1..1000) <= hitting_probability ? 1 : 0
  @hitting == 1
end
hitting_probability() click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 21
def hitting_probability
  ((move.accuracy / 100.0) * (pokemon.accuracy / target.evasion)) * 1000
end

Private Instance Methods

add_changes_no_stat_log(target, stat, change_by) click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 119
def add_changes_no_stat_log(target, stat, change_by)
  add_log 'changes_no_stat', target.trainer.name, target.name,
          stat, change_by
end
add_changes_stat_log(target, stat, change_by) click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 114
def add_changes_stat_log(target, stat, change_by)
  add_log 'changes_stat', target.trainer.name, target.name,
          stat, change_by
end
add_log(*args) click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 110
def add_log(*args)
  battle.add_to_log(*args)
end
add_move_does_not_hit_log() click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 133
def add_move_does_not_hit_log
  add_log 'move_does_not_hit', trainer.name, pokemon.name, move.name
end
add_received_damage_log() click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 137
def add_received_damage_log
  add_log 'received_damage', target.trainer.name, target.name,
          move.name, @damage.damage
end
add_received_no_damage_log() click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 142
def add_received_no_damage_log
  add_log 'received_no_damage', target.trainer.name, target.name,
          move.name
end
add_target_condition_added(condition_name) click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 124
def add_target_condition_added(condition_name)
  add_log 'target_condition_added', target.trainer.name, target.name,
          condition_name
end
add_uses_move_log() click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 129
def add_uses_move_log
  add_log 'uses_move', trainer.name, pokemon.name, move.name
end
execute_damage() click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 95
def execute_damage
  return if move.power.zero?
  @damage = Damage.new(turn, self)
  if @damage.damage > 0
    add_received_damage_log
    target.change_hp_by(-@damage.damage)
  else
    add_received_no_damage_log
  end
end
execute_stat_modifiers() click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 78
def execute_stat_modifiers
  return if move.stat_modifiers.empty?
  move.stat_modifiers.each do |stat_modifier|
    modifier_target = stat_modifier['affects_user'] ? pokemon : target
    stat = stat_modifier['stat']
    stat = random_stat if stat == 'random'
    if modifier_target.change_stat_by(stat.to_sym,
                                      stat_modifier['change_by'])
      add_changes_stat_log(modifier_target, stat,
                           stat_modifier['change_by'])
    else
      add_changes_no_stat_log(modifier_target, stat,
                              stat_modifier['change_by'])
    end
  end
end
execute_status_condition(condition) click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 72
def execute_status_condition(condition)
  return unless rand(1..100) <= condition['probability']
  add_target_condition_added(condition['condition'])
  target.add_status_condition(condition['condition'])
end
execute_status_condition_callbacks() click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 54
def execute_status_condition_callbacks
  return if move.power.zero?
  target.status_conditions.each do |c|
    c.after_received_damage(self)
  end
end
execute_status_conditions() click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 61
def execute_status_conditions
  status_conditions.each do |condition|
    execute_status_condition(condition)
  end
end
prevented_by_status_condition?() click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 48
def prevented_by_status_condition?
  pokemon.status_conditions.reduce(false) do |res, condition|
    res || condition.prevents_move?(self)
  end
end
random_stat() click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 106
def random_stat
  (Oakdex::Pokemon::BATTLE_STATS + InBattlePokemon::OTHER_STATS).sample
end
status_conditions() click to toggle source
# File lib/oakdex/battle/move_execution.rb, line 67
def status_conditions
  return [] if move.in_battle_properties.nil?
  move.in_battle_properties['status_conditions'] || []
end