ezmash

Multi Layer Fog (VX Ace)

Oct 7th, 2013
993
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #============================================================================
  2. # Multi Layer Fog
  3. # v1.0 by Shaz
  4. #----------------------------------------------------------------------------
  5. # This script lets you add one or more layers of fog to a map.
  6. # You can add vertical and/or horizontal scrolling to each fog layer.
  7. # You can have fog appear on top of the map or behind the map (above the
  8. # parallax layer).
  9. # You can adjust the fog's opacity and tone over time.
  10. #----------------------------------------------------------------------------
  11. # To Install:
  12. # Copy and paste into a new script slot in Materials.  This script aliases
  13. # existing methods, so can go below all other custom scripts.
  14. #----------------------------------------------------------------------------
  15. # To Use:
  16. # Create a Fogs folder inside your game's Graphics folder, and paste your
  17. # fog png files there.
  18. #
  19. # To add fog to a map, use the following in a Call Script event command:
  20. #    show_fog(number, "filename", hue, opacity, blend_type, zoom,
  21. #      speed-x, speed-y, z)
  22. # - only number and filename are mandatory
  23. # - fog is added like pictures - the number is simply an id (an array index),
  24. #   and if you don't specify the z value, fog with a higher number/id is drawn
  25. #   over fog with a lower number/id
  26. # - z position defaults to above the player.  If you want to show fog above the
  27. #   parallax layer but behind the map, use a negative value for z.  The
  28. #   parallax is drawn at -100
  29. #
  30. # To change fog tone, use the following in a Call Script event command:
  31. #    tint_fog(number, red, green, blue, gray, duration)
  32. #
  33. # To change the fog opacity, use the following in a Call Script event command:
  34. #    fade_fog(number, opacity, duration)
  35. #
  36. # To remove fog, use the following in a Call Script event command:
  37. #    erase_fog(number)
  38. #
  39. # By default fog will be removed when you change maps, and battle screens
  40. # will have the same fog as the map.  Change the CUSTOMIZATION options below
  41. # if you do not want this.
  42. #----------------------------------------------------------------------------
  43. # Terms:
  44. # Use in free or commercial games
  45. # Credit Shaz
  46. #============================================================================
  47.  
  48. #============================================================================
  49. # CUSTOMIZATION
  50. #
  51. # Set this to true or false to show (or not show) fog in the battle screen
  52. BATTLE_FOGS = true
  53.  
  54. # Set this to true or false to clear (or not clear) fogs on transfer to new map
  55. CLEAR_ON_TRANSFER = true
  56.  
  57. # DO NOT CHANGE ANYTHING BELOW THIS LINE
  58. #============================================================================
  59.  
  60.  
  61. module Cache
  62.   #--------------------------------------------------------------------------
  63.   # * Get Fog
  64.   #--------------------------------------------------------------------------
  65.   def self.fog(filename, hue)
  66.     load_bitmap("Graphics/Fogs/", filename, hue)
  67.   end
  68. end
  69.  
  70. class Game_Screen
  71.   #--------------------------------------------------------------------------
  72.   # * Public Instance Variables
  73.   #--------------------------------------------------------------------------
  74.   attr_reader   :fogs
  75.   #--------------------------------------------------------------------------
  76.   # * Object Initialization
  77.   #--------------------------------------------------------------------------
  78.   alias shaz_multi_fog_game_screen_initialize initialize
  79.   def initialize
  80.     @fogs = Game_Fogs.new
  81.     shaz_multi_fog_game_screen_initialize
  82.   end
  83.   #--------------------------------------------------------------------------
  84.   # * Clear
  85.   #--------------------------------------------------------------------------
  86.   alias shaz_multi_fog_game_screen_clear clear
  87.   def clear
  88.     shaz_multi_fog_game_screen_clear
  89.     clear_fogs
  90.   end
  91.   #--------------------------------------------------------------------------
  92.   # * Clear Fogs
  93.   #--------------------------------------------------------------------------
  94.   def clear_fogs
  95.     @fogs.each {|fog| fog.erase}
  96.   end
  97.   #--------------------------------------------------------------------------
  98.   # * Frame Update
  99.   #--------------------------------------------------------------------------
  100.   alias shaz_multi_fog_game_screen_update update
  101.   def update
  102.     shaz_multi_fog_game_screen_update
  103.     update_fogs
  104.   end
  105.   #--------------------------------------------------------------------------
  106.   # * Object Initialization
  107.   #--------------------------------------------------------------------------
  108.   def update_fogs
  109.     @fogs.each {|fog| fog.update}
  110.   end
  111. end
  112.  
  113. class Game_Fog
  114.   #--------------------------------------------------------------------------
  115.   # * Public Instance Variables
  116.   #--------------------------------------------------------------------------
  117.   attr_reader   :number
  118.   attr_reader   :name
  119.   attr_reader   :hue
  120.   attr_reader   :opacity
  121.   attr_reader   :blend_type
  122.   attr_reader   :zoom
  123.   attr_reader   :sx
  124.   attr_reader   :sy
  125.   attr_reader   :ox
  126.   attr_reader   :oy
  127.   attr_reader   :tone
  128.   attr_reader   :z
  129.   #--------------------------------------------------------------------------
  130.   # * Object Initialization
  131.   #--------------------------------------------------------------------------
  132.   def initialize(number)
  133.     @number = number
  134.     init_basic
  135.     init_movement
  136.     init_tone
  137.     init_opacity
  138.   end
  139.   #--------------------------------------------------------------------------
  140.   # * Initialize Basic Variable
  141.   #--------------------------------------------------------------------------
  142.   def init_basic
  143.     @name = ""
  144.     @hue = 90
  145.     @blend_type = 0
  146.     @zoom = 200
  147.     @sx = @sy = 0
  148.   end
  149.   #--------------------------------------------------------------------------
  150.   # * Initialize Movement Variable
  151.   #--------------------------------------------------------------------------
  152.   def init_movement
  153.     @ox = @oy = 0
  154.   end
  155.   #--------------------------------------------------------------------------
  156.   # * Initialize Tone
  157.   #--------------------------------------------------------------------------
  158.   def init_tone
  159.     @tone = Tone.new
  160.     @tone_target = Tone.new
  161.     @tone_duration = 0
  162.   end
  163.   #--------------------------------------------------------------------------
  164.   # * Initialize Opacity
  165.   #--------------------------------------------------------------------------
  166.   def init_opacity
  167.     @opacity = 64.0
  168.     @opacity_target = 64.0
  169.     @opacity_duration = 0
  170.   end
  171.   #--------------------------------------------------------------------------
  172.   # * Show Fog
  173.   #--------------------------------------------------------------------------
  174.   def show(name, hue, opacity, blend_type, zoom, sx, sy, z)
  175.     @name = name
  176.     @hue = hue
  177.     @opacity = opacity
  178.     @blend_type = blend_type
  179.     @zoom = zoom
  180.     @sx = sx
  181.     @sy = sy
  182.     @z = z.nil? ? 300 + @number : z
  183.     init_movement
  184.   end
  185.   #--------------------------------------------------------------------------
  186.   # * Start Changing Tone
  187.   #--------------------------------------------------------------------------
  188.   def start_tone_change(tone, duration)
  189.     @tone_target = tone.clone
  190.     @tone_duration = duration
  191.     @tone = @tone_target.clone if @tone_duration == 0
  192.   end
  193.   #--------------------------------------------------------------------------
  194.   # * Start Opacity Change
  195.   #--------------------------------------------------------------------------
  196.   def start_opacity_change(opacity, duration)
  197.     @opacity_target = opacity * 1.0
  198.     @opacity_duration = duration
  199.     @opacity = @opacity_target if @opacity_duration == 0
  200.   end
  201.   #--------------------------------------------------------------------------
  202.   # * Erase Fog
  203.   #--------------------------------------------------------------------------
  204.   def erase
  205.     @name = ""
  206.   end
  207.   #--------------------------------------------------------------------------
  208.   # * Frame Update
  209.   #--------------------------------------------------------------------------
  210.   def update
  211.     update_move
  212.     update_tone_change
  213.     update_opacity_change
  214.   end
  215.   #--------------------------------------------------------------------------
  216.   # * Update Fog Move
  217.   #--------------------------------------------------------------------------
  218.   def update_move
  219.     @ox -= @sx / 8.0
  220.     @oy -= @sy / 8.0
  221.   end
  222.   #--------------------------------------------------------------------------
  223.   # * Update Fog Tone Change
  224.   #--------------------------------------------------------------------------
  225.   def update_tone_change
  226.     return if @tone_duration == 0
  227.     d = @tone_duration
  228.     target = @tone_target
  229.     @tone.red = (@tone.red * (d - 1) + target.red) / d
  230.     @tone.green = (@tone.green * (d - 1) + target.green) / d
  231.     @tone.blue = (@tone.blue * (d - 1) + target.blue) / d
  232.     @tone.gray = (@tone.gray * (d - 1) + target.gray) / d
  233.     @tone_duration -= 1
  234.   end
  235.   #--------------------------------------------------------------------------
  236.   # * Update Fog Opacity Change
  237.   #--------------------------------------------------------------------------
  238.   def update_opacity_change
  239.     return if @opacity_duration == 0
  240.     d = @opacity_duration
  241.     @opacity = (@opacity * (d - 1) + @opacity_target) / d
  242.     @opacity_duration -= 1
  243.   end
  244. end
  245.  
  246. class Game_Fogs
  247.   #--------------------------------------------------------------------------
  248.   # * Object Initialization
  249.   #--------------------------------------------------------------------------
  250.   def initialize
  251.     @data = []
  252.   end
  253.   #--------------------------------------------------------------------------
  254.   # * Get Picture
  255.   #--------------------------------------------------------------------------
  256.   def [](number)
  257.     @data[number] ||= Game_Fog.new(number)
  258.   end
  259.   #--------------------------------------------------------------------------
  260.   # * Iterator
  261.   #--------------------------------------------------------------------------
  262.   def each
  263.     @data.compact.each {|fog| yield fog } if block_given?
  264.   end
  265. end
  266.  
  267. if CLEAR_ON_TRANSFER
  268.   class Game_Player < Game_Character
  269.     #--------------------------------------------------------------------------
  270.     # * Execute Player Transfer
  271.     #--------------------------------------------------------------------------
  272.     alias shaz_multi_fog_game_player_perform_transfer perform_transfer
  273.     def perform_transfer
  274.       if transfer? && @new_map_id != $game_map.map_id
  275.         $game_map.screen.fogs.each { |fog| fog.erase }
  276.       end
  277.       shaz_multi_fog_game_player_perform_transfer
  278.     end
  279.   end
  280. end
  281.  
  282. class Game_Interpreter
  283.   #--------------------------------------------------------------------------
  284.   # * Show Fog
  285.   #--------------------------------------------------------------------------
  286.   def show_fog(number, name, hue = 90, opacity = 64.0, blend_type = 0, zoom = 200,
  287.     sx = 0, sy = 0, z = nil)
  288.     screen.fogs[number].show(name, hue, opacity, blend_type, zoom, sx, sy, z)
  289.   end
  290.   #--------------------------------------------------------------------------
  291.   # * Tint Fog
  292.   #--------------------------------------------------------------------------
  293.   def tint_fog(number, red, green, blue, gray, duration)
  294.     screen.fogs[number].start_tone_change(Tone.new(red, green, blue, gray),
  295.       duration)
  296.   end
  297.   #--------------------------------------------------------------------------
  298.   # * Change Fog Opacity
  299.   #--------------------------------------------------------------------------
  300.   def fade_fog(number, opacity, duration)
  301.     screen.fogs[number].start_opacity_change(opacity, duration)
  302.   end
  303.   #--------------------------------------------------------------------------
  304.   # * Erase Fog
  305.   #--------------------------------------------------------------------------
  306.   def erase_fog(number)
  307.     screen.fogs[number].erase
  308.   end
  309. end
  310.  
  311. class Plane_Fog < Plane
  312.   #--------------------------------------------------------------------------
  313.   # * Object Initialization
  314.   #--------------------------------------------------------------------------
  315.   def initialize(viewport, fog)
  316.     super(viewport)
  317.     @fog = fog
  318.     self.z = @fog.z
  319.     self.zoom_x = @fog.zoom / 100
  320.     self.zoom_y = @fog.zoom / 100
  321.     self.blend_type = @fog.blend_type
  322.     update
  323.   end
  324.   #--------------------------------------------------------------------------
  325.   # * Free
  326.   #--------------------------------------------------------------------------
  327.   def dispose
  328.     bitmap.dispose if bitmap
  329.     super
  330.   end
  331.   #--------------------------------------------------------------------------
  332.   # * Frame Update
  333.   #--------------------------------------------------------------------------
  334.   def update
  335.     update_bitmap
  336.     update_move
  337.     update_tone
  338.     update_opacity
  339.   end
  340.   #--------------------------------------------------------------------------
  341.   # * Update Bitmap
  342.   #--------------------------------------------------------------------------
  343.   def update_bitmap
  344.     if @fog.name.empty?
  345.       self.bitmap = nil
  346.     else
  347.       self.bitmap = Cache.fog(@fog.name, @fog.hue)
  348.     end
  349.   end
  350.   #--------------------------------------------------------------------------
  351.   # * Update Move
  352.   #--------------------------------------------------------------------------
  353.   def update_move
  354.     self.ox = @fog.ox
  355.     self.oy = @fog.oy
  356.   end
  357.   #--------------------------------------------------------------------------
  358.   # * Update Tone
  359.   #--------------------------------------------------------------------------
  360.   def update_tone
  361.     self.tone = @fog.tone
  362.   end
  363.   #--------------------------------------------------------------------------
  364.   # * Update Opacity
  365.   #--------------------------------------------------------------------------
  366.   def update_opacity
  367.     self.opacity = @fog.opacity
  368.   end
  369. end  
  370.  
  371. class Spriteset_Map
  372.   #--------------------------------------------------------------------------
  373.   # * Object Initialization
  374.   #--------------------------------------------------------------------------
  375.   alias shaz_multi_fog_spriteset_map_initialize initialize
  376.   def initialize
  377.     shaz_multi_fog_spriteset_map_initialize
  378.     create_fogs
  379.     update
  380.   end
  381.   #--------------------------------------------------------------------------
  382.   # * Create Fog Plane
  383.   #--------------------------------------------------------------------------
  384.   def create_fogs
  385.     @fog_planes = []
  386.   end
  387.   #--------------------------------------------------------------------------
  388.   # * Free
  389.   #--------------------------------------------------------------------------
  390.   alias shaz_multi_fog_spriteset_map_dispose dispose
  391.   def dispose
  392.     dispose_fogs
  393.     shaz_multi_fog_spriteset_map_dispose
  394.   end
  395.   #--------------------------------------------------------------------------
  396.   # * Free Fog Plane
  397.   #--------------------------------------------------------------------------
  398.   def dispose_fogs
  399.     @fog_planes.compact.each {|fog| fog.dispose }
  400.   end
  401.   #--------------------------------------------------------------------------
  402.   # * Frame Update
  403.   #--------------------------------------------------------------------------
  404.   alias shaz_multi_fog_spriteset_map_update update
  405.   def update
  406.     update_fogs
  407.     shaz_multi_fog_spriteset_map_update
  408.   end
  409.   #--------------------------------------------------------------------------
  410.   # * Update Fogs
  411.   #--------------------------------------------------------------------------
  412.   def update_fogs
  413.     return if !@fog_planes
  414.     $game_map.screen.fogs.each do |fog|
  415.       @fog_planes[fog.number] ||= Plane_Fog.new(@viewport1, fog)
  416.       @fog_planes[fog.number].update
  417.     end
  418.   end
  419. end
  420.  
  421. if BATTLE_FOGS
  422.   class Spriteset_Battle
  423.     #--------------------------------------------------------------------------
  424.     # * Object Initialization
  425.     #--------------------------------------------------------------------------
  426.     alias shaz_multi_fog_spriteset_battle_initialize initialize
  427.     def initialize
  428.       shaz_multi_fog_spriteset_battle_initialize
  429.       create_fogs
  430.       update
  431.     end
  432.     #--------------------------------------------------------------------------
  433.     # * Create Fog Plane
  434.     #--------------------------------------------------------------------------
  435.     def create_fogs
  436.       @fog_planes = []
  437.     end
  438.     #--------------------------------------------------------------------------
  439.     # * Free
  440.     #--------------------------------------------------------------------------
  441.     alias shaz_multi_fog_spriteset_map_dispose dispose
  442.     def dispose
  443.       dispose_fogs
  444.       shaz_multi_fog_spriteset_map_dispose
  445.     end
  446.     #--------------------------------------------------------------------------
  447.     # * Free Fog Plane
  448.     #--------------------------------------------------------------------------
  449.     def dispose_fogs
  450.       @fog_planes.compact.each {|fog| fog.dispose }
  451.     end
  452.     #--------------------------------------------------------------------------
  453.     # * Frame Update
  454.     #--------------------------------------------------------------------------
  455.     alias shaz_multi_fog_spriteset_map_update update
  456.     def update
  457.       update_fogs
  458.       shaz_multi_fog_spriteset_map_update
  459.     end
  460.     #--------------------------------------------------------------------------
  461.     # * Update Fogs
  462.     #--------------------------------------------------------------------------
  463.     def update_fogs
  464.       return if !@fog_planes
  465.       $game_map.screen.update_fogs
  466.       $game_map.screen.fogs.each do |fog|
  467.         @fog_planes[fog.number] ||= Plane_Fog.new(@viewport1, fog)
  468.         @fog_planes[fog.number].update
  469.       end
  470.     end
  471.   end
  472. end # BATTLE_FOGS
RAW Paste Data