﻿#==============================================================================
# ★ ExMap_AutoPlane
#------------------------------------------------------------------------------
# 　設定雲、霧等顯示在特定地圖的腳本素材。
#==============================================================================

# 地圖設定。
# 請按照 地圖ID、檔案名、橫向回圈、縱向回圈、Z 座標、
# 不透明度（0 ～ 255）、合成方式 (0:通常 1:加算 2:減算）的順序填寫
# 例如) 地圖ID為、將圖片「fog.png」半透明並且顯示在一般圖片下面
# 　　向左5向下2的速度回圈，那麼就填寫 => [5, "fog", 5, -2, 50, 128, 0]
EXMAP_ATPLN_MAPS = [
  [0, "", 0, 0, 50, 0, 0],
  [0, "", 0, 0, 50, 0, 0],
]
   
# 設定檔目錄。
# 指定霧圖形檔位置 (Graphic/xxx/) 
# 0:System 1:Parallaxes 2:Pictures 3:Animations
EXMAP_ATPLN_FOLDER = 1

#------------------------------------------------------------------------------

class Game_Map
  alias _exmapln_setup setup
  alias _exmapln_update update
  alias _exmapln_set_display_pos set_display_pos
  alias _exmapln_scroll_down scroll_down
  alias _exmapln_scroll_left scroll_left
  alias _exmapln_scroll_right scroll_right
  alias _exmapln_scroll_up scroll_up
  #--------------------------------------------------------------------------
  # ○ 公開インスタンス変數 (追加定義)
  #--------------------------------------------------------------------------
  attr_reader   :fog                      # フォグデータ
  #--------------------------------------------------------------------------
  # ○ セットアップ (追加定義)
  #     map_id : マップ ID
  #--------------------------------------------------------------------------
  def setup(map_id)
    _exmapln_setup(map_id)
    setup_fog
  end
  #--------------------------------------------------------------------------
  # ☆ フォグのセットアップ
  #--------------------------------------------------------------------------
  def setup_fog
    @fog = [0, "", 0, 0, 50, 0, 0]
    @fog_sx = 0
    @fog_sy = 0
    @fog_x = 0
    @fog_y = 0
    for data in EXMAP_ATPLN_MAPS
      if @map_id == data[0]
        @fog = data
        @fog_sx = data[2]
        @fog_sy = data[3]
        break
      end
    end
  end
  #--------------------------------------------------------------------------
  # ○ フレーム更新 (追加定義)
  #--------------------------------------------------------------------------
  def update
    _exmapln_update
    update_fog
  end
  #--------------------------------------------------------------------------
  # ○ 表示位置の設定 (追加定義)
  #     x : 新しい表示 X 座標 (*256)
  #     y : 新しい表示 Y 座標 (*256)
  #--------------------------------------------------------------------------
  def set_display_pos(x, y)
    _exmapln_set_display_pos(x, y)
    @fog_x = x
    @fog_y = y
  end
  #--------------------------------------------------------------------------
  # ☆ フォグ表示 X 座標の計算
  #     bitmap : フォグビットマップ
  #--------------------------------------------------------------------------
  def calc_fog_x(bitmap)
    return bitmap == nil ? 0 : @fog_x / 8
  end
  #--------------------------------------------------------------------------
  # ☆ フォグ表示 Y 座標の計算
  #     bitmap : フォグビットマップ
  #--------------------------------------------------------------------------
  def calc_fog_y(bitmap)
    return bitmap == nil ? 0 : @fog_y / 8
  end
  #--------------------------------------------------------------------------
  # ○ 下にスクロール (追加定義)
  #     distance : スクロールする距離
  #--------------------------------------------------------------------------
  def scroll_down(distance)
    last_y = @display_y
    _exmapln_scroll_down(distance)
    if loop_vertical?
      @fog_y += distance
    else
      @fog_y += @display_y - last_y
    end
  end
  #--------------------------------------------------------------------------
  # ○ 左にスクロール (追加定義)
  #     distance : スクロールする距離
  #--------------------------------------------------------------------------
  def scroll_left(distance)
    last_x = @display_x
    _exmapln_scroll_left(distance)
    if loop_horizontal?
      @fog_x -= distance
    else
      @fog_x += @display_x - last_x
    end
  end
  #--------------------------------------------------------------------------
  # ○ 右にスクロール (追加定義)
  #     distance : スクロールする距離
  #--------------------------------------------------------------------------
  def scroll_right(distance)
    last_x = @display_x
    _exmapln_scroll_right(distance)
    if loop_horizontal?
      @fog_x += distance
    else
      @fog_x += @display_x - last_x
    end
  end
  #--------------------------------------------------------------------------
  # ○ 上にスクロール (追加定義)
  #     distance : スクロールする距離
  #--------------------------------------------------------------------------
  def scroll_up(distance)
    last_y = @display_y
    _exmapln_scroll_up(distance)
    if @fog != nil
      if loop_vertical?
        @fog_y -= distance
      else
        @fog_y += @display_y - last_y
      end
    end
  end
  #--------------------------------------------------------------------------
  # ☆ フォグの更新
  #--------------------------------------------------------------------------
  def update_fog
    @fog_x += @fog_sx * 2
    @fog_y += @fog_sy * 2
 end
end

class Spriteset_Map
  alias _exmapln_initialize initialize
  alias _exmapln_dispose dispose
  alias _exmapln_update update
  #--------------------------------------------------------------------------
  # ○ オブジェクト初期化 (追加定義)
  #--------------------------------------------------------------------------
  def initialize
    create_fog
    _exmapln_initialize
  end
  #--------------------------------------------------------------------------
  # ☆ フォグの作成
  #--------------------------------------------------------------------------
  def create_fog
    @fog = Plane.new
    if $game_map.fog != nil
      @fog.z = $game_map.fog[4]
      @fog.opacity = $game_map.fog[5]
      @fog.blend_type = $game_map.fog[6]
    end
  end
  #--------------------------------------------------------------------------
  # ○ 解放 (追加定義)
  #--------------------------------------------------------------------------
  def dispose
    _exmapln_dispose
    @fog.dispose
  end
  #--------------------------------------------------------------------------
  # ○ フレーム更新 (追加定義)
  #--------------------------------------------------------------------------
  def update
    _exmapln_update
    update_fog
  end
  #--------------------------------------------------------------------------
  # ☆ フォグの更新
  #--------------------------------------------------------------------------
  def update_fog
    if @fog_name != $game_map.fog[1]
      @fog_name = $game_map.fog[1]
      if @fog.bitmap != nil
        @fog.bitmap.dispose
        @fog.bitmap = nil
      end
      if @fog_name != ""
        case EXMAP_ATPLN_FOLDER
        when 0
          @fog.bitmap = Cache.system(@fog_name)
        when 1
          @fog.bitmap = Cache.parallax(@fog_name)
        when 2
          @fog.bitmap = Cache.picture(@fog_name)
        when 3
          @fog.bitmap = Cache.animation(@fog_name, 0)
        end
      end
      Graphics.frame_reset
    end
    @fog.ox = $game_map.calc_fog_x(@fog.bitmap)
    @fog.oy = $game_map.calc_fog_y(@fog.bitmap)
  end
end
