#==============================================================================
# +++ MOG - ADVANCED BATTLE HUD  (v1.4) +++
#==============================================================================
# By Moghunter 
# [url]http://www.atelier-rgss.com/[/url]
#==============================================================================
# Sistema de hud avançado de batalha.
#==============================================================================
 
#==============================================================================
# ● Histórico (Version History)
#==============================================================================
# v 1.4 - Corrigido o erro de crash randômico. (relativo a dispose de imagem.)
# v 1.3 - Script 100% independente do sistema de AT System.
#       - Correção da posição inicial da janela de Fuga.
#       - Correção da posição do cursor nos aliados quando a face está
#         desabilitada.
# v 1.2 - Corrigido a prioridade das condições.
# v 1.1 - Corrigido o glitch inicial da prioridade da face.
# v 1.0 - Primeiro lançamento.
#==============================================================================
 
#==============================================================================
# ■ - FACE ANIMADAS DOS PERSONAGENS - (Opcional)
#==============================================================================
# 1 - Grave as imagens das faces dos personagens na pasta.
#
# GRAPHICS/BATTLERS/
#
# 2 - Nomeie a imagem com o mesmo nome do personagem. (EG - Hertor.png)
# 3 - A largura da imagem deverá ser dividido por 5. (A escolha do tamanho
#     da face é livre desde que a lagura dividido pela altura seja igual a 5 )
#
#==============================================================================
 
#==============================================================================
# ■ CURSOR SETTING
#==============================================================================
module MOG_BATTLE_CURSOR
  #相對於目標格式光標位置。
  CURSOR_POSITION = [-62, -30]
  #目標名稱位置的定義。
  CURSOR_NAME_POSITION = [-71, 38]
  #啟用效果的幻燈片。
  CURSOR_SLIDE_EFFECT = true
  #啟用動畫磁懸浮。
  CURSOR_FLOAT_EFFECT = true  
  #光標優先級設置。
  CURSOR_Z = 500
  ACTOR_POSITION = [[478,190, 0],[498,230, 0],[518,270, 0],[538,310, 0]]  #選擇玩家腳色位置
end  
 
#==============================================================================
# ■ Scene Battle
#==============================================================================
class Scene_Battle < Scene_Base
  #--------------------------------------------------------------------------
  # ● Update
  #--------------------------------------------------------------------------  
  alias mog_battle_hud_update_basic update_basic
  def update_basic
      mog_battle_hud_update_basic
      update_command_window_visible
  end   
  #--------------------------------------------------------------------------
  # ● Update Command Window Visible
  #--------------------------------------------------------------------------    
  def update_command_window_visible
      @status_window.visible
      @actor_command_window.visible
      #@status_window.visible = @status_window.active ? true : false
      #@actor_command_window.visible = @actor_command_window.active ? true : false
      @skill_window.visible = @skill_window.active ? true : false
      @item_window.visible = @item_window.active ? true : false
      @help_window.visible = @skill_window.active || @item_window.active  ? true : false
  end
end
class Game_Temp 
 
  attr_accessor :battle_cursor
 
  #--------------------------------------------------------------------------
  # ● Initialize
  #-------------------------------------------------------------------------- 
  alias mog_battle_cursor_initialize initialize
  def initialize
      @battle_cursor = [0,0,true,""]
      mog_battle_cursor_initialize
  end   
end
 
