#==============================================================================
# +++ MOG - Animation + (V1.5) +++
#==============================================================================
# By Moghunter
# [url]http://www.atelier-rgss.com[/url]
#==============================================================================
# - O script adiciona algumas funções extras para apresentar as animações 
# de batalha.
#==============================================================================
# Coloque as tags abaixo na caixa de notas das habilidades ou itens.
#
# ID = ID of Animation.
#
#------------------------------------------------------------------------------
# ● HIT ANIMATION ●
#
# <Hit Animation = ID>
#
#------------------------------------------------------------------------------
# ● USE ANIMATION (TYPE 1) ● 
#
# <Use Animation = ID>
#
#------------------------------------------------------------------------------
# ● USE ANIMATION (TYPE 2) ● (Wait for animation to complete)
#
# <Wait Use Animation = ID>
#
#------------------------------------------------------------------------------
# ● STATES ANIMATION (LOOP) ● (States Tab) 
#
# <Loop Animation = ID>
#
#------------------------------------------------------------------------------
# ● DEATH ANIMATION ● (Enemy or Actor Tab) 
#
# <Death Animation = ID>
#
#==============================================================================
module MOG_ANIMATION_PLUS
   # Defina aqui as IDs das animações que irão ignorar o tempo de espera
   # para fluir a batalha, caso contrário o sistema de batalha vai
   # esperar todas as animações sem excessão terminarem para poder prosseguir
   # para o próximo estágio de batalha.
   #
   # É aconselhável colocar as animações das condições nesta opção para evitar
   # que o sistema espere a animação do loop terminar.
   #
   IGNORE_WAIT_TIME_ANIMATION_ID = [1,69,109,114,115,116,117,118,119,120]
 
   #Velocidade de loop entre as animações. 
   STATES_LOOP_SPEED = 30
 
   #Definição da animação de level UP.
   LEVEL_UP_ANIMATION = 37
 end
 
#==============================================================================
# Atualizações desta versão. 
#============================================================================
# (Ver 1.4) - Melhoria na compatibilidade com o script Battle Hud EX.
#==============================================================================
 
$imported = {} if $imported.nil?
$imported[:mog_animation_plus] = true
 
#==============================================================================
# ■ Game Battler
#==============================================================================
class Game_Battler < Game_BattlerBase
 
  attr_accessor :animation_loop
 
  #--------------------------------------------------------------------------
  # ● Initialize
  #--------------------------------------------------------------------------
  alias mog_animation_plus_initialize initialize
  def initialize
      mog_animation_plus_initialize
      @animation_loop = []
  end
 
  #--------------------------------------------------------------------------
  # ● Add New State
  #--------------------------------------------------------------------------               
  alias mog_animation_plus_add_new_state add_new_state
  def add_new_state(state_id) 
      mog_animation_plus_add_new_state(state_id)
      check_loop_animation
  end    
 
  #--------------------------------------------------------------------------
  # ● Erase State
  #--------------------------------------------------------------------------             
  alias mog_animation_plus_erase_state erase_state
  def erase_state(state_id)  
      mog_animation_plus_erase_state(state_id)  
      check_loop_animation
  end  
 
  #--------------------------------------------------------------------------
  # ● Check Loop Animation
  #--------------------------------------------------------------------------               
  def check_loop_animation
      @animation_loop.clear
      self.states.each_with_index do |i, index|
      if i.note =~ /<Loop Animation = (\d+)>/i    
         @animation_loop.push([$1.to_i,0, index + 1, true]) 
      end
      end
  end
 
  #--------------------------------------------------------------------------
  # ● Item Apply
  #--------------------------------------------------------------------------           
  alias mog_animation_plus_item_apply item_apply
  def item_apply(user, item)
      mog_animation_plus_item_apply(user, item)
      execute_animation_plus_hit(item) if @result.success
  end
 
  #--------------------------------------------------------------------------
  # ● Execute Animation Plus Hit
  #--------------------------------------------------------------------------           
  def execute_animation_plus_hit(item)
      return if item == nil
      return if !SceneManager.scene_is?(Scene_Battle)
      self.animation_id = $1.to_i if item.note =~ /<Hit Animation = (\d+)>/i   
      if self.dead?
         if self.is_a?(Game_Enemy)
            battler = $data_enemies[self.enemy_id]
         else
            battler = $data_actors[self.id]
         end  
         self.animation_id = $1.to_i if battler.note =~ /<Death Animation = (\d+)>/i
      end 
  end
 
end
 
#==============================================================================
# ■ Sprite_Base
#==============================================================================
class Sprite_Base < Sprite  
 
  #--------------------------------------------------------------------------
  # ● Battle Animation?
  #--------------------------------------------------------------------------             
  def battle_animation?
      if @animation != nil
         if MOG_ANIMATION_PLUS::IGNORE_WAIT_TIME_ANIMATION_ID.include?(@animation.id)
            return false
         end
         return true          
      end  
      return false 
  end
 
