﻿#===============================================================
# ● [VX] ◦ Multiple Fog 1.0 ◦ □
#--------------------------------------------------------------
# ◦ by Woratana [woratana@hotmail.com]
# ◦ Thaiware RPG Maker Community
# ◦ Released on: 01/03/2008
#--------------------------------------------------------------
=begin
----------------------------
● How to use fog
----------------------------
Call Script like this:
--------------------------
$fog.id = 1
$fog.name = "fog"
$fog.opacity = 60
$fog.blend = 0
$fog.ox = 0
$fog.oy = 0
$fog.zoom = 100
$fog.tone = [0,0,0,0]
$fog.show
---------------------------
● Description
---------------------------
$fog.id << ID of Fog (If you call fog on same ID, old fog with same ID will be delete)
$fog.name << Fog image file name (It has to be store in folder 'Pictures')
$fog.opacity << Opacity of fog (0 - 255)
$fog.blend << Blend Type of Fog (0: Normal, 1: Add, 2: Subtraction)
$fog.ox << Speed of fog move horizontally
$fog.oy << Speed of fog move vertically
$fog.zoom << Size of Fog (in %)
$fog.tone << Tone Color on Fog [Red, Green, Blue, Gray] put number 0 - 255 in it

$fog.show << USE THIS TO SHOW FOG WITH SETTING YOU JUST SET
(Last setting will be replace with default setting after show that fog)
-----------------------------
● Note
-----------------------------
- THIS IS NOT REAL FOG,so it doesn't has some option that XP has.
- You don't have to set all the options when calling new fog,
if those options are same as default setting.

-----------------------------
● Additional Methods
-----------------------------
These are what you can do with fog by call script :)

◦ Change Fog Opacity
$fog.opacity(fog id, opacity)

◦ Change Fog Blend Type
$fog.blend(fog id, blend type) # blend type >> (0: Normal, 1: Add, 2: Subtraction)

◦ Change Fog Move Speed
$fog.speed(fog id, ox, oy)

◦ Increase/Decrease Fog Move Speed
$fog.speed_plus(fog id, ox_addition, oy_addition)
# You can use negative number to decrease speed/change direction

◦ Change Fog Size
$fog.zoom(fog id, size) # Size is in %, e.g. type 100 for 100%

◦ Change Fog Tone
$fog.tone(fog id, tone) # Type tone like this >> [Red, Green, Blue, Gray]

◦ Delele Fog
$fog.delete(fog id)

◦ Delete All Fogs
$fog.clear

=end
#===============================================================

class Worale_Multiple_Fog
  
  Z_BASE = 200 # Z-Coordinate for Fog (Higher Z = Fog place above other objects)
  CLEAR_FOG = true # Clear all the fogs when teleport? (true/false)
  # You can call script to turn on/off this option by:
  # $game_system.clear_fog = (true/false)
  
  attr_accessor :name, :opacity, :blend, :ox, :oy, :id, :zoom, :tone
  def reset_fog
    #------------------------------------------------------------------------
    # ● Default Fog Setting
    # - This setup will be use if you don't set them in game before show fog
    # - This setup will replace last setup everytime after you show fog
    #------------------------------------------------------------------------
    @id = 0 # ID of Fog
    @name = "" # Leave it ""
    @opacity = 255 # Fog opacity (0 - 255)
    @blend = 0 # [0: Normal, 1: Add, 2: Subtraction]
    @ox = 0 # Speed to move fog horizontally
    @oy = 0 # Speed to move fog vertically
    @zoom = 100 # in %
    @tone = [0,0,0,0] # [Red, Green, Blue, Gray]
  end
  
  def initialize
    reset_fog
  end
  
  def find(id)
    for i in 0..$game_system.fog.size - 1
    next if $game_system.fog[i] == nil
     if $game_system.fog[i]["id"] == id
       return i
       break
     end
   end
   return nil
 end
  
  def opacity(id = 0,opacity = 255)
    return if id == 0
    id = find(id)
    if id != nil
    $game_system.fog[id]["opacity"] = opacity
    $game_system.fog_refresh = true
    end
  end
  
  def tone(id = 0,tone = [0,0,0,0])
    return if id == 0
    id = find(id)
    if id != nil
    $game_system.fog[id]["tone"] = tone
    $game_system.fog_refresh = true
    end
  end
  
  def zoom(id = 0,size = 100)
    return if id == 0
    id = find(id)
    if id != nil
    $game_system.fog[id]["zoom"] = (size / 100)
    $game_system.fog_refresh = true
    end
  end
  
  def blend(id = 0,blend = 0)
    return if id == 0
    id = find(id)
    if id != nil
    $game_system.fog[id]["blend"] = blend
    $game_system.fog_refresh = true
    end
  end
  
  def speed(id = 0, ox = 0, oy = 0)
    return if id == 0
    id = find(id)
    if id != nil
    $game_system.fog[id]["ox"] = ox
    $game_system.fog[id]["oy"] = oy
    $game_system.fog_refresh = true
    end
  end
  
  def speed_plus(id = 0, ox = 0, oy = 0)
    return if id == 0
    id = find(id)
    if id != nil
    $game_system.fog[id]["ox"] += ox
    $game_system.fog[id]["oy"] += oy
    $game_system.fog_refresh = true
    end
  end
  
  def delete(id = 0)
    return if id == 0
    id = find(id)
    if id != nil
    $game_system.fog.delete_at(id)
    $game_system.fog_refresh = true
    end
  end
  
  def show
    return if @id == 0 or @name == ""
    delete(@id)
    $game_system.fog.push ({"id" => @id, "name" => @name, "opacity" => @opacity, "blend" => @blend, "ox" => @ox, "oy" => @oy, "zoom" => (@zoom / 100), "tone" => @tone})
    $game_system.fog_refresh = true
    reset_fog
  end
  
  def clear
    $game_system.fog.clear
    $game_system.fog_refresh = true
  end
  
  #--------------------------------------
  $worale = {} if !$worale
  $worale["MutipleFog"] = true
  #--------------------------------------
  $fog = Worale_Multiple_Fog.new
  #=========================================
end

class Spriteset_Map
  alias wor_fog_sprmap_ini initialize
  alias wor_fog_sprmap_upd update
  alias wor_fog_sprmap_dis dispose
  def initialize
    create_fog
    wor_fog_sprmap_ini
  end
  def dispose
    dispose_fog
    wor_fog_sprmap_dis
  end
  def update
    update_fog
    wor_fog_sprmap_upd
  end

  def create_fog
    @fog_map = []
    @fog_oxoy = []
    return if $game_system.fog == [] or $game_system.fog == nil
    for i in 0..$game_system.fog.size - 1
      next if $game_system.fog[i] == nil
      id = $game_system.fog[i]["id"]
      @fog_map[id] = Plane.new(@viewport1)
      @fog_map[id].bitmap = Cache.picture($game_system.fog[i]["name"])
      @fog_map[id].opacity = $game_system.fog[i]["opacity"]
      @fog_map[id].blend_type = $game_system.fog[i]["blend"]
      @fog_map[id].zoom_x = @fog_map[id].zoom_y = $game_system.fog[i]["zoom"]
      @fog_map[id].tone = Tone.new($game_system.fog[i]["tone"][0],$game_system.fog[i]["tone"][1],$game_system.fog[i]["tone"][2])
      @fog_map[id].z = (id + Worale_Multiple_Fog::Z_BASE)
      @fog_oxoy[id] = [$game_system.fog[i]["ox"],$game_system.fog[i]["oy"]]
    end
  end
  
  def update_fog
    if $game_system.fog_refresh == true
      $game_system.fog_refresh = false
      dispose_fog
      create_fog
    end
    return if $game_system.fog == [] or $game_system.fog == nil
    return if @fog_map == [] or @fog_oxoy == []
    for i in 0..$game_system.fog.size - 1
      next if $game_system.fog[i] == nil
      id = $game_system.fog[i]["id"]
      return if @fog_map == [] or @fog_oxoy == []
      next if @fog_map[id].nil? or @fog_oxoy[id].nil?
      @fog_map[id].ox += @fog_oxoy[id][0]
      @fog_map[id].oy += @fog_oxoy[id][1]
    end
  end
  
  def dispose_fog
    return if @fog_mag == []
      for i in 0..@fog_map.size - 1
      next if @fog_map[i] == nil
        @fog_map[i].bitmap.dispose
        @fog_map[i].dispose
        @fog_map[i] = nil
        @fog_oxoy[i] = nil
      end
  end
end

class Game_System
  attr_accessor :fog, :fog_refresh, :clear_fog
  alias wor_fog_gamsys_ini initialize
  def initialize
    wor_fog_gamsys_ini
    @fog = Array.new
    @fog_refresh = false
    @clear_fog = Worale_Multiple_Fog::CLEAR_FOG
  end
end

class Window_Message < Window_Selectable
  alias wor_fog_winmes_ini initialize
  def initialize
    wor_fog_winmes_ini
    self.z += 200
  end
end

class Game_Interpreter
  alias wor_fog_gamint_201 command_201
  def command_201
  if $game_system.clear_fog
    if @params[0] == 0
      id_map = @params[1]
    else
      id_map = $game_variables[@params[1]]
    end
    $fog.clear if id_map != @map_id
  end
    wor_fog_gamint_201
  end
end