#==============================================================================
# ■ Spriteset Battle
#==============================================================================
class Spriteset_Battle
 
  include MOG_BATTLE_CURSOR
 
  #--------------------------------------------------------------------------
  # ● Initialize
  #--------------------------------------------------------------------------      
  alias mog_battle_cursor_initialize initialize
  def initialize
      mog_battle_cursor_initialize
      create_cursor
  end
 
  #--------------------------------------------------------------------------
  # ● Create_Cursor
  #--------------------------------------------------------------------------        
  def create_cursor
      @cursor = Sprite.new
      @cursor.bitmap = Cache.system("Battle_Cursor")
      @cursor.z = 999
      @cursor.visible = false
      @cursor_new_x = -64
      @cursor_new_y = -64
      @cursor_float_y = 0
      @cursor_float_duration = 0
      @c_real_y = 0
      @cursor_name = Sprite.new
      @cursor_name.bitmap = Bitmap.new(240,32)
      @cursor_name.z = @cursor.z + 1
      @cursor_name.bitmap.font.size = 18
      refresh_cursor_name
      update_cursor_name
      $game_temp.battle_cursor = [@cursor_new_x,@cursor_new_y,false]
 
 
  end  
 
  #--------------------------------------------------------------------------
  # ● Refresh Cursor Name
  #--------------------------------------------------------------------------              
  def refresh_cursor_name 
      return if  !(@cursor.x == $game_temp.battle_cursor[0] and
                   @cursor.y == @c_real_y)
      @cursor_name_enemy = $game_temp.battle_cursor[3]
      @cursor_name.bitmap.clear      
      @cursor_name.bitmap.draw_text(0,0,240,32,@cursor_name_enemy.to_s,1)      
  end
 
  #--------------------------------------------------------------------------
  # ● Dispose
  #--------------------------------------------------------------------------      
  alias mog_battle_cursor_dispose dispose
  def dispose
      mog_battle_cursor_dispose
      dispose_cursor
  end  
 
  #--------------------------------------------------------------------------
  # ● Dispose Cursor
  #--------------------------------------------------------------------------        
  def dispose_cursor    
      @cursor.bitmap.dispose
      @cursor.dispose
      @cursor_name.bitmap.dispose
      @cursor_name.dispose
  end
 
  #--------------------------------------------------------------------------
  # ● Update
  #--------------------------------------------------------------------------          
  alias mog_battle_cursor_update update
  def update
      mog_battle_cursor_update
      update_battle_cursor
  end  
 
  #--------------------------------------------------------------------------
  # ● Update Battle Cursor
  #--------------------------------------------------------------------------            
  def update_battle_cursor
      return if @cursor == nil
      @cursor.visible = $game_temp.battle_cursor[2]
      update_cursor_name
      if !@cursor.visible
         @cursor.x = -64
         @cursor.y = -64
         @cursor.opacity = 0
         update_cursor_name
         return
      end        
      @cursor.opacity += 15 
      update_cursor_float_effect
      if CURSOR_SLIDE_EFFECT
         update_cursor_slide_effect
      else
         @cursor.x = $game_temp.battle_cursor[0]
         @cursor.y = $game_temp.battle_cursor[1]
      end
  end
 
  #--------------------------------------------------------------------------
  # ● Update Cursor Name
  #--------------------------------------------------------------------------              
  def update_cursor_name
      refresh_cursor_name if @cursor_name_enemy != $game_temp.battle_cursor[3]
      @cursor_name.x = @cursor.x + CURSOR_NAME_POSITION[0] 
      @cursor_name.y = @cursor.y + CURSOR_NAME_POSITION[1]
      @cursor_name.opacity = @cursor.opacity
      @cursor_name.visible = @cursor.visible    
  end  
 
  #--------------------------------------------------------------------------
  # ● Update Cursor Slide Effect
  #--------------------------------------------------------------------------                
  def update_cursor_slide_effect
      @cursor_new_x = $game_temp.battle_cursor[0]
      @cursor_new_y = $game_temp.battle_cursor[1]      
      @cs_x = 5 + ((@cursor.x - @cursor_new_x).abs / 5)
      @cs_y = 5 + ((@cursor.y - @cursor_new_y).abs / 5)
      if @cursor.x > @cursor_new_x 
         @cursor.x -= @cs_x
         @cursor.x = @cursor_new_x if @cursor.x < @cursor_new_x
      elsif  @cursor.x < @cursor_new_x 
         @cursor.x += @cs_x
         @cursor.x = @cursor_new_x if @cursor.x > @cursor_new_x
      end     
      @c_real_y = @cursor_new_y + @cursor_float_y 
      if @cursor.y > @cursor_new_y 
         @cursor.y -= @cs_y
         @cursor.y = @c_real_y if @cursor.y < @c_real_y
      elsif  @cursor.y < @c_real_y 
         @cursor.y += @cs_y
         @cursor.y = @c_real_y if @cursor.y > @c_real_y
      end  
  end
 
  #--------------------------------------------------------------------------
  # ● Update Cursor Float Effect
  #--------------------------------------------------------------------------              
  def update_cursor_float_effect
      return if !CURSOR_FLOAT_EFFECT
      @cursor_float_duration += 1
      case @cursor_float_duration
        when 0..20
          @cursor_float_y += 1
        when 21..40
          @cursor_float_y  -= 1
        else
          @cursor_float_y = 0
          @cursor_float_duration = 0
      end        
  end
 
end
class Scene_Battle < Scene_Base
 
  #--------------------------------------------------------------------------
  # ● Create Enemy Window
  #--------------------------------------------------------------------------
  def create_enemy_window
      @enemy_window = Window_BattleEnemy_Cursor.new
      @enemy_window.set_handler(:ok,     method(:on_enemy_ok))
      @enemy_window.set_handler(:cancel, method(:on_enemy_cancel))
  end
 
  #--------------------------------------------------------------------------
  # ● Create Actor Window
  #--------------------------------------------------------------------------                
  def create_actor_window
      @actor_window = Window_BattleActor_Cursor.new
      @actor_window.set_handler(:ok,     method(:on_actor_ok))
      @actor_window.set_handler(:cancel, method(:on_actor_cancel))
  end  
 
end
#==============================================================================
# ■ Window Selectable Battle_Cursor
#==============================================================================
class Window_Selectable_Battle_Cursor < Window_Base
 
  attr_reader   :index
 
  #--------------------------------------------------------------------------
  # ● Initialize
  #-------------------------------------------------------------------------
  def initialize(x, y, width, height)
      super
      @index = -1
      @handler = {}
      deactivate
  end
 
  #--------------------------------------------------------------------------
  # ● Item Max
  #--------------------------------------------------------------------------
  def item_max
    return 0
  end
 
  #--------------------------------------------------------------------------
  # ● Active
  #--------------------------------------------------------------------------
  def active=(active)
      super
  end
 
  #--------------------------------------------------------------------------
  # ● Index
  #--------------------------------------------------------------------------
  def index=(index)
      @index = index
  end
 
  #--------------------------------------------------------------------------
  # ● Set Handler
  #--------------------------------------------------------------------------
  def set_handler(symbol, method)
      @handler[symbol] = method
  end
 
  #--------------------------------------------------------------------------
  # ● Handle?
  #--------------------------------------------------------------------------
  def handle?(symbol)
      @handler.include?(symbol)
  end
 
  #--------------------------------------------------------------------------
  # ● Call Handler
  #--------------------------------------------------------------------------
  def call_handler(symbol)
      @handler[symbol].call if handle?(symbol)
  end
 
  #--------------------------------------------------------------------------
  # ● Cursor Movable
  #--------------------------------------------------------------------------
  def cursor_movable?
      active && open? && !@cursor_fix && !@cursor_all && item_max > 0
  end  
 
  #--------------------------------------------------------------------------
  # ● Cursor Down
  #--------------------------------------------------------------------------  
  def cursor_down(wrap = false)
      self.index += 1
      check_index_limit
  end
 
  #--------------------------------------------------------------------------
  # ● Cursor Right
  #--------------------------------------------------------------------------
  def cursor_right(wrap = false)
      self.index += 1
      check_index_limit
  end  
 
  #--------------------------------------------------------------------------
  # ● Cursor UP
  #--------------------------------------------------------------------------
  def cursor_up(wrap = false)
      self.index -= 1
      check_index_limit
  end  
 
  #--------------------------------------------------------------------------
  # ● Cursor Left
  #--------------------------------------------------------------------------
  def cursor_left(wrap = false)
      self.index -= 1
      check_index_limit(self.index)
  end
 
  #--------------------------------------------------------------------------
  # ● Update
  #--------------------------------------------------------------------------
  def update
      super
      process_cursor_move
      process_handling
  end
 
  #--------------------------------------------------------------------------
  # ● Process Cursor Move
  #--------------------------------------------------------------------------
  def process_cursor_move
      return unless cursor_movable?
      last_index = @index
      cursor_down (Input.trigger?(:DOWN))  if Input.repeat?(:DOWN)
      cursor_up   (Input.trigger?(:UP))    if Input.repeat?(:UP)
      cursor_right(Input.trigger?(:RIGHT)) if Input.repeat?(:RIGHT)
      cursor_left (Input.trigger?(:LEFT))  if Input.repeat?(:LEFT)
      if @index != last_index
         Sound.play_cursor
         set_cursor_position(@index)
      end   
  end
 
  #--------------------------------------------------------------------------
  # ● Process Handling
  #--------------------------------------------------------------------------
  def process_handling
      return unless open? && active
      return process_ok       if ok_enabled?        && Input.trigger?(:C)
      return process_cancel   if cancel_enabled?    && Input.trigger?(:B)
  end
 
  #--------------------------------------------------------------------------
  # ● OK Enabled
  #--------------------------------------------------------------------------
  def ok_enabled?
      handle?(:ok)
  end
 
  #--------------------------------------------------------------------------
  # ● Cancel Enabled
  #--------------------------------------------------------------------------
  def cancel_enabled?
      handle?(:cancel)
  end
 
  #--------------------------------------------------------------------------
  # ● Process OK
  #--------------------------------------------------------------------------
  def process_ok
      if current_item_enabled?
         Sound.play_ok
         Input.update
         deactivate
         call_ok_handler
      else
         Sound.play_buzzer
      end
  end
 
  #--------------------------------------------------------------------------
  # ● Call OK Handler
  #--------------------------------------------------------------------------
  def call_ok_handler
      call_handler(:ok)
  end
 
  #--------------------------------------------------------------------------
  # ● Process Cancel
  #--------------------------------------------------------------------------
  def process_cancel
      Sound.play_cancel
      Input.update
      deactivate
      call_cancel_handler
  end
 
  #--------------------------------------------------------------------------
  # ● Call Cancel Handler
  #--------------------------------------------------------------------------
  def call_cancel_handler
      call_handler(:cancel)
  end
 
  #--------------------------------------------------------------------------
  # ● Set Cursor Position
  #--------------------------------------------------------------------------    
  def set_cursor_position(index)    
  end  
 
  #--------------------------------------------------------------------------
  # ● Current Item Enabled?
  #--------------------------------------------------------------------------
  def current_item_enabled?
      return true
  end
 
  #--------------------------------------------------------------------------
  # ● Refresh
  #--------------------------------------------------------------------------
  def refresh
      contents.clear
  end
 
  #--------------------------------------------------------------------------
  # ● Show
  #--------------------------------------------------------------------------
  def show
      set_cursor_position(self.index)
      $game_temp.battle_cursor[2] = true
      super
  end
 
  #--------------------------------------------------------------------------
  # ● Hide
  #--------------------------------------------------------------------------
  def hide
      $game_temp.battle_cursor[2] = false
      super
  end  
 
  #--------------------------------------------------------------------------
  # ● Check Index Limit
  #--------------------------------------------------------------------------      
  def check_index_limit(index = self.index)
      self.index = index
      self.index = 0 if self.index >= item_max
      self.index = (item_max - 1) if self.index < 0
  end      
 
