Class: Oakdex::Battle::Action

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/oakdex/battle/action.rb

Overview

Represents one Action. One turn has many actions.

Constant Summary collapse

RECALL_PRIORITY =
7
ITEM_PRIORITY =
6

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trainer, attributes) ⇒ Action

Returns a new instance of Action.



17
18
19
20
# File 'lib/oakdex/battle/action.rb', line 17

def initialize(trainer, attributes)
  @trainer = trainer
  @attributes = attributes
end

Instance Attribute Details

#damageObject (readonly)

Returns the value of attribute damage.



14
15
16
# File 'lib/oakdex/battle/action.rb', line 14

def damage
  @damage
end

#trainerObject (readonly)

Returns the value of attribute trainer.



14
15
16
# File 'lib/oakdex/battle/action.rb', line 14

def trainer
  @trainer
end

#turnObject

Returns the value of attribute turn.



15
16
17
# File 'lib/oakdex/battle/action.rb', line 15

def turn
  @turn
end

Instance Method Details

#executeObject



66
67
68
69
70
71
# File 'lib/oakdex/battle/action.rb', line 66

def execute
  return execute_growth if growth?
  return execute_recall if recall?
  return execute_use_item if item?
  targets.each { |t| MoveExecution.new(self, t).execute }
end

#hitting?Boolean

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/oakdex/battle/action.rb', line 61

def hitting?
  @hitting = rand(1..1000) <= hitting_probability ? 1 : 0
  @hitting == 1
end

#hitting_probabilityObject



57
58
59
# File 'lib/oakdex/battle/action.rb', line 57

def hitting_probability
  ((move.accuracy / 100.0) * (pokemon.accuracy / target.evasion)) * 1000
end

#item_idObject



73
74
75
# File 'lib/oakdex/battle/action.rb', line 73

def item_id
  @attributes['item_id']
end

#moveObject



51
52
53
54
55
# File 'lib/oakdex/battle/action.rb', line 51

def move
  return unless @attributes['move']
  @move ||= pokemon.moves.find { |m| m.name == @attributes['move'] }
  @move ||= Oakdex::Pokemon::Move.create(@attributes['move'])
end

#pokemonObject



26
27
28
29
# File 'lib/oakdex/battle/action.rb', line 26

def pokemon
  return pokemon_by_team_position if item?
  recall? ? pokemon_by_position : battle.pokemon_by_id(pokemon_id)
end

#pokemon_idObject



31
32
33
# File 'lib/oakdex/battle/action.rb', line 31

def pokemon_id
  move? ? @attributes['pokemon'] : nil
end

#pokemon_positionObject



35
36
37
# File 'lib/oakdex/battle/action.rb', line 35

def pokemon_position
  recall? ? @attributes['pokemon'] : nil
end

#priorityObject



22
23
24
# File 'lib/oakdex/battle/action.rb', line 22

def priority
  move&.priority || (recall? ? RECALL_PRIORITY : ITEM_PRIORITY)
end

#targetObject



39
40
41
# File 'lib/oakdex/battle/action.rb', line 39

def target
  recall? ? battle.pokemon_by_id(@attributes['target']) : targets
end

#target_idObject



43
44
45
# File 'lib/oakdex/battle/action.rb', line 43

def target_id
  recall? ? @attributes['target'] : nil
end

#typeObject



47
48
49
# File 'lib/oakdex/battle/action.rb', line 47

def type
  @attributes['action']
end