ezmash

Multi Layer Fog (VX Ace)

Oct 7th, 2013 (edited)
1,047
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. # Updates
  44. # 2021.02.27 Fixed blend type when transferring to other maps (thanks to BrokenAnkle21)
  45. #----------------------------------------------------------------------------
  46. # Terms:
  47. # Use in free or commercial games
  48. # Credit Shaz
  49. #============================================================================
  50.  
  51. #============================================================================
  52. # CUSTOMIZATION
  53. #
  54. # Set this to true or false to show (or not show) fog in the battle screen
  55. BATTLE_FOGS = true
  56.  
  57. # Set this to true or false to clear (or not clear) fogs on transfer to new map
  58. CLEAR_ON_TRANSFER = true
  59.  
  60. # DO NOT CHANGE ANYTHING BELOW THIS LINE
  61. #============================================================================
  62.  
  63.  
  64. module Cache
  65.   #--------------------------------------------------------------------------
  66.   # * Get Fog
  67.   #--------------------------------------------------------------------------
  68.   def self.fog(filename, hue)
  69.     load_bitmap("Graphics/Fogs/", filename, hue)
  70.   end
  71. end
  72.  
  73. class Game_Screen
  74.   #--------------------------------------------------------------------------
  75.   # * Public Instance Variables
  76.   #--------------------------------------------------------------------------
  77.   attr_reader   :fogs
  78.   #--------------------------------------------------------------------------
  79.   # * Object Initialization
  80.   #--------------------------------------------------------------------------
  81.   alias shaz_multi_fog_game_screen_initialize initialize
  82.   def initialize
  83.     @fogs = Game_Fogs.new
  84.     shaz_multi_fog_game_screen_initialize
  85.   end
  86.   #--------------------------------------------------------------------------
  87.   # * Clear
  88.   #--------------------------------------------------------------------------
  89.   alias shaz_multi_fog_game_screen_clear clear
  90.   def clear
  91.     shaz_multi_fog_game_screen_clear
  92.     clear_fogs
  93.   end
  94.   #--------------------------------------------------------------------------
  95.   # * Clear Fogs
  96.   #--------------------------------------------------------------------------
  97.   def clear_fogs
  98.     @fogs.each {|fog| fog.erase}
  99.   end
  100.   #--------------------------------------------------------------------------
  101.   # * Frame Update
  102.   #--------------------------------------------------------------------------
  103.   alias shaz_multi_fog_game_screen_update update
  104.   def update
  105.     shaz_multi_fog_game_screen_update
  106.     update_fogs
  107.   end
  108.   #--------------------------------------------------------------------------
  109.   # * Object Initialization
  110.   #--------------------------------------------------------------------------
  111.   def update_fogs
  112.     @fogs.each {|fog| fog.update}
  113.   end
  114. end
  115.  
  116. class Game_Fog
  117.   #--------------------------------------------------------------------------
  118.   # * Public Instance Variables
  119.   #--------------------------------------------------------------------------
  120.   attr_reader   :number
  121.   attr_reader   :name
  122.   attr_reader   :hue
  123.   attr_reader   :opacity
  124.   attr_reader   :blend_type
  125.   attr_reader   :zoom
  126.   attr_reader   :sx
  127.   attr_reader   :sy
  128.   attr_reader   :ox
  129.   attr_reader   :oy
  130.   attr_reader   :tone
  131.   attr_reader   :z
  132.   #--------------------------------------------------------------------------
  133.   # * Object Initialization
  134.   #--------------------------------------------------------------------------
  135.   def initialize(number)
  136.     @number = number
  137.     init_basic
  138.     init_movement
  139.     init_tone
  140.     init_opacity
  141.   end
  142.   #--------------------------------------------------------------------------
  143.   # * Initialize Basic Variable
  144.   #--------------------------------------------------------------------------
  145.   def init_basic
  146.     @name = ""
  147.     @hue = 90
  148.     @blend_type = 0
  149.     @zoom = 200
  150.     @sx = @sy = 0
  151.   end
  152.   #--------------------------------------------------------------------------
  153.   # * Initialize Movement Variable
  154.   #--------------------------------------------------------------------------
  155.   def init_movement
  156.     @ox = @oy = 0
  157.   end
  158.   #--------------------------------------------------------------------------
  159.   # * Initialize Tone
  160.   #--------------------------------------------------------------------------
  161.   def init_tone
  162.     @tone = Tone.new
  163.     @tone_target = Tone.new
  164.     @tone_duration = 0
  165.   end
  166.   #--------------------------------------------------------------------------
  167.   # * Initialize Opacity
  168.   #--------------------------------------------------------------------------
  169.   def init_opacity
  170.     @opacity = 64.0
  171.     @opacity_target = 64.0
  172.     @opacity_duration = 0
  173.   end
  174.   #--------------------------------------------------------------------------
  175.   # * Show Fog
  176.   #--------------------------------------------------------------------------
  177.   def show(name, hue, opacity, blend_type, zoom, sx, sy, z)
  178.     @name = name
  179.     @hue = hue
  180.     @opacity = opacity
  181.     @blend_type = blend_type
  182.     @zoom = zoom
  183.     @sx = sx
  184.     @sy = sy
  185.     @z = z.nil? ? 300 + @number : z
  186.     init_movement
  187.   end
  188.   #--------------------------------------------------------------------------
  189.   # * Start Changing Tone
  190.   #--------------------------------------------------------------------------
  191.   def start_tone_change(tone, duration)
  192.     @tone_target = tone.clone
  193.     @tone_duration = duration
  194.     @tone = @tone_target.clone if @tone_duration == 0
  195.   end
  196.   #--------------------------------------------------------------------------
  197.   # * Start Opacity Change
  198.   #--------------------------------------------------------------------------
  199.   def start_opacity_change(opacity, duration)
  200.     @opacity_target = opacity * 1.0
  201.     @opacity_duration = duration
  202.     @opacity = @opacity_target if @opacity_duration == 0
  203.   end
  204.   #--------------------------------------------------------------------------
  205.   # * Erase Fog
  206.   #--------------------------------------------------------------------------
  207.   def erase
  208.     @name = ""
  209.   end
  210.   #--------------------------------------------------------------------------
  211.   # * Frame Update
  212.   #--------------------------------------------------------------------------
  213.   def update
  214.     update_move
  215.     update_tone_change
  216.     update_opacity_change
  217.   end
  218.   #--------------------------------------------------------------------------
  219.   # * Update Fog Move
  220.   #--------------------------------------------------------------------------
  221.   def update_move
  222.     @ox -= @sx / 8.0
  223.     @oy -= @sy / 8.0
  224.   end
  225.   #--------------------------------------------------------------------------
  226.   # * Update Fog Tone Change
  227.   #--------------------------------------------------------------------------
  228.   def update_tone_change
  229.     return if @tone_duration == 0
  230.     d = @tone_duration
  231.     target = @tone_target
  232.     @tone.red = (@tone.red * (d - 1) + target.red) / d
  233.     @tone.green = (@tone.green * (d - 1) + target.green) / d
  234.     @tone.blue = (@tone.blue * (d - 1) + target.blue) / d
  235.     @tone.gray = (@tone.gray * (d - 1) + target.gray) / d
  236.     @tone_duration -= 1
  237.   end
  238.   #--------------------------------------------------------------------------
  239.   # * Update Fog Opacity Change
  240.   #--------------------------------------------------------------------------
  241.   def update_opacity_change
  242.     return if @opacity_duration == 0
  243.     d = @opacity_duration
  244.     @opacity = (@opacity * (d - 1) + @opacity_target) / d
  245.     @opacity_duration -= 1
  246.   end
  247. end
  248.  
  249. class Game_Fogs
  250.   #--------------------------------------------------------------------------
  251.   # * Object Initialization
  252.   #--------------------------------------------------------------------------
  253.   def initialize
  254.     @data = []
  255.   end
  256.   #--------------------------------------------------------------------------
  257.   # * Get Picture
  258.   #--------------------------------------------------------------------------
  259.   def [](number)
  260.     @data[number] ||= Game_Fog.new(number)
  261.   end
  262.   #--------------------------------------------------------------------------
  263.   # * Iterator
  264.   #--------------------------------------------------------------------------
  265.   def each
  266.     @data.compact.each {|fog| yield fog } if block_given?
  267.   end
  268. end
  269.  
  270. if CLEAR_ON_TRANSFER
  271.   class Game_Player < Game_Character
  272.     #--------------------------------------------------------------------------
  273.     # * Execute Player Transfer
  274.     #--------------------------------------------------------------------------
  275.     alias shaz_multi_fog_game_player_perform_transfer perform_transfer
  276.     def perform_transfer
  277.       if transfer? && @new_map_id != $game_map.map_id
  278.         $game_map.screen.fogs.each { |fog| fog.erase }
  279.       end
  280.       shaz_multi_fog_game_player_perform_transfer
  281.     end
  282.   end
  283. end
  284.  
  285. class Game_Interpreter
  286.   #--------------------------------------------------------------------------
  287.   # * Show Fog
  288.   #--------------------------------------------------------------------------
  289.   def show_fog(number, name, hue = 90, opacity = 64.0, blend_type = 0, zoom = 200,
  290.     sx = 0, sy = 0, z = nil)
  291.     screen.fogs[number].show(name, hue, opacity, blend_type, zoom, sx, sy, z)
  292.   end
  293.   #--------------------------------------------------------------------------
  294.   # * Tint Fog
  295.   #--------------------------------------------------------------------------
  296.   def tint_fog(number, red, green, blue, gray, duration)
  297.     screen.fogs[number].start_tone_change(Tone.new(red, green, blue, gray),
  298.       duration)
  299.   end
  300.   #--------------------------------------------------------------------------
  301.   # * Change Fog Opacity
  302.   #--------------------------------------------------------------------------
  303.   def fade_fog(number, opacity, duration)
  304.     screen.fogs[number].start_opacity_change(opacity, duration)
  305.   end
  306.   #--------------------------------------------------------------------------
  307.   # * Erase Fog
  308.   #--------------------------------------------------------------------------
  309.   def erase_fog(number)
  310.     screen.fogs[number].erase
  311.   end
  312. end
  313.  
  314. class Plane_Fog < Plane
  315.   #--------------------------------------------------------------------------
  316.   # * Object Initialization
  317.   #--------------------------------------------------------------------------
  318.   def initialize(viewport, fog)
  319.     super(viewport)
  320.     @fog = fog
  321.     self.z = @fog.z
  322.     self.zoom_x = @fog.zoom / 100
  323.     self.zoom_y = @fog.zoom / 100
  324.     self.blend_type = @fog.blend_type
  325.     update
  326.   end
  327.   #--------------------------------------------------------------------------
  328.   # * Free
  329.   #--------------------------------------------------------------------------
  330.   def dispose
  331.     bitmap.dispose if bitmap
  332.     super
  333.   end
  334.   #--------------------------------------------------------------------------
  335.   # * Frame Update
  336.   #--------------------------------------------------------------------------
  337.   def update
  338.     update_bitmap
  339.     update_move
  340.     update_tone
  341.     update_opacity
  342.     update_blend
  343.   end
  344.   #--------------------------------------------------------------------------
  345.   # * Update Bitmap
  346.   #--------------------------------------------------------------------------
  347.   def update_bitmap
  348.     if @fog.name.empty?
  349.       self.bitmap = nil
  350.     else
  351.       self.bitmap = Cache.fog(@fog.name, @fog.hue)
  352.     end
  353.   end
  354.   #--------------------------------------------------------------------------
  355.   # * Update Move
  356.   #--------------------------------------------------------------------------
  357.   def update_move
  358.     self.ox = @fog.ox
  359.     self.oy = @fog.oy
  360.   end
  361.   #--------------------------------------------------------------------------
  362.   # * Update Tone
  363.   #--------------------------------------------------------------------------
  364.   def update_tone
  365.     self.tone = @fog.tone
  366.   end
  367.   #--------------------------------------------------------------------------
  368.   # * Update Opacity
  369.   #--------------------------------------------------------------------------
  370.   def update_opacity
  371.     self.opacity = @fog.opacity
  372.   end
  373.   #--------------------------------------------------------------------------
  374.   # * Update Blend
  375.   #--------------------------------------------------------------------------
  376.   def update_blend
  377.     self.blend_type = @fog.blend_type
  378.   end
  379. end  
  380.  
  381. class Spriteset_Map
  382.   #--------------------------------------------------------------------------
  383.   # * Object Initialization
  384.   #--------------------------------------------------------------------------
  385.   alias shaz_multi_fog_spriteset_map_initialize initialize
  386.   def initialize
  387.     shaz_multi_fog_spriteset_map_initialize
  388.     create_fogs
  389.     update
  390.   end
  391.   #--------------------------------------------------------------------------
  392.   # * Create Fog Plane
  393.   #--------------------------------------------------------------------------
  394.   def create_fogs
  395.     @fog_planes = []
  396.   end
  397.   #--------------------------------------------------------------------------
  398.   # * Free
  399.   #--------------------------------------------------------------------------
  400.   alias shaz_multi_fog_spriteset_map_dispose dispose
  401.   def dispose
  402.     dispose_fogs
  403.     shaz_multi_fog_spriteset_map_dispose
  404.   end
  405.   #--------------------------------------------------------------------------
  406.   # * Free Fog Plane
  407.   #--------------------------------------------------------------------------
  408.   def dispose_fogs
  409.     @fog_planes.compact.each {|fog| fog.dispose }
  410.   end
  411.   #--------------------------------------------------------------------------
  412.   # * Frame Update
  413.   #--------------------------------------------------------------------------
  414.   alias shaz_multi_fog_spriteset_map_update update
  415.   def update
  416.     update_fogs
  417.     shaz_multi_fog_spriteset_map_update
  418.   end
  419.   #--------------------------------------------------------------------------
  420.   # * Update Fogs
  421.   #--------------------------------------------------------------------------
  422.   def update_fogs
  423.     return if !@fog_planes
  424.     $game_map.screen.fogs.each do |fog|
  425.       @fog_planes[fog.number] ||= Plane_Fog.new(@viewport1, fog)
  426.       @fog_planes[fog.number].update
  427.     end
  428.   end
  429. end
  430.  
  431. if BATTLE_FOGS
  432.   class Spriteset_Battle
  433.     #--------------------------------------------------------------------------
  434.     # * Object Initialization
  435.     #--------------------------------------------------------------------------
  436.     alias shaz_multi_fog_spriteset_battle_initialize initialize
  437.     def initialize
  438.       shaz_multi_fog_spriteset_battle_initialize
  439.       create_fogs
  440.       update
  441.     end
  442.     #--------------------------------------------------------------------------
  443.     # * Create Fog Plane
  444.     #--------------------------------------------------------------------------
  445.     def create_fogs
  446.       @fog_planes = []
  447.     end
  448.     #--------------------------------------------------------------------------
  449.     # * Free
  450.     #--------------------------------------------------------------------------
  451.     alias shaz_multi_fog_spriteset_map_dispose dispose
  452.     def dispose
  453.       dispose_fogs
  454.       shaz_multi_fog_spriteset_map_dispose
  455.     end
  456.     #--------------------------------------------------------------------------
  457.     # * Free Fog Plane
  458.     #--------------------------------------------------------------------------
  459.     def dispose_fogs
  460.       @fog_planes.compact.each {|fog| fog.dispose }
  461.     end
  462.     #--------------------------------------------------------------------------
  463.     # * Frame Update
  464.     #--------------------------------------------------------------------------
  465.     alias shaz_multi_fog_spriteset_map_update update
  466.     def update
  467.       update_fogs
  468.       shaz_multi_fog_spriteset_map_update
  469.     end
  470.     #--------------------------------------------------------------------------
  471.     # * Update Fogs
  472.     #--------------------------------------------------------------------------
  473.     def update_fogs
  474.       return if !@fog_planes
  475.       $game_map.screen.update_fogs
  476.       $game_map.screen.fogs.each do |fog|
  477.         @fog_planes[fog.number] ||= Plane_Fog.new(@viewport1, fog)
  478.         @fog_planes[fog.number].update
  479.       end
  480.     end
  481.   end
  482. end # BATTLE_FOGS
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×