#==============================================================================
# +++ MOG - Battle Cursor (2.2) +++
#==============================================================================
# By Moghunter 
# [url]http://www.atelier-rgss.com/[/url]
#==============================================================================
# Sistema de cursor animado de batalha nos sprites dos battlers.
#==============================================================================
# Arquivo necessário. (Graphics/System)
# 
# Battle_Cursor.png
# Battle_Cursor2.png *(necessário o script Scope EX)
#
#==============================================================================
# NOTA - Deixe este script abaixo do script Battler Motion.
#==============================================================================
# ● Histórico (Version History)
#==============================================================================
# (2.2) Melhoria no sistema de posição da origem Z.
#==============================================================================
 
#==============================================================================
# ■ CURSOR SETTING
#==============================================================================
module MOG_BATTLE_CURSOR
  #Definição da posição do cursor em relação ao alvo.
  CURSOR_POSITION = [10, -30]
  #Definição da posição do nome do alvo.
  CURSOR_NAME_POSITION = [10, 20]
  #Ativar efeito deslizar.
  CURSOR_SLIDE_EFFECT = true
  #Ativar animação de levitação.
  CURSOR_FLOAT_EFFECT = false
  #Definição da prioridade do cursor.
  CURSOR_Z = 2000
  #Posição do cursor Secundário.
  TARGET_CURSOR_2_POS = [0, -48]
  #Ativar animação de zoom.
  TARGET_CURSOR_2_ZOOM_EFFECT = true
end
 
$imported = {} if $imported.nil?
$imported[:mog_battle_cursor] = true
 
#==============================================================================
# ■ Game Temp
#==============================================================================
class Game_Temp 
 
  attr_accessor :battle_cursor
  attr_accessor :battle_cursor_data
  attr_accessor :target_skill
 
  #--------------------------------------------------------------------------
  # ● Initialize
  #--------------------------------------------------------------------------  
  alias mog_battle_cursor_initialize initialize
  def initialize
      @battle_cursor = [0,0,false,""] ; @target_skill = []
      mog_battle_cursor_initialize
  end  
 
  #--------------------------------------------------------------------------
  # ● Clear Target Temp
  #--------------------------------------------------------------------------    
  def clear_target_temp
      $game_party.members.each {|t| t.target_temp = false } rescue nil
      $game_troop.members.each {|t| t.target_temp = false } rescue nil      
  end
 
end
 
