Advertisement
Rafael_Sol_Maker

RAFAEL_SOL_MAKER's ACE PERFECT FOG v1.0

Dec 18th, 2011
9,436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 12.26 KB | None | 0 0
  1. #===============================================================================
  2. #                 RAFAEL_SOL_MAKER's ACE PERFECT FOG v1.0
  3. #_______________________________________________________________________________
  4. #   Description  | Adds a fog effect, identical to the RPG Maker XP fog, in
  5. #                | RPG Maker VX maps, with also similar functionality.
  6. #                | It's just a port of my VX's version. Enough said.
  7. #________________|______________________________________________________________
  8. #     Usage      | To show the fog, type this command in a 'Script...'
  9. #                | event command:
  10. #                |  
  11. #                |    setup_fog("filename", hue, opacity, blend_type, zoom,
  12. #                |              speed_x, speed_y, visible?)
  13. #                |
  14. #                |   Where:      Is equivalent to:
  15. #                | filename    > Name of the bitmap used in the fog effect.
  16. #                | hue         > Hue(Color). Use a value between 0 and 360.
  17. #                | opacity     > Opacity. Use a value between 0 and 255.
  18. #                | blend_type  > Bitmap alpha blend mode. See values below.
  19. #                | zoom        > Zoom, in scale. Decimal values are allowed.
  20. #                | speed_x     > Horizontal speed, in pixels. Negatives allowed.
  21. #                | speed_y     > Vertical speed, in pixels. Negatives allowed.
  22. #                | visible?    > Visibility of the fog. Use 'true' or 'false'.
  23. #                |
  24. #              > | Place all fog graphics in the folder 'Graphics/Fogs/'.
  25. #                |
  26. #              > | The parameter 'blend_type' accepts three values:
  27. #                | BLEND_NORMAL, BLEND_ADD and BLEND_SUB,  for the use of
  28. #                | normal, addition and subtraction blend modes, respectively.
  29. #                |
  30. #              > | Note: All parameters are optional; default values will be
  31. #                | used, if omitted. It's possible to omit only the parameteres
  32. #                | you want. To do this, just type 'nil' in its place instead.
  33. #                |
  34. #              > | You can set the default values in the general configurations
  35. #                | module. Now let's see the other commands:
  36. #                |
  37. #                |     change_fog_tone(tone, [duration])
  38. #                |     change_fog_opacity(opacity, [duration])
  39. #                |
  40. #              > | To change the hue, and opacity of the fog, respectively.
  41. #                | In 'tone', you will need to use a 'Tone' object:
  42. #                |   Tone.new(red, green, blue, [gray])
  43. #                | Where colors accept values of -255 to 255 and gray 0 to 255.
  44. #                | In opacity use a value between 0 and 255 too.
  45. #                | The value of 'duration' is optional, use a value in frames.
  46. #                | If the value is omitted the transition will be immediate.
  47. #                |
  48. #                |     show_fog
  49. #                |     hide_fog
  50. #                |
  51. #              > | Use these commands to show and hide the fog, respectively.
  52. #________________|______________________________________________________________
  53. # Specifications | Difficulty to Use:
  54. #                |  * * ½ (some commands may need little scripting knowledge)
  55. #                | Scripting Difficulty:
  56. #                |  * * (required some graphics/Game engine knowledge)
  57. #                | Compatibility:
  58. #                |  * * * *(probably highly compatible)
  59. #                | New Methods:
  60. #                |  - (many, and a brand new Game class, see in the code below)
  61. #                | Overwritten Methods:
  62. #                |  - (none)
  63. #                | Aliased Methods:
  64. #                |  - Game_Map.setup
  65. #                |  - Game_Map.update
  66. #                |  - Spriteset_Map.initialize
  67. #                |  - Spriteset_Map.update
  68. #                |  - Spriteset_Map.dispose
  69. #________________|______________________________________________________________
  70. # Special Thanks | Miget man12, Woratana
  71. #________________|______________________________________________________________
  72. #===============================================================================
  73.  
  74. #_______________________________________________________________________________
  75. #     SCRIPT CONFIGURATIONS - Some adjusts that you can do without problems.
  76. #_______________________________________________________________________________
  77. #===============================================================================
  78.  
  79. module PPVXAce_General_Configs
  80.   Fog_Filename = 'Fog01'  # Bitmap name
  81.   Fog_Hue = 0             # Hue(Colour)
  82.   Fog_Opacity = 128       # Opacity
  83.   Fog_Blend_Type = 1      # Blend Mode
  84.   Fog_Zoom = 1            # Zoom Scale
  85.   Fog_SpeedX = 4          # Horizontal Speed
  86.   Fog_SpeedY = 4          # Vertical Speed
  87.   Fog_Visible = true      # Visibility
  88. end
  89.  
  90. #_______________________________________________________________________________
  91. #     BEGGINING OF THE SCRIPT - Don't modify without know what you're doing!
  92. #_______________________________________________________________________________
  93. #===============================================================================
  94.  
  95. module Cache
  96.   def self.fog(filename)
  97.     load_bitmap('Graphics/Fogs/', filename)
  98.   end
  99. end
  100.  
  101. class Game_Interpreter
  102.   include PPVXAce_General_Configs  
  103.  
  104.   BLEND_NORMAL = 0  # Blend Mode: Normal
  105.   BLEND_ADD = 1     # Blend Mode: Addition
  106.   BLEND_SUB = 2     # Blend Mode: Subtraction
  107.  
  108.   #--------------------------------------------------------------------------
  109.   # Fog Initiaization
  110.   #--------------------------------------------------------------------------  
  111.   def setup_fog(filename = Fog_Filename, hue = Fog_Hue, opacity = Fog_Opacity,
  112.                 blend_type = Fog_Blend_Type, zoom = Fog_Zoom, sx = Fog_SpeedX,
  113.                 sy = Fog_SpeedY, visible = Fog_Visible)
  114.    
  115.     filename = Fog_Filename if filename.nil?
  116.     hue = Fog_Hue if hue.nil?
  117.     opacity = Fog_Opacity if opacity.nil?
  118.     blend_type = Fog_Blend_Type if blend_type.nil?
  119.     zoom = Fog_Zoom if zoom.nil?
  120.     sx = Fog_SpeedX if sx.nil?
  121.     sy = Fog_SpeedY if sy.nil?
  122.     visible = Fog_Visible if visible.nil?
  123.  
  124.     # Start the fog, use defaults if value is omitted('nil')
  125.     $game_map.setup_fog(filename, hue, opacity , blend_type, zoom, sx, sy, visible)
  126.   end
  127.  
  128.   #--------------------------------------------------------------------------
  129.   # Fog Tone
  130.   #--------------------------------------------------------------------------  
  131.   def change_fog_tone(tone, duration = 0)
  132.     # Start the changing of the color tone
  133.     $game_map.fog.start_tone_change(tone, duration)
  134.     return true
  135.   end
  136.  
  137.   #--------------------------------------------------------------------------
  138.   # Fog Opacity
  139.   #--------------------------------------------------------------------------  
  140.   def change_fog_opacity(opacity, duration = 0)
  141.     # Start the changing of the opacity level
  142.     $game_map.fog.start_opacity_change(opacity, duration)
  143.     return true
  144.   end
  145.  
  146.   #--------------------------------------------------------------------------
  147.   # Hide Fog
  148.   #--------------------------------------------------------------------------  
  149.   def hide_fog
  150.     # Make the fog invisible
  151.     $game_map.fog.visible = false
  152.     return true
  153.   end
  154.  
  155.   #--------------------------------------------------------------------------
  156.   # Show Fog
  157.   #--------------------------------------------------------------------------  
  158.   def show_fog
  159.     # Make fog visible again
  160.     $game_map.fog.visible = true
  161.     return true
  162.   end
  163.  
  164. end
  165.  
  166. class Game_Fog
  167.   attr_accessor :name
  168.   attr_accessor :hue
  169.   attr_accessor :opacity
  170.   attr_accessor :blend_type
  171.   attr_accessor :zoom
  172.   attr_accessor :sx
  173.   attr_accessor :sy
  174.   attr_accessor :visible
  175.   attr_reader   :ox
  176.   attr_reader   :oy
  177.   attr_reader   :tone
  178.  
  179.   def initialize
  180.     @name = ""
  181.     @hue = 0
  182.     @opacity = 255.0
  183.     @blend_type = 0
  184.     @zoom = 100.0
  185.     @sx = 0
  186.     @sy = 0
  187.     @ox = 0
  188.     @oy = 0
  189.     @visible = true
  190.     @tone = Tone.new(0, 0, 0, 0)
  191.     @tone_target = Tone.new(0, 0, 0, 0)
  192.     @tone_duration = 0
  193.     @opacity_duration = 0
  194.     @opacity_target = 0
  195.   end
  196.  
  197.   def setup(name, hue, opacity , blend_type, zoom, sx, sy, visible)
  198.     @name = name
  199.     @hue = hue
  200.     @opacity =  opacity
  201.     @blend_type = blend_type
  202.     @zoom = zoom
  203.     @sx = sx
  204.     @sy = sy
  205.     @visible = visible
  206.     @ox = 0
  207.     @oy = 0
  208.     @tone = Tone.new(0, 0, 0, 0)
  209.     @tone_target = Tone.new(0, 0, 0, 0)
  210.     @tone_duration = 0
  211.     @opacity_duration = 0
  212.     @opacity_target = 0    
  213.   end
  214.  
  215.   def start_tone_change(tone, duration)
  216.     @tone_target = tone.clone
  217.     @tone_duration = duration
  218.     if @tone_duration == 0
  219.       @tone = @tone_target.clone
  220.     end
  221.   end
  222.  
  223.   def start_opacity_change(opacity, duration)
  224.     @opacity_target = opacity * 1.0
  225.     @opacity_duration = duration
  226.     if @opacity_duration == 0
  227.       @opacity = @opacity_target
  228.     end
  229.   end
  230.  
  231.   def update
  232.     @ox -= @sx
  233.     @oy -= @sy
  234.     if @tone_duration >= 1
  235.       d = @tone_duration
  236.       target = @tone_target
  237.       @tone.set( (@tone.red   * (d - 1) + target.red)  / d,
  238.                  (@tone.green * (d - 1) + target.green)/ d,
  239.                  (@tone.blue  * (d - 1) + target.blue) / d,
  240.                  (@tone.gray  * (d - 1) + target.gray) / d )
  241.       @tone_duration -= 1
  242.     end
  243.     if @opacity_duration >= 1
  244.       d = @opacity_duration
  245.       @opacity =(@opacity *(d - 1) + @opacity_target) / d
  246.       @opacity_duration -= 1
  247.     end
  248.   end
  249.  
  250. end
  251.  
  252. class Game_Map
  253.   attr_accessor :fog
  254.  
  255.   alias solmaker_gamemap_fog_setup setup
  256.   def setup(map_id)
  257.     setup_fog_basic
  258.     solmaker_gamemap_fog_setup(map_id)    
  259.   end
  260.  
  261.   alias solmaker_gamemap_fog_update update
  262.   def update(main = false)
  263.     update_fog
  264.     solmaker_gamemap_fog_update(main)
  265.   end
  266.  
  267.   def setup_fog(name, hue, opacity, blend_type, zoom, sx, sy, visible)
  268.     visible = true if visible != true and visible != false
  269.     @fog = Game_Fog.new
  270.     @fog.setup(name.to_s, hue.to_i, opacity.to_f, blend_type.to_i,
  271.       zoom.to_f, sx.to_i, sy.to_i, visible) rescue raise(ArgumentError,
  272.       'Error during fog setup!\nPlease check the given values!')
  273.   end
  274.  
  275.   def setup_fog_basic  
  276.     @fog = Game_Fog.new
  277.   end
  278.  
  279.   def update_fog
  280.   end
  281.  
  282. end
  283.  
  284. class Spriteset_Map
  285.  
  286.   alias solmaker_fog_initialize initialize
  287.   def initialize
  288.     create_fog
  289.     solmaker_fog_initialize
  290.   end
  291.  
  292.   alias solmaker_fog_update update
  293.   def update
  294.     update_fog
  295.     solmaker_fog_update
  296.   end
  297.  
  298.   alias solmaker_fog_dispose dispose
  299.   def dispose
  300.     dispose_fog
  301.     solmaker_fog_dispose
  302.   end
  303.  
  304.   def create_fog
  305.     @plane_fog = Plane.new(@viewport1)
  306.     @plane_fog.z = 100
  307.     @temp_name = ""; @temp_hue = 0
  308.   end  
  309.  
  310.   def update_fog
  311.     $game_map.fog.update
  312.     if @temp_name != $game_map.fog.name or @temp_hue != $game_map.fog.hue
  313.       if @plane_fog.bitmap != nil
  314.         @plane_fog.bitmap.dispose
  315.         @plane_fog.bitmap = nil
  316.       end
  317.       if $game_map.fog.name != ""
  318.         @plane_fog.bitmap = Cache.fog($game_map.fog.name)
  319.         @plane_fog.bitmap.hue_change($game_map.fog.hue)
  320.       end
  321.       Graphics.frame_reset
  322.     end
  323.  
  324.     @plane_fog.opacity = $game_map.fog.opacity
  325.     @plane_fog.blend_type = $game_map.fog.blend_type
  326.     @plane_fog.zoom_x = $game_map.fog.zoom
  327.     @plane_fog.zoom_y = $game_map.fog.zoom
  328.     @plane_fog.visible = $game_map.fog.visible
  329.     @plane_fog.tone = $game_map.fog.tone
  330.    
  331.     @plane_fog.ox = ($game_map.display_x + $game_map.fog.ox) / 8.0 unless @plane_fog.nil?
  332.     @plane_fog.oy = ($game_map.display_y + $game_map.fog.oy) / 8.0 unless @plane_fog.nil?
  333.     @temp_name = $game_map.fog.name;   @temp_hue = $game_map.fog.hue
  334.   end
  335.  
  336.   def dispose_fog
  337.     # Prevents a bug while setting saturation, undoing saturation already processed
  338.     @plane_fog.bitmap.hue_change -@temp_hue unless @plane_fog.bitmap.nil?
  339.     Graphics.frame_reset
  340.     @plane_fog.dispose unless @plane_fog.nil?
  341.     @plane_fog = nil
  342.   end
  343.  
  344. end
  345.  
  346. #_______________________________________________________________________________
  347. #                  END OF THE SCRIPT - See ya next time!
  348. #_______________________________________________________________________________
  349. #===============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement