#==============================================================================
# ■ Game_Damage
#------------------------------------------------------------------------------
# 处理伤害的类,该类在 Game_Battler 的内部使用。
#==============================================================================
class Game_Damage < Hash
include Comparable
#--------------------------------------------------------------------------
# ● 初始化对像
# args : 初始伤害值( HP / SP / 其他信息 )
#--------------------------------------------------------------------------
def initialize(*args)
return if args.empty?
case args[0]
when Numeric
self[:hp] = args[0]
args[1] && self[:sp] = args[1]
args[2] && self[:info] = args[2].to_s
when String
self[:info] = args[0]
when Hash
self.merge!(args[0])
when Array
initialize(*args[0])
end
end
#--------------------------------------------------------------------------
# ● 与数值运算的共轭处理
# other : 被运算的另一个数值
#--------------------------------------------------------------------------
def coerce(other)
other.is_a?(Numeric) ? self.value.coerce(other) : [self.to_s, other.to_s]
end
#--------------------------------------------------------------------------
# ● 伤害数值
#--------------------------------------------------------------------------
def value
# 按照 HP / SP / 其他增加量 / 0 的顺序返回
self[:hp] || self[:sp] || self.values.detect {|x| x.is_a?(Numeric)} || 0
end
#--------------------------------------------------------------------------
# ● 植入 Numeric 类的方法
#--------------------------------------------------------------------------
def to_i; value.to_i; end
def abs; value.abs; end
def ceil; value.ceil; end
def floor; value.floor; end
def round; value.round; end
def truncate; value.truncate; end
def integer?; value.integer?; end
#--------------------------------------------------------------------------
# ● 转为字符串
#--------------------------------------------------------------------------
def to_s
empty? ? Vocab.miss : self[:info] || self.value.to_s
end
#--------------------------------------------------------------------------
# ● Miss 的判定
#--------------------------------------------------------------------------
def missed?
empty? or self[:info] == Vocab.miss
end
#--------------------------------------------------------------------------
# ● 大小比较判定
#--------------------------------------------------------------------------
def <=>(other)
# 不等号另一侧是数值的情况下
if other.is_a?(Numeric)
self.value <=> other
# 不等号另一侧是字符串的情况下
elsif other.is_a?(String)
self.to_s <=> other
# 不等号另一侧是伤害的情况下
elsif other.is_a?(Game_Damage)
# 如果两个伤害都为空
if self.empty? and other.empty?
0
# 如果自己的HP伤害有效
elsif self[:hp]
self[:hp] <=> (other[:hp] || 0)
# 如果另一个HP伤害有效
elsif @other[:hp]
0 <=> other[:hp]
end
end
end
#--------------------------------------------------------------------------
# ● 正号处理
#--------------------------------------------------------------------------
def +@
self.clone
end
#--------------------------------------------------------------------------
# ● 负号处理
#--------------------------------------------------------------------------
def -@
result = clone
result.each {|k, v| result[k] = -v if v.is_a?(Numeric)}
result
end
#--------------------------------------------------------------------------
# ● 四则运算处理
#--------------------------------------------------------------------------
def +(other); calc(other, :+); end
def -(other); self + (-other); end
def *(other); calc(other, :*); end
def /(other); calc(other, :/); end
def %(other); calc(other, :%); end
def **(other); calc(other, :**); end
#--------------------------------------------------------------------------
# ● 四则运算的计算(内部使用)
#--------------------------------------------------------------------------
private
def calc(other, sym)
# 与数值进行运算的情况下
if other.is_a?(Numeric)
result = clone
result.each {|k, v| result[k] = v.send(sym, other) if v.is_a?(Numeric)}
result
# 与另一个伤害进行运算的情况下
else
Game_Damage.new(merge(other) {|k, v1, v2| v1.send(sym, v2)})
end
end
end