end
#==============================================================================
# ■ Window_BattleEnemy
#==============================================================================
class Window_BattleEnemy_Cursor < Window_Selectable_Battle_Cursor
 
  include MOG_BATTLE_CURSOR
 
  #--------------------------------------------------------------------------
  # ● Initialize
  #--------------------------------------------------------------------------
  def initialize
      super(0, 0, 640, 48)
      self.index = 0
      self.visible = false
      set_cursor_position(self.index) 
  end    
  #--------------------------------------------------------------------------
  # ● Item Max
  #--------------------------------------------------------------------------
  def item_max
      $game_troop.alive_members.size
  end
 
  #--------------------------------------------------------------------------
  # ● Enemy
  #--------------------------------------------------------------------------  
  def enemy
      $game_troop.alive_members[self.index]
  end  
 
 
  #--------------------------------------------------------------------------
  # ● Set Cursor Position
  #--------------------------------------------------------------------------    
  def set_cursor_position(index)
      check_index_limit(index)
      return if $game_troop.alive_members[self.index] == nil
      $game_temp.battle_cursor[0] = $game_troop.alive_members[self.index].screen_x + CURSOR_POSITION[0]
      $game_temp.battle_cursor[1] = $game_troop.alive_members[self.index].screen_y + CURSOR_POSITION[1]
      $game_temp.battle_cursor[3] = $game_troop.alive_members[self.index].name
      refresh
      draw_text(0, 0, 300, 24, "型態:")
      draw_text(140, 0, 300, 24, "狀態:")
      draw_text(60, 0, 300, 24, "火") if enemy.enemy.note.include?("火")
      draw_text(60, 0, 300, 24, "冰") if enemy.enemy.note.include?("冰")
      draw_text(60, 0, 300, 24, "雷") if enemy.enemy.note.include?("雷")
      draw_text(60, 0, 300, 24, "土") if enemy.enemy.note.include?("土")
      draw_text(60, 0, 300, 24, "風") if enemy.enemy.note.include?("風")
      draw_text(60, 0, 300, 24, "光") if enemy.enemy.note.include?("光")
      draw_text(60, 0, 300, 24, "闇") if enemy.enemy.note.include?("闇")
      draw_text(60, 0, 300, 24, "飛行") if enemy.enemy.note.include?("飛行")
      draw_text(60, 0, 300, 24, "潛行") if enemy.enemy.note.include?("潛行")
      draw_text(60, 0, 300, 24, "劍") if enemy.enemy.note.include?("劍")
      draw_text(60, 0, 300, 24, "槍") if enemy.enemy.note.include?("槍")
      draw_text(60, 0, 300, 24, "術") if enemy.enemy.note.include?("術")
      draw_text(60, 0, 300, 24, "戰") if enemy.enemy.note.include?("戰")
      draw_text(60, 0, 300, 24, "弓") if enemy.enemy.note.include?("弓")
      draw_actor_icons(enemy, 200, 0, 192)
      draw_actor_hp(enemy, 440, 0, 70)
      draw_actor_mp(enemy, 520, 0, 70)
  end
end
#==============================================================================
# ■ Window_BattleActor Cursor
#==============================================================================
class Window_BattleActor_Cursor < Window_Selectable_Battle_Cursor
 
  include MOG_BATTLE_CURSOR
  #--------------------------------------------------------------------------
  # ● Initialize
  #--------------------------------------------------------------------------
  def initialize
      super(-32, -32, 32, 32)
      self.index = 0
      self.visible = true
      set_cursor_position(self.index) 
  end  
 
  #--------------------------------------------------------------------------
  # ● Item Max
  #--------------------------------------------------------------------------
  def item_max
      $game_party.members.size
  end  
  #--------------------------------------------------------------------------
  # ● Set Cursor Position
  #--------------------------------------------------------------------------    
  def set_cursor_position(index)
      check_index_limit(index)
      return if $game_party.members[self.index] == nil
      screen_x = $game_party.members[self.index].screen_x rescue nil
      screen_y = $game_party.members[self.index].screen_y rescue nil
      if screen_x == nil or screen_y == nil
        screen_x = 480 + index*15
        screen_y = 150 + index*50
      end  
      $game_temp.battle_cursor[0] = screen_x + CURSOR_POSITION[0]
      $game_temp.battle_cursor[1] = screen_y + CURSOR_POSITION[1]
      $game_temp.battle_cursor[3] = $game_party.members[self.index].name
  end
 
end
 
$mog_rgss3_battle_hud = true