#==============================================================================
# ■ Spriteset Battle Cursor
#==============================================================================
class Sprite_Battle_Cursor < Sprite
  include MOG_BATTLE_CURSOR
 
  #--------------------------------------------------------------------------
  # ● Initialize
  #--------------------------------------------------------------------------        
  def initialize(viewport = nil)
      super(viewport) 
      $game_temp.battle_cursor = [0,0,false,""] 
      self.bitmap = Cache.system("Battle_Cursor")
      self.visible = $game_temp.battle_cursor[2] ; self.z = CURSOR_Z
      @cursor_name = Sprite.new ; @cursor_name.bitmap = Bitmap.new(120,32)
      @cursor_name.z = self.z + 1 ; @cursor_name.bitmap.font.size = 16
      @cursor_name_enemy = $game_temp.battle_cursor[3]
      @cursor_name_position = [CURSOR_NAME_POSITION[0] ,CURSOR_NAME_POSITION[1]]
      @cursor_float = [0,0] ;  refresh_cursor_name
  end
 
  #--------------------------------------------------------------------------
  # ● Dispose Sprite
  #--------------------------------------------------------------------------          
  def dispose
      super
      dispose_sprite_cursor
  end
 
  #--------------------------------------------------------------------------
  # ● Dispose Sprite Cursor
  #--------------------------------------------------------------------------            
  def dispose_sprite_cursor
      @cursor_name.bitmap.dispose ; @cursor_name.dispose
      self.bitmap.dispose ; $game_temp.clear_target_temp
  end
 
  #--------------------------------------------------------------------------
  # ● Refresh Cursor Name
  #--------------------------------------------------------------------------              
  def refresh_cursor_name
      @cursor_name_enemy = $game_temp.battle_cursor[3]
      @cursor_name.bitmap.clear
      @cursor_name.bitmap.draw_text(0,0,120,32,@cursor_name_enemy.to_s,1)
  end 
 
  #--------------------------------------------------------------------------
  # ● Update
  #--------------------------------------------------------------------------            
  def update
      super
      update_sprite_cursor
  end
 
  #--------------------------------------------------------------------------
  # ● Update Sprite Cursor
  #--------------------------------------------------------------------------              
  def update_sprite_cursor
      update_visible ; update_cursor_float_effect
      execute_move(0,self.x,$game_temp.battle_cursor[0])
      execute_move(1,self.y,$game_temp.battle_cursor[1] + @cursor_float[1])
      update_sprite_name
  end
 
  #--------------------------------------------------------------------------
  # ● Update Visible
  #--------------------------------------------------------------------------                
  def update_visible
      self.visible = $game_temp.battle_cursor[2]
      (self.x = -64 ; self.y = -64) if !self.visible
  end  
 
  #--------------------------------------------------------------------------
  # ● Update Sprite Name
  #--------------------------------------------------------------------------                
  def update_sprite_name
      return if @cursor_name == nil
      refresh_cursor_name  if @cursor_name_enemy != $game_temp.battle_cursor[3]
      @cursor_name.x = self.x + @cursor_name_position[0]
      @cursor_name.y = self.y + @cursor_name_position[1]
      @cursor_name.opacity = self.opacity ; @cursor_name.visible = self.visible
  end  
 
  #--------------------------------------------------------------------------
  # ● Update Cursor Float Effect
  #--------------------------------------------------------------------------              
  def update_cursor_float_effect
      return if !CURSOR_FLOAT_EFFECT
      @cursor_float[0] += 1
      case @cursor_float[0]
        when 0..20  ; @cursor_float[1] += 1
        when 21..40 ; @cursor_float[1] -= 1
        else
          @cursor_float[0] = 0 ;   @cursor_float[1] = 0
      end        
  end  
 
  #--------------------------------------------------------------------------
  # ● Execute Move
  #--------------------------------------------------------------------------      
  def execute_move(type,cp,np)
      sp = 5 + ((cp - np).abs / 5)
      if cp > np 
         cp -= sp ; cp = np if cp < np
      elsif cp < np 
         cp += sp ; cp = np if cp > np
      end     
      self.x = cp if type == 0 ; self.y = cp if type == 1
  end      
 
end
 
#==============================================================================
# ■ Spriteset Battle
#==============================================================================
class Spriteset_Battle
 
  #--------------------------------------------------------------------------
  # ● Initialize
  #--------------------------------------------------------------------------      
  alias mog_battle_cursor_initialize initialize
  def initialize
      mog_battle_cursor_initialize
      create_cursor
  end
 
  #--------------------------------------------------------------------------
  # ● Dispose
  #--------------------------------------------------------------------------      
  alias mog_battle_cursor_dispose dispose
  def dispose
      mog_battle_cursor_dispose
      dispose_cursor
  end 
 
  #--------------------------------------------------------------------------
  # ● Update
  #--------------------------------------------------------------------------          
  alias mog_battle_cursor_update update
  def update
      mog_battle_cursor_update
      update_battle_cursor
  end  
 
  #--------------------------------------------------------------------------
  # ● Create_Cursor
  #--------------------------------------------------------------------------        
  def create_cursor
      return if @battle_cursor != nil
      $game_temp.clear_target_temp ; @battle_cursor = Sprite_Battle_Cursor.new      
  end 
 
  #--------------------------------------------------------------------------
  # ● Dispose Cursor
  #--------------------------------------------------------------------------        
  def dispose_cursor
      return if @battle_cursor == nil
      @battle_cursor.dispose
  end  
 
  #--------------------------------------------------------------------------
  # ● Update Battle Cursor
  #--------------------------------------------------------------------------          
  def update_battle_cursor
      return if @battle_cursor == nil
      @battle_cursor.update      
  end
 
end
 
