Class: Pktool::Attack

Inherits:
Object
  • Object
show all
Defined in:
lib/attack.rb

Instance Method Summary collapse

Constructor Details

#initialize(move, attacker, defender) ⇒ Attack

Returns a new instance of Attack.

Raises:



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/attack.rb', line 6

def initialize(move, attacker, defender)
  move = Move.where(name: move).first
  raise Error, "存在ない技です。" unless move
  @move = move
  @attacker = attacker
  @defender = defender
  @effects = []
  @level  = 50.0
  @effect_rate = 1.0
  @type_match = [attacker.type1, attacker.type2].include?(move.attack_type)
  @type_rate = 1.0
  @vital_rate = 1.5
end

Instance Method Details

#add_effect(effect) ⇒ Object



20
21
# File 'lib/attack.rb', line 20

def add_effect(effect)
end

#damageObject



50
51
52
53
54
55
56
57
# File 'lib/attack.rb', line 50

def damage
  type_effect
  select_move_type
  base = fetch_base_damage
  min = base * 0.85
  max = base * 1.0
  {min: min.floor, max: max.floor}
end

#defeatObject



59
60
61
62
# File 'lib/attack.rb', line 59

def defeat
  hp = @defender.statistics(:H)
  return {num: (hp.to_f / damage[:min]).ceil, rate: (damage[:min] / hp.to_f)}
end

#fetch_base_damageObject



46
47
48
# File 'lib/attack.rb', line 46

def fetch_base_damage
  ((@move.power * @attacker.effected_statistics(@attack_stat) * (@level * 2.0 / 5.0 + 2.0 )) / @defender.effected_statistics(@defence_stat) / 50.0 * @effect_rate * type_match + 2.0) * @type_rate
end

#select_move_typeObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/attack.rb', line 35

def select_move_type
  case @move.move_type
  when "物理"
    @attack_stat = :A
    @defence_stat = :B
  when "特殊"
    @attack_stat = :C
    @defence_stat = :D
  end
end

#type_effectObject



23
24
25
26
27
28
29
# File 'lib/attack.rb', line 23

def type_effect
  type_effect = open("data/type.json") do |io|
    JSON.load(io)
  end
  @type_rate *= type_effect[@defender.type1][@move.attack_type]
  @type_rate *= type_effect[@defender.type2][@move.attack_type] unless @defender.type2.empty?
end

#type_matchObject



31
32
33
# File 'lib/attack.rb', line 31

def type_match
   @type_match ? 1.5 : 1.0
end