#==============================================================================
#  Scene_Map
#------------------------------------------------------------------------------
# @}bvʂ̏sNXłB
#==============================================================================

class Scene_Map < Scene_Base
  #--------------------------------------------------------------------------
  #  Jn
  #--------------------------------------------------------------------------
  def start
    super
    $game_map.refresh
    @spriteset = Spriteset_Map.new
    @message_window = Window_Message.new
  end
  #--------------------------------------------------------------------------
  #  gWVs
  #--------------------------------------------------------------------------
  def perform_transition
    if Graphics.brightness == 0       # 퓬A[hȂ
      fadein(30)
    else                              # j[̕AȂ
      Graphics.transition(15)
    end
  end
  #--------------------------------------------------------------------------
  #  I
  #--------------------------------------------------------------------------
  def terminate
    super
    if $scene.is_a?(Scene_Battle)     # ogʂɐ؂ւ̏ꍇ
      @spriteset.dispose_characters   # wi쐬̂߂ɃLB
    end
    snapshot_for_background
    @spriteset.dispose
    @message_window.dispose
    if $scene.is_a?(Scene_Battle)     # ogʂɐ؂ւ̏ꍇ
      perform_battle_transition       # 퓬OgWVs
    end
  end
  #--------------------------------------------------------------------------
  #  {XV
  #--------------------------------------------------------------------------
  def update_basic
    Graphics.update                   # Q[ʂXV
    Input.update                      # ͏XV
    $game_map.update                  # }bvXV
    @spriteset.update                 # XvCgZbgXV
  end
  #--------------------------------------------------------------------------
  #  t[XV
  #--------------------------------------------------------------------------
  def update
    super
    $game_map.interpreter.update      # C^v^XV
    $game_map.update                  # }bvXV
    $game_player.update               # vC[XV
    $game_system.update               # ^C}[XV
    @spriteset.update                 # XvCgZbgXV
    @message_window.update            # bZ[WEBhEXV
    unless $game_message.visible      # bZ[W\ȊO
      update_transfer_player
      update_encounter
      update_call_menu
      update_call_debug
      update_scene_change
    end
  end
  #--------------------------------------------------------------------------
  #  ʂ̃tF[hC
  #     duration : 
  #    }bvʂł́AGraphics.fadeout 𒼐ڎgƓVGtFNg≓ĩX
  #  N[Ȃǂ~܂Ȃǂ̕ss邽߁AIɃtF[hCsB
  #--------------------------------------------------------------------------
  def fadein(duration)
    Graphics.transition(0)
    for i in 0..duration-1
      Graphics.brightness = 255 * i / duration
      update_basic
    end
    Graphics.brightness = 255
  end
  #--------------------------------------------------------------------------
  #  ʂ̃tF[hAEg
  #     duration : 
  #    L̃tF[hCƓAGraphics.fadein ͒ڎgȂB
  #--------------------------------------------------------------------------
  def fadeout(duration)
    Graphics.transition(0)
    for i in 0..duration-1
      Graphics.brightness = 255 - 255 * i / duration
      update_basic
    end
    Graphics.brightness = 0
  end
  #--------------------------------------------------------------------------
  #  ꏊړ̏
  #--------------------------------------------------------------------------
  def update_transfer_player
    return unless $game_player.transfer?
    fade = (Graphics.brightness > 0)
    fadeout(30) if fade
    @spriteset.dispose              # XvCgZbg
    $game_player.perform_transfer   # ꏊړ̎s
    #T14-022 ADD--->
    autoplay_enabled = Ex.get_env( Ex::ENV_TRANSFER_AUTOPLAY )
    if autoplay_enabled == true
    #<--------------
      $game_map.autoplay            # BGM  BGS ̎؂ւ
    #T14-022 ADD--->
    end
    #<--------------
    $game_map.update
    Graphics.wait(15)
    @spriteset = Spriteset_Map.new  # XvCgZbgč쐬
    fadein(30) if fade
    Input.update
  end
  #--------------------------------------------------------------------------
  #  GJEg̏
  #--------------------------------------------------------------------------
  def update_encounter
    return if $game_player.encounter_count > 0        # H
    return if $game_map.interpreter.running?          # CxgsH
    return if $game_system.encounter_disabled         # GJEg֎~H
    troop_id = $game_player.make_encounter_troop_id   # GO[v
    return if $data_troops[troop_id] == nil           # GO[vH
    $game_troop.setup(troop_id)
    $game_troop.can_escape = true
    $game_temp.battle_proc = nil
    $game_temp.next_scene = "battle"
    preemptive_or_surprise
  end
  #--------------------------------------------------------------------------
  #  搧Uƕsӑł̊m
  #--------------------------------------------------------------------------
  def preemptive_or_surprise
    actors_agi = $game_party.average_agi
    enemies_agi = $game_troop.average_agi
    if actors_agi >= enemies_agi
      percent_preemptive = 5
      percent_surprise = 3
    else
      percent_preemptive = 3
      percent_surprise = 5
    end
    if rand(100) < percent_preemptive
      $game_troop.preemptive = true
    elsif rand(100) < percent_surprise
      $game_troop.surprise = true
    end
  end
  #--------------------------------------------------------------------------
  #  LZ{^ɂ郁j[Ăяo
  #--------------------------------------------------------------------------
  def update_call_menu
    if Input.trigger?(Input::B)
      return if $game_map.interpreter.running?        # CxgsH
      return if $game_system.menu_disabled            # j[֎~H
      $game_temp.menu_beep = true                     # SE ttOݒ
      $game_temp.next_scene = "menu"
    end
  end
  #--------------------------------------------------------------------------
  #  F9 L[ɂfobOĂяo
  #--------------------------------------------------------------------------
  def update_call_debug
    if $TEST and Input.press?(Input::F9)    # eXgvC F9 L[
      $game_temp.next_scene = "debug"
    end
  end
  #--------------------------------------------------------------------------
  #  ʐ؂ւ̎s
  #--------------------------------------------------------------------------
  def update_scene_change
    return if $game_player.moving?    # vC[̈ړH
    case $game_temp.next_scene
    when "battle"
      call_battle
    when "shop"
      call_shop
    when "name"
      call_name
    when "menu"
      call_menu
    when "save"
      call_save
    when "debug"
      call_debug
    when "gameover"
      call_gameover
    when "title"
      call_title
    else
      $game_temp.next_scene = nil
    end
  end
  #--------------------------------------------------------------------------
  #  ogʂւ̐؂ւ
  #--------------------------------------------------------------------------
  def call_battle
    @spriteset.update
    Graphics.update
    $game_player.make_encounter_count
    $game_player.straighten
    $game_temp.map_bgm = RPG::BGM.last
    $game_temp.map_bgs = RPG::BGS.last
    RPG::BGM.stop
    RPG::BGS.stop
    Sound.play_battle_start
    $game_system.battle_bgm.play
    $game_temp.next_scene = nil
    $scene = Scene_Battle.new
  end
  #--------------------------------------------------------------------------
  #  Vbvʂւ̐؂ւ
  #--------------------------------------------------------------------------
  def call_shop
    $game_temp.next_scene = nil
    $scene = Scene_Shop.new
  end
  #--------------------------------------------------------------------------
  #  O͉ʂւ̐؂ւ
  #--------------------------------------------------------------------------
  def call_name
    $game_temp.next_scene = nil
    $scene = Scene_Name.new
  end
  #--------------------------------------------------------------------------
  #  j[ʂւ̐؂ւ
  #--------------------------------------------------------------------------
  def call_menu
    if $game_temp.menu_beep
      Sound.play_decision
      $game_temp.menu_beep = false
    end
    $game_temp.next_scene = nil
    $scene = Scene_Menu.new
  end
  #--------------------------------------------------------------------------
  #  Z[uʂւ̐؂ւ
  #--------------------------------------------------------------------------
  def call_save
    $game_temp.next_scene = nil
    $scene = Scene_File.new(true, false, true)
  end
  #--------------------------------------------------------------------------
  #  fobOʂւ̐؂ւ
  #--------------------------------------------------------------------------
  def call_debug
    Sound.play_decision
    $game_temp.next_scene = nil
    $scene = Scene_Debug.new
  end
  #--------------------------------------------------------------------------
  #  Q[I[o[ʂւ̐؂ւ
  #--------------------------------------------------------------------------
  def call_gameover
    $game_temp.next_scene = nil
    $scene = Scene_Gameover.new
  end
  #--------------------------------------------------------------------------
  #  ^Cgʂւ̐؂ւ
  #--------------------------------------------------------------------------
  def call_title
    $game_temp.next_scene = nil
    $scene = Scene_Title.new
    fadeout(60)
  end
  #--------------------------------------------------------------------------
  #  퓬OgWVs
  #--------------------------------------------------------------------------
  def perform_battle_transition
    Graphics.transition(80, "Graphics/System/BattleStart", 80)
    Graphics.freeze
  end
end