#==============================================================================
# ■ Battle Cursor Index
#==============================================================================
module Battle_Cursor_index
 
  include MOG_BATTLE_CURSOR
 
  #--------------------------------------------------------------------------
  # ● Check Index Limit
  #--------------------------------------------------------------------------      
  def check_index_limit
      self.index = 0 if self.index >= item_max
      self.index = (item_max - 1) if self.index < 0
  end      
 
  #--------------------------------------------------------------------------
  # ● Set Cursor Position Enemy
  #--------------------------------------------------------------------------    
  def set_cursor_position_enemy
      return if !self.active
      @fcursor = nil
      $game_temp.battle_cursor[0] = $game_troop.alive_members[self.index].screen_x + CURSOR_POSITION[0] rescue nil
      $game_temp.battle_cursor[1] = $game_troop.alive_members[self.index].screen_y + CURSOR_POSITION[1] rescue nil
      $game_temp.battle_cursor[3] = $game_troop.alive_members[self.index].name rescue nil
      $game_temp.battle_cursor_data = $game_troop.alive_members[self.index]
      $game_temp.battle_cursor = [0,0,false,0] if $game_temp.battle_cursor[0] == nil
      enable_target_sprites($game_temp.battle_cursor_data.index)
  end
 
  #--------------------------------------------------------------------------
  # ● Enable Target Sprites
  #--------------------------------------------------------------------------    
  def enable_target_sprites(target_index)  
      return if $game_temp.target_skill.empty?
      $game_temp.clear_target_temp ; battler = $game_temp.target_skill[1]
      type = $game_temp.target_skill[0].is_a?(RPG::Skill) ? 0 : 1
      target_index = $game_temp.battle_cursor_data.index
      battler.make_action_temp($game_temp.target_skill[0].id, target_index,type)      
      targets = battler.action_temp.make_targets.compact
      targets.each {|t| t.target_temp = true }
  end  
 
  #--------------------------------------------------------------------------
  # ● Set Cursor Position Actor
  #--------------------------------------------------------------------------    
  def set_cursor_position_actor
      return if !self.active      
      @fcursor = nil
      $game_temp.battle_cursor[0] = $game_party.members[self.index].screen_x + CURSOR_POSITION[0] rescue nil
      $game_temp.battle_cursor[1] = $game_party.members[self.index].screen_y + CURSOR_POSITION[1] rescue nil
      $game_temp.battle_cursor[3] = $game_party.members[self.index].name rescue nil
      $game_temp.battle_cursor_data = $game_party.members[self.index]
      $game_temp.battle_cursor = [0,0,false,0] if $game_temp.battle_cursor[0] == nil
      enable_target_sprites(self.index)
  end  
 
  #--------------------------------------------------------------------------
  # ● Process Cursor Move
  #--------------------------------------------------------------------------
  def process_cursor_move
      $game_temp.battle_cursor_data = nil
      return unless cursor_movable?
      last_index = @index
      cursor_move_index(1) if Input.repeat?(:DOWN)
      cursor_move_index(-1) if Input.repeat?(:UP)
      cursor_move_index(1) if Input.repeat?(:RIGHT)
      cursor_move_index(-1) if Input.repeat?(:LEFT)
      $game_temp.battle_cursor_data = $game_troop.alive_members[@index]
      Sound.play_cursor if @index != last_index
  end
 
  #--------------------------------------------------------------------------
  # ● Process Cursor Move Index
  #--------------------------------------------------------------------------  
  def cursor_move_index(value = 0)
      self.index += value ;  check_index_limit
  end
 
end
 
#==============================================================================
# ■ Window_BattleActor
#==============================================================================
class Window_BattleActor < Window_BattleStatus
  include Battle_Cursor_index
 
  #--------------------------------------------------------------------------
  # ● Update
  #--------------------------------------------------------------------------  
  alias mog_battle_cursor_actor_update update
  def update
      pre_index = self.index
      mog_battle_cursor_actor_update
      set_cursor_position_actor if pre_index != self.index or @fcursor
  end  
 
  #--------------------------------------------------------------------------
  # ● Show
  #--------------------------------------------------------------------------
  alias mog_battle_cursor_show show
  def show
      if @info_viewport
         set_cursor_position_actor ; $game_temp.battle_cursor[2] = true
         @fcursor = true
      end
      mog_battle_cursor_show
  end
 
  #--------------------------------------------------------------------------
  # ● Hide
  #--------------------------------------------------------------------------
  alias mog_battle_cursor_hide hide
  def hide
      $game_temp.battle_cursor[2] = false if @info_viewport
      $game_temp.clear_target_temp ; $game_temp.target_skill.clear
      mog_battle_cursor_hide
  end  
 
end
 
#==============================================================================
# ■ Game Enemy
#==============================================================================
class Game_Enemy < Game_Battler
  attr_accessor :index 
end
 