end  
 
 
#==============================================================================
# ■ Sprite Battler
#==============================================================================
class Sprite_Battler < Sprite_Base
 
  #--------------------------------------------------------------------------
  # ● Update Position
  #--------------------------------------------------------------------------             
  alias mog_animation_plus_update_position update_position
  def update_position
      mog_animation_plus_update_position
      update_loop_animation if can_loop_animation?
  end
 
  #--------------------------------------------------------------------------
  # ● Can Loop Animation?
  #--------------------------------------------------------------------------             
  def can_loop_animation?
      return false if BattleManager.in_turn?
      return false if self.animation?
      return false if @battler.animation_loop.empty? or @battler.dead?  
      if $imported[:mog_battle_hud_ex] 
      return false if $game_message.visible and MOG_BATTLE_HUD_EX::MESSAGE_WINDOW_FADE_HUD
      return false if !$game_temp.battle_hud_visible
      end          
      return true
  end
 
  #--------------------------------------------------------------------------
  # ● Update Loop Animation
  #--------------------------------------------------------------------------               
  def update_loop_animation 
      for i in @battler.animation_loop
          next if !i[3]
          i[1] += 1 
          if i[1] >= MOG_ANIMATION_PLUS::STATES_LOOP_SPEED
             i[1] = 0 ; @battler.animation_id = i[0] ; i[3] = false
             if i[2] >= @battler.animation_loop.size
                for i in @battler.animation_loop
                    i[1] = 0 ; i[3] = true
                end                 
            end  
          end
          break           
      end        
  end
 
  #--------------------------------------------------------------------------
  # ● Update Z Correction
  #--------------------------------------------------------------------------                   
  def update_z_correction
      return if !@animation
      @ani_sprites.each do |sprite| sprite.z = self.z + 1 end
  end  
 
end
 
#==============================================================================
# ■ Spriteset_Battle
#==============================================================================
class Spriteset_Battle
 
  #--------------------------------------------------------------------------
  # ● Animation?
  #--------------------------------------------------------------------------                     
  def animation?
      battler_sprites.any? {|sprite| sprite.battle_animation? }
  end  
 
end
 
#==============================================================================
# ■ Scene Battle
#==============================================================================
class Scene_Battle < Scene_Base
 
  #--------------------------------------------------------------------------
  # ● Start
  #--------------------------------------------------------------------------
  alias mog_animation_plus_start start
  def start
      $game_party.members.each {|actor| actor.check_loop_animation }    
      mog_animation_plus_start
  end
 
  #--------------------------------------------------------------------------
  # ● Use Item
  #--------------------------------------------------------------------------         
  alias mog_animation_plus_use_item use_item
  def use_item
      execute_cast_animation
      mog_animation_plus_use_item        
  end
 
  #--------------------------------------------------------------------------
  # ● Execute Cast Animation
  #--------------------------------------------------------------------------       
  def execute_cast_animation
      return if @subject.current_action.item == nil rescue return
      item = @subject.current_action.item
      if item.note =~ /<Use Animation = (\d+)>/i 
         execute_animation(@subject, $1.to_i,false)    
      elsif item.note =~ /<Wait Use Animation = (\d+)>/i  
         execute_animation(@subject, $1.to_i,true)    
      end  
  end  
 
  #--------------------------------------------------------------------------
  # ● Execute Animation
  #--------------------------------------------------------------------------     
  def execute_animation(subject , anime_id = 0, wait_animation = false)
      return if anime_id <= 0 or subject == nil
      subject.animation_id = anime_id rescue nil
      if wait_animation
         duration = ($data_animations[anime_id].frame_max * 4) + 1
         for i in 0..duration
             @spriteset.update ; Graphics.update
         end      
      end    
  end  
 
end    
 
#==============================================================================
# ■ Game Actor
#==============================================================================
class Game_Actor < Game_Battler
 
  #--------------------------------------------------------------------------
  # ● Level UP
  #--------------------------------------------------------------------------       
  alias mog_animation_plus_level_up level_up
  def level_up
       mog_animation_plus_level_up
       self.animation_id = MOG_ANIMATION_PLUS::LEVEL_UP_ANIMATION if can_lvup_animation?
  end
 
  #--------------------------------------------------------------------------
  # ● Can Lvup Animation
  #--------------------------------------------------------------------------       
  def can_lvup_animation?
      return false if !use_sprite?
      return false if !SceneManager.scene_is?(Scene_Battle)
      if $imported[:mog_battle_hud_ex] 
          return false if $game_message.visible and MOG_BATTLE_HUD_EX::MESSAGE_WINDOW_FADE_HUD
          return false if !$game_temp.battle_hud_visible
      end
      return true
  end
 
end