ezmash

Multi Layer Fog v2.0

Mar 23rd, 2017
277
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #============================================================================
  2. # Multi Layer Fog
  3. # v2.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. # History
  12. # v2.0 - attempt to fix the "stick with player" issue some people were having
  13. #----------------------------------------------------------------------------
  14. # To Install:
  15. # Copy and paste into a new script slot in Materials. This script aliases
  16. # existing methods, so can go below all other custom scripts.
  17. #----------------------------------------------------------------------------
  18. # To Use:
  19. # Create a Fogs folder inside your game's Graphics folder, and paste your
  20. # fog png files there.
  21. #
  22. # To add fog to a map, use the following in a Call Script event command:
  23. # show_fog(number, "filename", hue, opacity, blend_type, zoom,
  24. # speed-x, speed-y, z)
  25. # - only number and filename are mandatory
  26. # - fog is added like pictures - the number is simply an id (an array index),
  27. # and if you don't specify the z value, fog with a higher number/id is drawn
  28. # over fog with a lower number/id
  29. # - z position defaults to above the player. If you want to show fog above the
  30. # parallax layer but behind the map, use a negative value for z. The
  31. # parallax is drawn at -100
  32. #
  33. # To change fog tone, use the following in a Call Script event command:
  34. # tint_fog(number, red, green, blue, gray, duration)
  35. #
  36. # To change the fog opacity, use the following in a Call Script event command:
  37. # fade_fog(number, opacity, duration)
  38. #
  39. # To remove fog, use the following in a Call Script event command:
  40. # erase_fog(number)
  41. #
  42. # By default fog will be removed when you change maps, and battle screens
  43. # will have the same fog as the map. Change the CUSTOMIZATION options below
  44. # if you do not want this.
  45. #----------------------------------------------------------------------------
  46. # Terms:
  47. # Use in free or commercial games
  48. # Credit Shaz
  49. #============================================================================
  50. #============================================================================
  51. # CUSTOMIZATION
  52. #
  53. # Set this to true or false to show (or not show) fog in the battle screen
  54. BATTLE_FOGS = true
  55.  
  56. # Set this to true or false to clear (or not clear) fogs on transfer to new map
  57. CLEAR_ON_TRANSFER = true
  58.  
  59. # DO NOT CHANGE ANYTHING BELOW THIS LINE
  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 = @sx2 = sx
  181.         @sy = @sy2 = 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.         @sx2 -= @sx / 8.0
  220.         @sy2 -= @sy / 8.0
  221.         @ox = $game_map.display_x * 32 + @sx2
  222.         @oy = $game_map.display_y * 32 + @sy2
  223.     end
  224.     #--------------------------------------------------------------------------
  225.     # * Update Fog Tone Change
  226.     #--------------------------------------------------------------------------
  227.     def update_tone_change
  228.         return if @tone_duration == 0
  229.         d = @tone_duration
  230.         target = @tone_target
  231.         @tone.red = (@tone.red * (d - 1) + target.red) / d
  232.         @tone.green = (@tone.green * (d - 1) + target.green) / d
  233.         @tone.blue = (@tone.blue * (d - 1) + target.blue) / d
  234.         @tone.gray = (@tone.gray * (d - 1) + target.gray) / d
  235.         @tone_duration -= 1
  236.     end
  237.     #--------------------------------------------------------------------------
  238.     # * Update Fog Opacity Change
  239.     #--------------------------------------------------------------------------
  240.     def update_opacity_change
  241.         return if @opacity_duration == 0
  242.         d = @opacity_duration
  243.         @opacity = (@opacity * (d - 1) + @opacity_target) / d
  244.         @opacity_duration -= 1
  245.     end
  246. end
  247.  
  248. class Game_Fogs
  249.     #--------------------------------------------------------------------------
  250.     # * Object Initialization
  251.     #--------------------------------------------------------------------------
  252.     def initialize
  253.         @data = []
  254.     end
  255.     #--------------------------------------------------------------------------
  256.     # * Get Picture
  257.     #--------------------------------------------------------------------------
  258.     def [](number)
  259.         @data[number] ||= Game_Fog.new(number)
  260.     end
  261.     #--------------------------------------------------------------------------
  262.     # * Iterator
  263.     #--------------------------------------------------------------------------
  264.     def each
  265.         @data.compact.each {|fog| yield fog } if block_given?
  266.     end
  267. end
  268.  
  269. if CLEAR_ON_TRANSFER
  270.     class Game_Player < Game_Character
  271.         #--------------------------------------------------------------------------
  272.         # * Execute Player Transfer
  273.         #--------------------------------------------------------------------------
  274.         alias shaz_multi_fog_game_player_perform_transfer perform_transfer
  275.         def perform_transfer
  276.             if transfer? && @new_map_id != $game_map.map_id
  277.                 $game_map.screen.fogs.each { |fog| fog.erase }
  278.             end
  279.             shaz_multi_fog_game_player_perform_transfer
  280.         end
  281.     end
  282. end
  283.  
  284. class Game_Interpreter
  285.     #--------------------------------------------------------------------------
  286.     # * Show Fog
  287.     #--------------------------------------------------------------------------
  288.     def show_fog(number, name, hue = 90, opacity = 64.0, blend_type = 0, zoom = 200, sx = 0, sy = 0, z = nil)
  289.         screen.fogs[number].show(name, hue, opacity, blend_type, zoom, sx, sy, z)
  290.     end
  291.     #--------------------------------------------------------------------------
  292.     # * Tint Fog
  293.     #--------------------------------------------------------------------------
  294.     def tint_fog(number, red, green, blue, gray, duration)
  295.         screen.fogs[number].start_tone_change(Tone.new(red, green, blue, gray), 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