#==============================================================================
# 障壁系统
#==============================================================================
# 作者:刹那铃音
#==============================================================================
# 作用简介:
# 让角色或怪物拥有障壁,障壁能抵消伤害。
#==============================================================================
# 脚本冲突:
# 暂未发现脚本冲突,请提前备份好工程,工程数据出现问题作者不予解决。
#==============================================================================
# 使用方法:
# 1.备注:
# 在角色或敌人的技能备注下写入<hp_def x>,战斗开始时角色或敌人获得x点障壁值。
# 2.事件脚本:
#(1)通过战斗中事件的脚本指令调整障壁值的增加和减少。
#add_battler_hp_def(actor,id,x)
#actor:为true时指代角色,为false时指代敌人。
#id:指第id个角色或敌人。
#x:指定角色或敌人的障壁值增加x(x为负数时则为减少)。
#(2)通过战斗中事件的脚本指令调整障壁值的倍数。
#rate_battler_hp_def(actor,id,x)
#actor:为true时指代玩角色,为false时指代敌人。
#id:指第id个角色或敌人。
#x:指定角色或敌人的障壁值在其基础值上乘以x。
#==============================================================================
# 显示的文字:
#============================================================================== 
module Vocab
  ActorDef     = "%s的障壁抵挡了%s点伤害!"
  EnemyDef     = "%s的障壁抵挡了%s点伤害!"
  ActorCurrentDef     = "%s当前障壁:%s!"
  EnemyCurrentDef     = "%s当前障壁:%s!"
end
class RPG::Skill
  def hp_def
    if /<hp_def (\d+)>/i =~ @note;return $1.to_i;else;return 0;end
  end  
end
class RPG::Enemy
  def hp_def
    if /<hp_def (\d+)>/i =~ @note;return $1.to_i;else;return 0;end
  end  
end
 
class Game_ActionResult
  attr_accessor   :hp_def
  #--------------------------------------------------------------------------
  # ● 清除伤害值
  #--------------------------------------------------------------------------
  alias old_804173948_clear_damage_values clear_damage_values
  def clear_damage_values
    old_804173948_clear_damage_values
    @hp_def = 0
  end
  #--------------------------------------------------------------------------
  # ● 获取 HP 抵伤的文字
  #--------------------------------------------------------------------------
  def hp_def_text
    if @hp_def > 0
      fmt = @battler.actor? ? Vocab::ActorDef : Vocab::EnemyDef
      sprintf(fmt, @battler.name, @hp_def)
    end
  end
  #--------------------------------------------------------------------------
  # ● 获取 当前障壁 文字
  #--------------------------------------------------------------------------
  def hp_current_def_text
    fmt = @battler.actor? ? Vocab::ActorCurrentDef : Vocab::ActorCurrentDef
    sprintf(fmt, @battler.name, @battler.hp_def)
  end
end
 
class Window_BattleLog
  #--------------------------------------------------------------------------
  # ● 显示 HP 伤害
  #--------------------------------------------------------------------------
  alias old_804173948_display_hp_damage display_hp_damage
  def display_hp_damage(target, item)
    if target.result.hp_def > 0 && item && item.damage.to_hp?
      add_text(target.result.hp_def_text)
      add_text(target.result.hp_current_def_text)
      wait
    end
    old_804173948_display_hp_damage(target,item);
  end
end
class Game_BattlerBase
  attr_accessor   :hp_def
  #--------------------------------------------------------------------------
  # ● 初始化对象
  #--------------------------------------------------------------------------
  alias old_804173948_initialize initialize
  def initialize
    old_804173948_initialize
    @hp_def = 0
  end
end 
class Game_Battler
  #--------------------------------------------------------------------------
  # ● 处理伤害
  #    调用前需要设置好
  #    @result.hp_damage   @result.mp_damage 
  #    @result.hp_drain    @result.mp_drain
  #--------------------------------------------------------------------------
  def execute_damage(user)
    if @result.hp_damage>0
      if @result.hp_damage<self.hp_def
        self.hp_def -= @result.hp_damage
        @result.hp_def = @result.hp_damage
        @result.hp_damage = 0
      else
        @result.hp_damage -= self.hp_def
        @result.hp_def = self.hp_def
        self.hp_def = 0
      end
    end
    on_damage(@result.hp_damage) if @result.hp_damage > 0
 
    self.hp -= @result.hp_damage
    self.mp -= @result.mp_damage
    user.hp += @result.hp_drain
    user.mp += @result.mp_drain
  end
end
class Game_Actor  
  #--------------------------------------------------------------------------
  # ● 战斗开始处理
  #--------------------------------------------------------------------------
  def on_battle_start
    super
    self.hp_def = battle_hp_def
  end
  def battle_hp_def
    sum = 0
    skills.each{|skill| sum+=skill.hp_def}
    return sum
  end
end
class Game_Enemy
  #--------------------------------------------------------------------------
  # ● 战斗开始处理
  #--------------------------------------------------------------------------
  def on_battle_start
    super
    self.hp_def = battle_hp_def
  end
  def skills
    enemy.actions.collect{|action|$data_skills[action.skill_id]}
  end
  def battle_hp_def
    sum = enemy.hp_def
    skills.each{|skill| sum+=skill.hp_def}
    return sum
  end
end
 
class Game_Interpreter
  def change_battler_hp_def(actor,id,value)
    battler = (actor ? $game_party.members[id-1] : $game_troop.members[id-1])
    battler.hp_def = value
  end
  def add_battler_hp_def(actor,id,value)
    battler = (actor ? $game_party.members[id-1] : $game_troop.members[id-1])
    battler.hp_def += value
  end
  def rate_battler_hp_def(actor,id,value)
    battler = (actor ? $game_party.members[id-1] : $game_troop.members[id-1])
    battler.hp_def = (battler.battle_hp_def * value).round 
  end
end