#==============================================================================
# ■ Game Troop
#==============================================================================
class Game_Troop < Game_Unit
 
  #--------------------------------------------------------------------------
  # * Setup
  #--------------------------------------------------------------------------
  alias mog_battle_cursor_setup setup
  def setup(troop_id)
      mog_battle_cursor_setup(troop_id)
      if can_sort_index?
         @enemies.sort! {|a,b| a.screen_x <=> b.screen_x}
         @enemies.each_with_index {|e, i| e.index = i }
      end
  end
 
  #--------------------------------------------------------------------------
  # * Can Sort Index?
  #--------------------------------------------------------------------------
  def can_sort_index?
      return false if @enemies.size < 3
      return false if troop.pages.size > 1
      return false if !troop.pages[0].list[0].parameters.empty? rescue false
      return true
  end
 
end
 
#==============================================================================
# ■ Window_BattleEnemy
#==============================================================================
class Window_BattleEnemy < Window_Selectable
  include Battle_Cursor_index
 
  #--------------------------------------------------------------------------
  # ● Update
  #--------------------------------------------------------------------------  
  alias mog_battle_cursor_enemy_update update
  def update
      pre_index = self.index 
      mog_battle_cursor_enemy_update
      set_cursor_position_enemy if pre_index != self.index or @fcursor
  end
 
  #--------------------------------------------------------------------------
  # ● Show
  #--------------------------------------------------------------------------
  alias mog_battle_cursor_show show
  def show
      if @info_viewport
         set_cursor_position_enemy ; $game_temp.battle_cursor[2] = true
         @fcursor = true
      end
      mog_battle_cursor_show
  end
 
  #--------------------------------------------------------------------------
  # ● Hide
  #--------------------------------------------------------------------------
  alias mog_battle_cursor_hide hide
  def hide
      $game_temp.battle_cursor[2] = false if @info_viewport
      $game_temp.clear_target_temp ; $game_temp.target_skill.clear
      mog_battle_cursor_hide
  end  
 
end
 
#==============================================================================
# ■ Window Selectable
#==============================================================================
class Window_Selectable < Window_Base  
 
  #--------------------------------------------------------------------------
  # * Process OK
  #-------------------------------------------------------------------------
  alias mog_target_cursor_process_ok process_ok
  def process_ok
      mog_target_cursor_process_ok
      execute_target_selection if can_execute_target_selection?
  end
 
  #--------------------------------------------------------------------------
  # * Can Execute Target Selection
  #-------------------------------------------------------------------------
  def can_execute_target_selection?
      return false if !SceneManager.scene_is?(Scene_Battle)
      return false if !current_item_enabled?
      return true if self.is_a?(Window_BattleSkill)
      return true if self.is_a?(Window_BattleItem)
      return true if self.is_a?(Window_ActorCommand) and self.index == 0
      return false
  end
 
  #--------------------------------------------------------------------------
  # * Execute Target Selection
  #-------------------------------------------------------------------------
  def execute_target_selection
      return if BattleManager.actor == nil 
      $game_temp.target_skill.clear
      item = @data[index] rescue nil
      item = $data_skills[BattleManager.actor.attack_skill_id] rescue nil if self.is_a?(Window_ActorCommand)
      return if item == nil 
      if [2,8,10].include?(item.scope) or item.note =~ /<Scope Range = (\d+) - (\d+) - (\d+) - (\d+)>/   
         $game_temp.target_skill = [item,BattleManager.actor]
      end   
  end  
 
end
 
#==============================================================================
# ■ Game Battler
#==============================================================================
class Game_Battler < Game_BattlerBase
 
  attr_accessor :target_temp
  attr_accessor :action_temp
 
  #--------------------------------------------------------------------------
  # * Initialize
  #--------------------------------------------------------------------------
  alias mog_target_cursor_initialize initialize
  def initialize
      @target_cursor_temp = false
      mog_target_cursor_initialize
  end
 
  #--------------------------------------------------------------------------
  # ● Make Action Temp
  #--------------------------------------------------------------------------
  def make_action_temp(action_id,target_index,type)
      return if action_id == nil
      action = Game_Action.new(self, true)
      action.set_skill(action_id) if type == 0
      action.set_item(action_id) if type == 1
      action.target_index = target_index
      @action_temp = action
  end     
 
end
 
#==============================================================================
# ■ Game BattlerBase
#==============================================================================
class Game_BattlerBase
   attr_accessor :hidden
end
 
