#==============================================================================
# ■ 翻转动画 by 各种压力的猫君
#------------------------------------------------------------------------------
#  翻转显示动画,支持水平翻转和垂直翻转,理论支持任何地方的动画显示。
#------------------------------------------------------------------------------
#   使用方法:
#     ① 扩大你的动画数据库上限
#     ② 在设定段指定翻转开始编号
#     ③ 需要翻转播放时播放指定编号的动画
#   特殊动画:
#     数据库中动画名以[nf]结尾的动画不会被翻转显示
#     数据库中动画名以[hf]结尾的动画会水平翻转显示
#     数据库中动画名以[vf]结尾的动画会垂直翻转显示
#     识别标示中的字母可在设定段中更改(正则表达式)
#     ※特殊动画的优先级高于数字指定※
#==============================================================================
 
#------------------------------------------------------------------------------
# ● 设定段
#------------------------------------------------------------------------------
 
module FFAni # Full_Flip_Animation
 
  # 水平翻转编号偏移量
  # 若设定为100,则101号动画为1号动画的水平翻转(数据库中可以为空)
  HF_ID = 100
 
  # 垂直翻转编号偏移量(※请确保这个数字比上面一个数字大!※)
  # 若设定为200,则201号动画为1号动画的垂直翻转(数据库中可以为空)
  VF_ID = 200
 
  # 特殊动画识别正则表达式
  NF_Flag = /\[nf\]\z/ # 不翻转
  HF_Flag = /\[hf\]\z/ # 水平翻转
  VF_Flag = /\[vf\]\z/ # 垂直翻转
 
end
 
"         ┏━━━━━━━━━━━━━━━━━┓         "
"         ┃※以下内容无一定脚本基础请勿修改※┃         "
"         ┗━━━━━━━━━━━━━━━━━┛         "
 
#==============================================================================
# ■ RPG::Sprite
#------------------------------------------------------------------------------
#  追加了 RPGXP 使用的各种效果处理的精灵的类。
#==============================================================================
 
module RPG
  class Sprite < ::Sprite
    #------------------------------------------------------------------------
    # ● 播放动画
    #------------------------------------------------------------------------
    def animation(animation, hit)
      dispose_animation
      @_animation = animation
      #---------------------------------------------------
      # 根据动画ID判断翻转情况
      #---------------------------------------------------
      if @_animation.id > FFAni::VF_ID    # 垂直
        full_flip = 2
        fixed_animation_id = @_animation.id - FFAni::VF_ID
      elsif @_animation.id > FFAni::HF_ID # 水平
        full_flip = 1
        fixed_animation_id = @_animation.id - FFAni::HF_ID
      end
      #---------------------------------------------------
      # 根据动画名判断翻转情况
      #---------------------------------------------------
      if(FFAni::NF_Flag =~ @_animation.name)  # 不翻转
        full_flip = 0
      elsif(FFAni::HF_Flag =~ @_animation.name) # 水平
        full_flip = 1
      elsif(FFAni::VF_Flag =~ @_animation.name) # 垂直
        full_flip = 2
      end
      #---------------------------------------------------
      # 修正参数
      #   @_full_flip
      #     0 : 不翻转
      #     1 : 水平翻转
      #     2 : 垂直翻转
      #---------------------------------------------------
      if full_flip == nil
        @_full_flip = 0
      else
        @_full_flip = full_flip
      end
      unless fixed_animation_id == nil
        @_animation = $data_animations[fixed_animation_id]
      end
      #---------------------------------------------------
      return if @_animation == nil
      @_animation_hit = hit
      @_animation_duration = @_animation.frame_max
      animation_name = @_animation.animation_name
      animation_hue = @_animation.animation_hue
      bitmap = RPG::Cache.animation(animation_name, animation_hue)
      if @@_reference_count.include?(bitmap)
        @@_reference_count[bitmap] += 1
      else
        @@_reference_count[bitmap] = 1
      end
      @_animation_sprites = []
      if @_animation.position != 3 or not @@_animations.include?(animation)
        for i in 0..15
          sprite = ::Sprite.new(self.viewport)
          sprite.bitmap = bitmap
          sprite.visible = false
          @_animation_sprites.push(sprite)
        end
        unless @@_animations.include?(animation)
          @@_animations.push(animation)
        end
      end
      update_animation
    end
    #------------------------------------------------------------------------
    # ● 播放循环动画
    #------------------------------------------------------------------------
    def loop_animation(animation)
      return if animation == @_loop_animation
      dispose_loop_animation
      @_loop_animation = animation
      return if @_loop_animation == nil
      @_loop_animation_index = 0
      animation_name = @_loop_animation.animation_name
      animation_hue = @_loop_animation.animation_hue
      bitmap = RPG::Cache.animation(animation_name, animation_hue)
      if @@_reference_count.include?(bitmap)
        @@_reference_count[bitmap] += 1
      else
        @@_reference_count[bitmap] = 1
      end
      @_loop_animation_sprites = []
      for i in 0..15
        sprite = ::Sprite.new(self.viewport)
        sprite.bitmap = bitmap
        sprite.visible = false
        @_loop_animation_sprites.push(sprite)
      end
      update_loop_animation
    end
    #------------------------------------------------------------------------
    # ● 设定动画Sprites
    #------------------------------------------------------------------------
    def animation_set_sprites(sprites, cell_data, position)
      for i in 0..15
        sprite = sprites[i]
        pattern = cell_data[i, 0]
        if sprite == nil or pattern == nil or pattern == -1
          sprite.visible = false if sprite != nil
          next
        end
        sprite.visible = true
        sprite.src_rect.set(pattern % 5 * 192, pattern / 5 * 192, 192, 192)
        if position == 3
          if self.viewport != nil
            sprite.x = self.viewport.rect.width / 2
            sprite.y = self.viewport.rect.height - 160
          else
            sprite.x = 320
            sprite.y = 240
          end
        else
          sprite.x = self.x - self.ox + self.src_rect.width / 2
          sprite.y = self.y - self.oy + self.src_rect.height / 2
          sprite.y -= self.src_rect.height / 4 if position == 0
          sprite.y += self.src_rect.height / 4 if position == 2
        end
        #---------------------------------------------------
        # 脚本本体
        #---------------------------------------------------
        case @_full_flip
        when 0  # 啥也不做
          sprite.x += cell_data[i, 1]
          sprite.y += cell_data[i, 2]
          sprite.angle = cell_data[i, 4]
          sprite.mirror = (cell_data[i, 5] == 1)
        when 1  # 水平翻转
          sprite.x -= cell_data[i, 1]
          sprite.y += cell_data[i, 2]
          sprite.angle = 360 - cell_data[i, 4]
          sprite.mirror = (cell_data[i, 5] == 0)
        when 2  # 垂直翻转
          sprite.x += cell_data[i, 1]
          sprite.y -= cell_data[i, 2]
          temp_angel = cell_data[i, 4] + 180
          sprite.angle = temp_angel > 360 ? temp_angel - 360 : temp_angel    
          sprite.mirror = (cell_data[i, 5] == 0)  
        end
        #---------------------------------------------------
        sprite.z = 2000
        sprite.ox = 96
        sprite.oy = 96
        sprite.zoom_x = cell_data[i, 3] / 100.0
        sprite.zoom_y = cell_data[i, 3] / 100.0
        sprite.opacity = cell_data[i, 6] * self.opacity / 255.0
        sprite.blend_type = cell_data[i, 7]
      end
    end
  end
end