#==============================================================================
# ** TDS On-Map Party Switching
#    Ver: 1.0
#------------------------------------------------------------------------------
#  * Description:
#  This script lets you switch party members while on the map by pressing
#  predefined keys.
#------------------------------------------------------------------------------
#  * Features: 
#  Switching party members on the map.
#------------------------------------------------------------------------------
#  * Instructions:
#  Just put it in your game under the "Materials" area of the script list and
#  enjoy.
#
#  To switch party members press the Q or W keys, unless you wish to change them
#  in the settings area below.
#------------------------------------------------------------------------------
#  * Notes:
#  None.
#------------------------------------------------------------------------------
# WARNING:
#
# Do not release, distribute or change my work without my expressed written 
# consent, doing so violates the terms of use of this work.
#
# If you really want to share my work please just post a link to the original
# site.
#
# * Not Knowing English or understanding these terms will not excuse you in any
#   way from the consequenses.
#==============================================================================
# * Import to Global Hash *
#==============================================================================
($imported ||= {})[:TDS_On_Map_Party_Switching] = true

#==============================================================================
# ** TDS
#------------------------------------------------------------------------------
#  A module containing TDS data structures, mostly script settings.
#==============================================================================

module TDS
  #============================================================================
  # ** On_Map_Party_Switching_Settings
  #----------------------------------------------------------------------------
  #  This Module contains On-Map Party Switching Settings.
  #============================================================================  
  module On_Map_Party_Switching_Settings
    #--------------------------------------------------------------------------
    # * Constants (Settings)
    #--------------------------------------------------------------------------        
    # Party Switching Keys (Right, Left)
    Switching_Keys = [:R, :L]
    # If True it will rotate through all party members instead of the visible ones.
    All_Members_Switch = true
    # Switching Sound (Name, Volume, Pitch) (Leave Name Empty "" for no sound)
    Switching_Sound = RPG::SE.new("Decision1", 100, 100)
  end
end


#==============================================================================
# ** Game_Party
#------------------------------------------------------------------------------
#  This class handles parties. Information such as gold and items is included.
# Instances of this class are referenced by $game_party.
#==============================================================================

class Game_Party < Game_Unit
  #--------------------------------------------------------------------------
  # * Rotate Party Members
  #--------------------------------------------------------------------------
  def rotate_party_members(dir, all = true)
    # If Rotation of all party members
    if all
      # Rotate Actors Array
      @actors.rotate!(dir) 
    else
      # Old Party
      party = @actors.drop(max_battle_members)      
      # New Party
      new_party = @actors.take(max_battle_members)
      # Rotate new Party
      new_party.rotate!(dir)
      # Set Actors Array
      @actors = new_party + party
    end
  end
end


#==============================================================================
# ** Scene_Map
#------------------------------------------------------------------------------
#  This class performs the map screen processing.
#==============================================================================

class Scene_Map < Scene_Base
  #--------------------------------------------------------------------------
  # * Alias Listing
  #--------------------------------------------------------------------------
  alias tds_on_map_party_switching_scene_map_update                   update
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update(*args, &block)
    # Run Original Method
    tds_on_map_party_switching_scene_map_update(*args, &block)
    # Update Party Switching Input
    update_party_switching_input 
  end
  #--------------------------------------------------------------------------
  # * Update Party Switching Input
  #--------------------------------------------------------------------------
  def update_party_switching_input
    # Return if interpreter is running or Game Player is not movable
    return if $game_map.interpreter.running? or !$game_player.movable?
    # Go Through Input
    TDS::On_Map_Party_Switching_Settings::Switching_Keys.each {|input|
      # If Input Trigger
      if Input.trigger?(input)      
        # Play Switching Sound
        TDS::On_Map_Party_Switching_Settings::Switching_Sound.play
        # All Party Switch Flag
        all = TDS::On_Map_Party_Switching_Settings::All_Members_Switch
        # Rotate Party Members
        $game_party.rotate_party_members(1, all)  if input == TDS::On_Map_Party_Switching_Settings::Switching_Keys.at(0)
        $game_party.rotate_party_members(-1, all) if input == TDS::On_Map_Party_Switching_Settings::Switching_Keys.at(1)
        # Refresh Game Player
        $game_player.refresh
        break
      end      
    }    
  end  
end