#==============================================================================
# ■ Scene Battler
#==============================================================================
class Scene_Battle < Scene_Base 
 
  #--------------------------------------------------------------------------
  # * On Skill OK
  #--------------------------------------------------------------------------
  def on_skill_ok
      [url=home.php?mod=space&uid=260100]@skill[/url] = @skill_window.item
      BattleManager.actor.input.set_skill(@skill.id)
      BattleManager.actor.last_skill.object = @skill
      if [0,11].include?(@skill.scope)
         @skill_window.hide ; next_command
      elsif @skill.for_opponent?
         select_enemy_selection
      else
         select_actor_selection
      end
  end
 
  #--------------------------------------------------------------------------
  # * On Item OK
  #--------------------------------------------------------------------------
  def on_item_ok
      @item = @item_window.item
      BattleManager.actor.input.set_item(@item.id)
      if [0,11].include?(@item.scope)
         @item_window.hide ; next_command
      elsif @item.for_opponent?
         select_enemy_selection
      else
         select_actor_selection
      end
      $game_party.last_item.object = @item
  end
 
end
 
#==============================================================================
# ■ Sprite Battler
#==============================================================================
class Sprite_Battler < Sprite_Base
  include MOG_BATTLE_CURSOR
  #--------------------------------------------------------------------------
  # * Initialize
  #--------------------------------------------------------------------------
  alias mog_target_cursor_initialize initialize
  def initialize(viewport, battler = nil)
      mog_target_cursor_initialize(viewport, battler)
      create_target_cursor
  end 
 
  #--------------------------------------------------------------------------
  # * Create Target Cursor
  #--------------------------------------------------------------------------
  def create_target_cursor
      @target_cursor = Sprite.new
      @target_cursor.bitmap = Cache.system("Battle_Cursor2")
      @target_cursor.ox = @target_cursor.bitmap.width / 2
      @target_cursor.oy = @target_cursor.bitmap.height / 2
      @target_cursor.visible = false
      @target_visible_old = @target_cursor.visible
      @tc_pos = [TARGET_CURSOR_2_POS[0],TARGET_CURSOR_2_POS[1] + @target_cursor.oy]
  end
 
  #--------------------------------------------------------------------------
  # * Dispose
  #--------------------------------------------------------------------------
  alias mog_target_cursor_dispose dispose
  def dispose
      dispose_target_cursor
      mog_target_cursor_dispose
  end
 
  #--------------------------------------------------------------------------
  # * Dispose Target Cursor
  #--------------------------------------------------------------------------
  def dispose_target_cursor
      return if @target_cursor == nil
      @target_cursor.bitmap.dispose ; @target_cursor.dispose
  end
 
  #--------------------------------------------------------------------------
  # * Update Postion
  #--------------------------------------------------------------------------
  alias mog_target_cursor_update_position update_position
  def update_position
      mog_target_cursor_update_position
      update_target_cursor
  end
 
  #--------------------------------------------------------------------------
  # * Update Target Cursor
  #--------------------------------------------------------------------------
  def update_target_cursor
      return if @target_cursor == nil
      refresh_zoom if @target_visible_old != @target_cursor.visible
      @target_cursor.visible = can_target_cursor_visible?
      return if !@target_cursor.visible
      @target_cursor.x = self.x + @tc_pos[0]
      @target_cursor.y = self.y + @tc_pos[1]
      @target_cursor.z = CURSOR_Z + 1
      @target_cursor.opacity = 130 + rand(125)
      @target_cursor.zoom_x -= 0.05 if @target_cursor.zoom_x > 1.00
      @target_cursor.zoom_x = 1.00 if @target_cursor.zoom_x < 1.00
      @target_cursor.zoom_y = @target_cursor.zoom_x            
  end
 
  #--------------------------------------------------------------------------
  # * Refresh Zoom
  #--------------------------------------------------------------------------
  def refresh_zoom
      @target_visible_old = @target_cursor.visible
      @target_cursor.zoom_x = 1.50 if TARGET_CURSOR_2_ZOOM_EFFECT
      @target_cursor.zoom_y = @target_cursor.zoom_x
  end
 
  #--------------------------------------------------------------------------
  # * Can Target Cursor Visible?
  #--------------------------------------------------------------------------
  def can_target_cursor_visible?
      return false if @battler.dead?
      return false if @battler.hidden
      return false if !@battler.target_temp
      return true
  end
 
end