Advertisement
ezmash

Multi Layer Fog v2.0

Mar 23rd, 2017 (edited)
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 17.56 KB | None | 0 0
  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. # 2021.02.27 Fix blend type when transferring maps (thanks to BrokenAnkle21)
  14. #----------------------------------------------------------------------------
  15. # To Install:
  16. # Copy and paste into a new script slot in Materials. This script aliases
  17. # existing methods, so can go below all other custom scripts.
  18. #----------------------------------------------------------------------------
  19. # To Use:
  20. # Create a Fogs folder inside your game's Graphics folder, and paste your
  21. # fog png files there.
  22. #
  23. # To add fog to a map, use the following in a Call Script event command:
  24. # show_fog(number, "filename", hue, opacity, blend_type, zoom,
  25. # speed-x, speed-y, z)
  26. # - only number and filename are mandatory
  27. # - fog is added like pictures - the number is simply an id (an array index),
  28. # and if you don't specify the z value, fog with a higher number/id is drawn
  29. # over fog with a lower number/id
  30. # - z position defaults to above the player. If you want to show fog above the
  31. # parallax layer but behind the map, use a negative value for z. The
  32. # parallax is drawn at -100
  33. #
  34. # To change fog tone, use the following in a Call Script event command:
  35. # tint_fog(number, red, green, blue, gray, duration)
  36. #
  37. # To change the fog opacity, use the following in a Call Script event command:
  38. # fade_fog(number, opacity, duration)
  39. #
  40. # To remove fog, use the following in a Call Script event command:
  41. # erase_fog(number)
  42. #
  43. # By default fog will be removed when you change maps, and battle screens
  44. # will have the same fog as the map. Change the CUSTOMIZATION options below
  45. # if you do not want this.
  46. #----------------------------------------------------------------------------
  47. # Terms:
  48. # Use in free or commercial games
  49. # Credit Shaz
  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. module Cache
  63.     #--------------------------------------------------------------------------
  64.     # * Get Fog
  65.     #--------------------------------------------------------------------------
  66.     def self.fog(filename, hue)
  67.         load_bitmap("Graphics/Fogs/", filename, hue)
  68.     end
  69. end
  70.  
  71. class Game_Screen
  72.     #--------------------------------------------------------------------------
  73.     # * Public Instance Variables
  74.     #--------------------------------------------------------------------------
  75.     attr_reader :fogs
  76.     #--------------------------------------------------------------------------
  77.     # * Object Initialization
  78.     #--------------------------------------------------------------------------
  79.     alias shaz_multi_fog_game_screen_initialize initialize
  80.     def initialize
  81.         @fogs = Game_Fogs.new
  82.         shaz_multi_fog_game_screen_initialize
  83.     end
  84.     #--------------------------------------------------------------------------
  85.     # * Clear
  86.     #--------------------------------------------------------------------------
  87.     alias shaz_multi_fog_game_screen_clear clear
  88.     def clear
  89.         shaz_multi_fog_game_screen_clear
  90.         clear_fogs
  91.     end
  92.     #--------------------------------------------------------------------------
  93.     # * Clear Fogs
  94.     #--------------------------------------------------------------------------
  95.     def clear_fogs
  96.         @fogs.each {|fog| fog.erase}
  97.     end
  98.     #--------------------------------------------------------------------------
  99.     # * Frame Update
  100.     #--------------------------------------------------------------------------
  101.     alias shaz_multi_fog_game_screen_update update
  102.     def update
  103.         shaz_multi_fog_game_screen_update
  104.         update_fogs
  105.     end
  106.     #--------------------------------------------------------------------------
  107.     # * Object Initialization
  108.     #--------------------------------------------------------------------------
  109.     def update_fogs
  110.         @fogs.each {|fog| fog.update}
  111.     end
  112. end
  113.  
  114. class Game_Fog
  115.     #--------------------------------------------------------------------------
  116.     # * Public Instance Variables
  117.     #--------------------------------------------------------------------------
  118.     attr_reader :number
  119.     attr_reader :name
  120.     attr_reader :hue
  121.     attr_reader :opacity
  122.     attr_reader :blend_type
  123.     attr_reader :zoom
  124.     attr_reader :sx
  125.     attr_reader :sy
  126.     attr_reader :ox
  127.     attr_reader :oy
  128.     attr_reader :tone
  129.     attr_reader :z
  130.     #--------------------------------------------------------------------------
  131.     # * Object Initialization
  132.     #--------------------------------------------------------------------------
  133.     def initialize(number)
  134.         @number = number
  135.         init_basic
  136.         init_movement
  137.         init_tone
  138.         init_opacity
  139.     end
  140.     #--------------------------------------------------------------------------
  141.     # * Initialize Basic Variable
  142.     #--------------------------------------------------------------------------
  143.     def init_basic
  144.         @name = ""
  145.         @hue = 90
  146.         @blend_type = 0
  147.         @zoom = 200
  148.         @sx = @sy = 0
  149.     end
  150.     #--------------------------------------------------------------------------
  151.     # * Initialize Movement Variable
  152.     #--------------------------------------------------------------------------
  153.     def init_movement
  154.         @ox = @oy = 0
  155.     end
  156.     #--------------------------------------------------------------------------
  157.     # * Initialize Tone
  158.     #--------------------------------------------------------------------------
  159.     def init_tone
  160.         @tone = Tone.new
  161.         @tone_target = Tone.new
  162.         @tone_duration = 0
  163.     end
  164.     #--------------------------------------------------------------------------
  165.     # * Initialize Opacity
  166.     #--------------------------------------------------------------------------
  167.     def init_opacity
  168.         @opacity = 64.0
  169.         @opacity_target = 64.0
  170.         @opacity_duration = 0
  171.     end
  172.     #--------------------------------------------------------------------------
  173.     # * Show Fog
  174.     #--------------------------------------------------------------------------
  175.     def show(name, hue, opacity, blend_type, zoom, sx, sy, z)
  176.         @name = name
  177.         @hue = hue
  178.         @opacity = opacity
  179.         @blend_type = blend_type
  180.         @zoom = zoom
  181.         @sx = @sx2 = sx
  182.         @sy = @sy2 = sy
  183.         @z = z.nil? ? 300 + @number : z
  184.         init_movement
  185.     end
  186.     #--------------------------------------------------------------------------
  187.     # * Start Changing Tone
  188.     #--------------------------------------------------------------------------
  189.     def start_tone_change(tone, duration)
  190.         @tone_target = tone.clone
  191.         @tone_duration = duration
  192.         @tone = @tone_target.clone if @tone_duration == 0
  193.     end
  194.     #--------------------------------------------------------------------------
  195.     # * Start Opacity Change
  196.     #--------------------------------------------------------------------------
  197.     def start_opacity_change(opacity, duration)
  198.         @opacity_target = opacity * 1.0
  199.         @opacity_duration = duration
  200.         @opacity = @opacity_target if @opacity_duration == 0
  201.     end
  202.     #--------------------------------------------------------------------------
  203.     # * Erase Fog
  204.     #--------------------------------------------------------------------------
  205.     def erase
  206.         @name = ""
  207.     end
  208.     #--------------------------------------------------------------------------
  209.     # * Frame Update
  210.     #--------------------------------------------------------------------------
  211.     def update
  212.         update_move
  213.         update_tone_change
  214.         update_opacity_change
  215.     end
  216.     #--------------------------------------------------------------------------
  217.     # * Update Fog Move
  218.     #--------------------------------------------------------------------------
  219.     def update_move
  220.         @sx2 -= @sx / 8.0
  221.         @sy2 -= @sy / 8.0
  222.         @ox = $game_map.display_x * 32 + @sx2
  223.         @oy = $game_map.display_y * 32 + @sy2
  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, sx = 0, sy = 0, z = nil)
  290.         screen.fogs[number].show(name, hue, opacity, blend_type, zoom, sx, sy, z)
  291.     end
  292.     #--------------------------------------------------------------------------
  293.     # * Tint Fog
  294.     #--------------------------------------------------------------------------
  295.     def tint_fog(number, red, green, blue, gray, duration)
  296.         screen.fogs[number].start_tone_change(Tone.new(red, green, blue, gray), duration)
  297.     end
  298.     #--------------------------------------------------------------------------
  299.     # * Change Fog Opacity
  300.     #--------------------------------------------------------------------------
  301.     def fade_fog(number, opacity, duration)
  302.         screen.fogs[number].start_opacity_change(opacity, duration)
  303.     end
  304.     #--------------------------------------------------------------------------
  305.     # * Erase Fog
  306.     #--------------------------------------------------------------------------
  307.     def erase_fog(number)
  308.         screen.fogs[number].erase
  309.     end
  310. end
  311.  
  312. class Plane_Fog < Plane
  313.     #--------------------------------------------------------------------------
  314.     # * Object Initialization
  315.     #--------------------------------------------------------------------------
  316.     def initialize(viewport, fog)
  317.         super(viewport)
  318.         @fog = fog
  319.         self.z = @fog.z
  320.         self.zoom_x = @fog.zoom / 100
  321.         self.zoom_y = @fog.zoom / 100
  322.         self.blend_type = @fog.blend_type
  323.         update
  324.     end
  325.     #--------------------------------------------------------------------------
  326.     # * Free
  327.     #--------------------------------------------------------------------------
  328.     def dispose
  329.         bitmap.dispose if bitmap
  330.         super
  331.     end
  332.     #--------------------------------------------------------------------------
  333.     # * Frame Update
  334.     #--------------------------------------------------------------------------
  335.     def update
  336.         update_bitmap
  337.         update_move
  338.         update_tone
  339.         update_opacity  
  340.         update_blend
  341.     end
  342.     #--------------------------------------------------------------------------
  343.     # * Update Bitmap
  344.     #--------------------------------------------------------------------------
  345.     def update_bitmap
  346.         if @fog.name.empty?
  347.             self.bitmap = nil
  348.         else
  349.             self.bitmap = Cache.fog(@fog.name, @fog.hue)
  350.         end
  351.     end
  352.     #--------------------------------------------------------------------------
  353.     # * Update Move
  354.     #--------------------------------------------------------------------------
  355.     def update_move
  356.         self.ox = @fog.ox
  357.         self.oy = @fog.oy
  358.     end
  359.     #--------------------------------------------------------------------------
  360.     # * Update Tone
  361.     #--------------------------------------------------------------------------
  362.     def update_tone
  363.         self.tone = @fog.tone
  364.     end
  365.     #--------------------------------------------------------------------------
  366.     # * Update Opacity
  367.     #--------------------------------------------------------------------------
  368.     def update_opacity
  369.         self.opacity = @fog.opacity
  370.     end
  371.     #--------------------------------------------------------------------------
  372.     # * Update Blend
  373.     #--------------------------------------------------------------------------
  374.     def update_blend
  375.         self.blend_type = @fog.blend_type
  376.     end
  377. end
  378.  
  379. class Spriteset_Map
  380.     #--------------------------------------------------------------------------
  381.     # * Object Initialization
  382.     #--------------------------------------------------------------------------
  383.     alias shaz_multi_fog_spriteset_map_initialize initialize
  384.     def initialize
  385.         shaz_multi_fog_spriteset_map_initialize
  386.         create_fogs
  387.         update
  388.     end
  389.     #--------------------------------------------------------------------------
  390.     # * Create Fog Plane
  391.     #--------------------------------------------------------------------------
  392.     def create_fogs
  393.         @fog_planes = []
  394.     end
  395.     #--------------------------------------------------------------------------
  396.     # * Free
  397.     #--------------------------------------------------------------------------
  398.     alias shaz_multi_fog_spriteset_map_dispose dispose
  399.     def dispose
  400.         dispose_fogs
  401.         shaz_multi_fog_spriteset_map_dispose
  402.     end
  403.     #--------------------------------------------------------------------------
  404.     # * Free Fog Plane
  405.     #--------------------------------------------------------------------------
  406.     def dispose_fogs
  407.         @fog_planes.compact.each {|fog| fog.dispose }
  408.     end
  409.     #--------------------------------------------------------------------------
  410.     # * Frame Update
  411.     #--------------------------------------------------------------------------
  412.     alias shaz_multi_fog_spriteset_map_update update
  413.     def update
  414.         update_fogs
  415.         shaz_multi_fog_spriteset_map_update
  416.     end
  417.     #--------------------------------------------------------------------------
  418.     # * Update Fogs
  419.     #--------------------------------------------------------------------------
  420.     def update_fogs
  421.         return if !@fog_planes
  422.         $game_map.screen.fogs.each do |fog|
  423.             @fog_planes[fog.number] ||= Plane_Fog.new(@viewport1, fog)
  424.             @fog_planes[fog.number].update
  425.         end
  426.     end
  427. end
  428.  
  429. if BATTLE_FOGS
  430.     class Spriteset_Battle
  431.         #--------------------------------------------------------------------------
  432.         # * Object Initialization
  433.         #--------------------------------------------------------------------------
  434.         alias shaz_multi_fog_spriteset_battle_initialize initialize
  435.         def initialize
  436.             shaz_multi_fog_spriteset_battle_initialize
  437.             create_fogs
  438.             update
  439.         end
  440.         #--------------------------------------------------------------------------
  441.         # * Create Fog Plane
  442.         #--------------------------------------------------------------------------
  443.         def create_fogs
  444.             @fog_planes = []
  445.         end
  446.         #--------------------------------------------------------------------------
  447.         # * Free
  448.         #--------------------------------------------------------------------------
  449.         alias shaz_multi_fog_spriteset_map_dispose dispose
  450.         def dispose
  451.             dispose_fogs
  452.             shaz_multi_fog_spriteset_map_dispose
  453.         end
  454.         #--------------------------------------------------------------------------
  455.         # * Free Fog Plane
  456.         #--------------------------------------------------------------------------
  457.         def dispose_fogs
  458.             @fog_planes.compact.each {|fog| fog.dispose }
  459.         end
  460.         #--------------------------------------------------------------------------
  461.         # * Frame Update
  462.         #--------------------------------------------------------------------------
  463.         alias shaz_multi_fog_spriteset_map_update update
  464.         def update
  465.             update_fogs
  466.             shaz_multi_fog_spriteset_map_update
  467.         end
  468.         #--------------------------------------------------------------------------
  469.         # * Update Fogs
  470.         #--------------------------------------------------------------------------
  471.         def update_fogs
  472.             return if !@fog_planes
  473.             $game_map.screen.update_fogs
  474.             $game_map.screen.fogs.each do |fog|
  475.                 @fog_planes[fog.number] ||= Plane_Fog.new(@viewport1, fog)
  476.                 @fog_planes[fog.number].update
  477.             end
  478.         end
  479.     end
  480. end # BATTLE_FOGS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement