TheoAllen

Theo - Fog Screen v2 ENG

May 14th, 2014
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 16.64 KB | None | 0 0
  1. # =============================================================================
  2. # TheoAllen - Fog Screen
  3. # Version : 2.0
  4. # Contact : www.rpgmakerid.com (or) http://theolized.blogspot.com
  5. # (English documentation)
  6. # =============================================================================
  7. ($imported ||={})[:Theo_FogScreen] = true
  8. # =============================================================================
  9. # CHANGE LOGS:
  10. # -----------------------------------------------------------------------------
  11. # 2013.08.24 - Rewrite script with very different workflows (v2.0)
  12. #            - Multiple fogs support
  13. #            - Now support very slow scrolling speed (sekitar 0.1)
  14. #            - Fog not scrolled alongside player
  15. #            - Now support battle fog
  16. #            - Change map notetag
  17. #            - Remove opacity variance
  18. # 2013.06.13 - Added global fog (v1.3)
  19. #            - Added global disable switch
  20. #            - Added specific disable switch
  21. # 2013.06.12 - Bugfix. Where fog isn't disposed when changing map
  22. # 2013.05.15 - Added blend type
  23. #            - Added zoom
  24. #            - Added opacity variance
  25. # 2013.05.09 - Started and Finished script (v1.0)
  26. # =============================================================================
  27. =begin
  28.  
  29.   -----------------------------------------------------------------------------
  30.   Opening :
  31.   This is a very different fog script than version 1.3. The usage and workflows
  32.   are really not same. So, if you use my previous fog screen, you should remove
  33.   and change to this one. I'm sorry if you need to re-setting from start
  34.  
  35.   -----------------------------------------------------------------------------
  36.   Introduction :
  37.   This script simply displays for as RMXP have.
  38.  
  39.   -----------------------------------------------------------------------------
  40.   How to use :
  41.   Put this script below material but above main
  42.   Edit the database configurations for used fogs
  43.   To display and remove fogs, make a script call.
  44.  
  45.   To display :
  46.   add_fog(key)
  47.   add_fog(key,fadein)
  48.  
  49.   To remove :
  50.   delete_fog(key)
  51.   delete_fog(key,fadeout)
  52.  
  53.   Note :
  54.   - key is a hash key from fog database below.
  55.   - fadein/fadeout is its fading speed. i.e if you set it to 5, then in every
  56.     frame, its opacity will be increased by 5 until its max value which have
  57.     you specified. If you ommit fadein/fadeout parameter, it will be instantly
  58.     displayed
  59.   - You can't call two or more fogs which has also same hash key.
  60.  
  61.   clear_fogs
  62.   ^
  63.   If you want to clear all the fogs
  64.  
  65.   -----------------------------------------------------------------------------
  66.   Notetag :
  67.   The notetag is quite simple. all you need it to put this notetag to map
  68.   properties
  69.  
  70.   <add fog: key>
  71.  
  72.   -----------------------------------------------------------------------------
  73.   Terms of use :
  74.   Credit me, TheoAllen. You are free to edit this script by your own. As long
  75.   as you don't claim it yours. For commercial purpose, don't forget to give me
  76.   a free copy of the game.
  77.  
  78. =end
  79. # =============================================================================
  80. # Configuration :
  81. # =============================================================================
  82. module THEO
  83.   module Fog
  84.   # -------------------------------------------------------------------------  
  85.   # Include it in battle scene?
  86.   # -------------------------------------------------------------------------  
  87.     BattleFog = true  
  88.   # Set to true if you also want to display fogs in battle scene
  89.    
  90.   # -------------------------------------------------------------------------
  91.   # Fog Database List ~
  92.   # Level : Easy
  93.   # -------------------------------------------------------------------------  
  94.   # Configuration guides
  95.   #
  96.   # key     => Hash key used to call your fog (In both script call and notetag)
  97.   # name    => Fog image filename that must be existed in Graphics/Pictures
  98.   # opacity => For opacity (0 - 255)
  99.   # speed_x => Horizontal scroll speed
  100.   # speed_y => Vertical scroll speed
  101.   # zoom_x  => Horizontal zoom scale (1.0 is normal size)
  102.   # zoom_y  => Vertical zoom scale (1.0 is normal size)
  103.   # -------------------------------------------------------------------------  
  104.     List = {
  105.     # "key"   => ["name", opacity, speed_x, speed_y, zoom_x, zoom_y]
  106.       "fog"   => ["fog",      128,     0.2,     0.2,    1.0,    1.0],
  107.       "cloud" => ["cloud",     90,    -0.2,    -0.2,    2.0,    2.0],
  108.       "storm" => ["storm",     90,    -2.0,     2.0,    1.0,    1.0],
  109.       "leaf"  => ["leaf",      90,       0,       0,    2.0,    2.0],
  110.     # Add more
  111.    
  112.     } # <-- Don touch ~ !
  113.   # -------------------------------------------------------------------------  
  114.   # Fog Extended Database List
  115.   # Level : Hard
  116.   # -------------------------------------------------------------------------
  117.   # There're more configurations besides those above. But it just optional.
  118.   # For reason of neatness, I would like to split it up
  119.   #
  120.   # Configuration guide :
  121.   # Start it by write like this. Key is a hash key, remember
  122.   # fog = fog_data["key"]
  123.   #
  124.   # Then change the attributes :
  125.   # fog = fog_data["fog"]
  126.   # fog.name = "something"
  127.   # fog.opacity = 100
  128.   #
  129.   # Here is the list of the unlisted attributes beside the previous config
  130.   #
  131.   # fog.switch      >> Switch ID. If the switch is turned ON, it will be invis
  132.   # fog.blend_type  >> Type of blending. Choose between 0 - 2 (default : 0)
  133.   # fog.tone        >> Same as tint screen. Fill it with Tone.new(R,G,B,Gray)
  134.   # fog.z           >> Z Coordinate. Greater value will displays the fog over
  135.   #                    the other fogs. Default value is 250
  136.   #
  137.   # For scale scroll
  138.   # fog.scroll_scale_x >> Horizontal scale
  139.   # fog.scroll_scale_y >> Vertical scale
  140.   #
  141.   # These are for scrolling scale. If you put them in 0.0, then the fog will
  142.   # scrolled alongside the player, i.e the fog will followed the player. The
  143.   # default value is 1.0
  144.   # -------------------------------------------------------------------------
  145.     def self.custom_fogs
  146.       fog = fog_data["leaf"]
  147.       fog.scroll_scale_x = 0.4
  148.       fog.scroll_scale_y = 0.4
  149.      
  150.       fog = fog_data["rain1"]
  151.       fog.blend_type = 1
  152.      
  153.       fog = fog_data["rain2"]
  154.       fog.blend_type = 2
  155.     end
  156.    
  157.   end
  158. end
  159. # =============================================================================
  160. # End of config. You need scripting skill to edit below this line.
  161. # =============================================================================
  162. module THEO
  163.   module Fog
  164.     def self.load_fogs
  165.       List.each do |key,data|
  166.         fog = fog_data[key]
  167.         fog.name = data[0]
  168.         fog.opacity = data[1]
  169.         fog.speed_x = data[2]
  170.         fog.speed_y = data[3]
  171.         fog.zoom_x = data[4]
  172.         fog.zoom_y = data[5]
  173.       end
  174.     end
  175.    
  176.     def self.fog_data
  177.       $game_temp.fogs
  178.     end    
  179.   end
  180. end
  181. # =============================================================================
  182. # ▼ DataManager
  183. # =============================================================================
  184. class << DataManager
  185.  
  186.   alias theo_fog_create_obj create_game_objects
  187.   def create_game_objects
  188.     theo_fog_create_obj
  189.     THEO::Fog.load_fogs
  190.     THEO::Fog.custom_fogs
  191.   end
  192.  
  193. end
  194. # =============================================================================
  195. # ▼ DataFogs
  196. # =============================================================================
  197. class DataFogs
  198.  
  199.   def initialize
  200.     @data = {}
  201.   end
  202.  
  203.   def [](key)
  204.     @data[key] ||= Fog.new(key)
  205.   end
  206.  
  207. end
  208. # =============================================================================
  209. # ▼ Fogs
  210. # =============================================================================
  211. class Fog
  212.   attr_accessor :key
  213.   attr_accessor :name
  214.   attr_accessor :opacity
  215.   attr_accessor :speed_x
  216.   attr_accessor :speed_y
  217.   attr_accessor :zoom_x
  218.   attr_accessor :zoom_y
  219.   attr_accessor :scroll_scale_x
  220.   attr_accessor :scroll_scale_y
  221.   attr_accessor :blend_type
  222.   attr_accessor :tone
  223.   attr_accessor :switch
  224.   attr_accessor :z
  225.  
  226.   def initialize(key)
  227.     @key = key
  228.     @name = ""
  229.     @opacity = 255
  230.     @speed_x = 0.0
  231.     @speed_y = 0.0
  232.     @zoom_x = 1.0
  233.     @zoom_y = 1.0
  234.     @scroll_scale_x = 1.0
  235.     @scroll_scale_y = 1.0
  236.     @blend_type = 0
  237.     @tone = Tone.new
  238.     @switch = 0
  239.     @z = 250
  240.   end
  241.  
  242.   def visible
  243.     !$game_switches[@switch]
  244.   end
  245.  
  246. end
  247. # =============================================================================
  248. # ▼ Game_Temp
  249. # =============================================================================
  250. class Game_Temp
  251.   attr_accessor :clear_fog
  252.   attr_reader :fogs
  253.  
  254.   # --------------------------------------------------------------------------
  255.   # They said that too much global variables isn't good. So, I used an
  256.   # instance variable to store my fogs database =P
  257.   # --------------------------------------------------------------------------
  258.   alias theo_fog_init initialize
  259.   def initialize
  260.     theo_fog_init
  261.     @fogs = DataFogs.new
  262.     @clear_fog = false
  263.   end
  264.  
  265. end
  266. # =============================================================================
  267. # ▼ Game_System
  268. # =============================================================================
  269. class Game_System
  270.   attr_reader :used_fog
  271.  
  272.   alias theo_fog_init initialize
  273.   def initialize
  274.     theo_fog_init
  275.     @used_fog = []
  276.   end
  277.  
  278. end
  279. # =============================================================================
  280. # ▼ Game_Interpreter
  281. # =============================================================================
  282. class Game_Interpreter
  283.  
  284.   def clear_fogs
  285.     $game_system.used_fog.clear
  286.     $game_temp.clear_fog = true
  287.     Fiber.yield
  288.   end
  289.  
  290.   def get_fog(key)
  291.     plane = planefogs
  292.     return nil unless plane
  293.     plane.get_fog(key)
  294.   end
  295.  
  296.   def planefogs
  297.     scene = SceneManager.scene
  298.     spriteset = scene.instance_variable_get("@spriteset")
  299.     return spriteset.instance_variable_get("@fogs")
  300.   end
  301.  
  302.   def delete_fog(key,speed = 255)
  303.     fog = get_fog(key)
  304.     return unless fog
  305.     $game_system.used_fog.delete(key)
  306.     fog.fadeout(speed)
  307.     fog.fade_delete = true
  308.   end
  309.  
  310.   def add_fog(key,speed = 255)
  311.     return if $game_system.used_fog.include?(key)
  312.     $game_system.used_fog.push(key)
  313.     fog = planefogs[key]
  314.     fog.fadein(speed)
  315.   end
  316.  
  317. end
  318. # =============================================================================
  319. # ▼ Game_Map
  320. # =============================================================================
  321. class Game_Map
  322.   attr_accessor :used_fogs
  323.  
  324.   alias theo_fog_init initialize
  325.   def initialize
  326.     theo_fog_init
  327.     @used_fogs = []
  328.   end
  329.  
  330.   alias theo_fog_setup setup
  331.   def setup(map_id)
  332.     theo_fog_setup(map_id)
  333.     setup_fogs
  334.   end
  335.  
  336.   def setup_fogs
  337.     @used_fogs = []
  338.     @map.note.split(/[\r\n]+/).each do |line|
  339.       case line
  340.       when /<(?:ADD_FOG|add fog): (.*)>/i
  341.         key = $1.to_s
  342.         next if @used_fogs.include?(key)
  343.         @used_fogs.push(key)
  344.       end
  345.     end
  346.   end
  347.  
  348. end
  349. # =============================================================================
  350. # ▼ PlaneFog
  351. # =============================================================================
  352. class PlaneFog < Plane
  353.   attr_accessor :fade_delete
  354.   attr_accessor :key
  355.  
  356.   def initialize(key, viewport)
  357.     super(viewport)
  358.     @real_ox = self.ox.to_f + rand(Graphics.width)
  359.     @real_oy = self.oy.to_f + rand(Graphics.height)
  360.     load_data(key)
  361.     update_oxoy
  362.     @fade = 0
  363.     @fade_delete = false
  364.   end
  365.  
  366.   def load_data(key)
  367.     @data = THEO::Fog.fog_data[key]
  368.     self.bitmap = Cache.picture(@data.name)
  369.     @data.instance_variables.each do |varsymb|
  370.       ivar_name = varsymb.to_s.gsub(/@/){""}
  371.       eval("
  372.      if self.respond_to?(\"#{ivar_name}\")
  373.        self.#{ivar_name} = @data.#{ivar_name}
  374.      end
  375.      ")
  376.     end
  377.   end
  378.  
  379.   def fadeout(speed)
  380.     @fade = -speed
  381.   end
  382.  
  383.   def fadein(speed)
  384.     @fade = speed
  385.     self.opacity = 0
  386.   end
  387.  
  388.   def spd_x
  389.     @data.speed_x
  390.   end
  391.  
  392.   def spd_y
  393.     @data.speed_y
  394.   end
  395.  
  396.   def update
  397.     update_real_oxoy
  398.     update_oxoy
  399.     update_visible
  400.     update_fade
  401.   end
  402.  
  403.   def update_real_oxoy
  404.     @real_ox += spd_x
  405.     @real_oy += spd_y
  406.   end
  407.  
  408.   def update_oxoy
  409.     self.ox = fog_display_x + @real_ox
  410.     self.oy = fog_display_y + @real_oy
  411.   end
  412.  
  413.   def update_fade
  414.     self.opacity = [[opacity + @fade,0].max,max_opacity].min
  415.   end
  416.  
  417.   def max_opacity
  418.     @data.opacity
  419.   end
  420.  
  421.   def fog_display_x
  422.     $game_map.display_x * (32.0 * @data.scroll_scale_x)
  423.   end
  424.  
  425.   def fog_display_y
  426.     $game_map.display_y * (32.0 * @data.scroll_scale_y)
  427.   end
  428.  
  429.   def update_visible
  430.     self.visible = @data.visible
  431.   end
  432.  
  433. end
  434. # =============================================================================
  435. # ▼ PlaneFogs
  436. # =============================================================================
  437. class PlaneFogs
  438.  
  439.   def initialize(viewport)
  440.     @data = {}
  441.     @viewport = viewport
  442.     init_used_fog
  443.   end
  444.  
  445.   def init_used_fog
  446.     $game_system.used_fog.each do |fogname|
  447.       self[fogname]
  448.     end
  449.   end
  450.  
  451.   def get_fog(key)
  452.     @data[key]
  453.   end
  454.  
  455.   def delete(key)
  456.     fog = @data[key]
  457.     return unless fog
  458.     fog.dispose
  459.     @data.delete(key)
  460.   end
  461.  
  462.   def [](key)
  463.     @data[key] ||= PlaneFog.new(key, @viewport)
  464.   end
  465.  
  466.   def update
  467.     update_basic
  468.     @data.values.each {|fog| fog.update unless fog.disposed?}
  469.   end
  470.  
  471.   def update_basic
  472.     update_delete
  473.     update_clear
  474.   end
  475.  
  476.   def update_delete
  477.     @data.values.each do |fog|
  478.       next unless fog.fade_delete && fog.opacity == 0
  479.       delete(fog.key)
  480.       $game_system.used_fog.delete(fog.key)
  481.     end
  482.   end
  483.  
  484.   def update_clear
  485.     if $game_temp.clear_fog
  486.       @data.keys.each do |key|
  487.         delete(key)
  488.       end
  489.       $game_temp.clear_fog = false
  490.     end
  491.   end
  492.  
  493.   def dispose
  494.     @data.values.each {|fog| fog.dispose}
  495.   end
  496.  
  497. end
  498. # =============================================================================
  499. # ▼ MapFogs
  500. # =============================================================================
  501. class Mapfogs < PlaneFogs
  502.  
  503.   def init_used_fogs
  504.     @used_fogs = $game_map.used_fogs.dup
  505.     @used_fogs.each do |fogname|
  506.       self[fogname]
  507.     end
  508.   end
  509.  
  510.   def update_basic
  511.     update_used_fog
  512.   end
  513.  
  514.   def update_used_fog
  515.     if @used_fogs != $game_map.used_fogs
  516.       delete_all
  517.       init_used_fogs
  518.     end
  519.   end
  520.  
  521.   def delete_all
  522.     @data.values.each do |fog|
  523.       delete(fog.key)
  524.     end
  525.   end
  526.  
  527. end
  528. # =============================================================================
  529. # ▼ Spriteset_Map
  530. # =============================================================================
  531. class Spriteset_Map
  532.  
  533.   alias theo_fog_viewports create_viewports
  534.   def create_viewports
  535.     theo_fog_viewports
  536.     create_mapfogs
  537.     create_global_fogs
  538.   end
  539.  
  540.   def create_mapfogs
  541.     @mapfogs = Mapfogs.new(@viewport1)
  542.   end
  543.  
  544.   def create_global_fogs
  545.     @fogs = PlaneFogs.new(@viewport1)
  546.   end
  547.  
  548.   alias theo_fog_update update
  549.   def update
  550.     theo_fog_update
  551.     update_global_fogs
  552.     update_mapfogs
  553.   end
  554.  
  555.   def update_global_fogs
  556.     @fogs.update
  557.   end
  558.  
  559.   def update_mapfogs
  560.     @mapfogs.update
  561.   end
  562.  
  563.   alias theo_fog_dispose dispose
  564.   def dispose
  565.     theo_fog_dispose
  566.     dispose_global_fogs
  567.     dispose_mapfogs
  568.   end
  569.  
  570.   def dispose_global_fogs
  571.     @fogs.dispose
  572.   end
  573.  
  574.   def dispose_mapfogs
  575.     @mapfogs.dispose    
  576.   end
  577.  
  578. end
  579. # =============================================================================
  580. # ▼ Spriteset_Battle
  581. # =============================================================================
  582. class Spriteset_Battle
  583.  
  584.   def use_fog?
  585.     THEO::Fog::BattleFog
  586.   end
  587.  
  588.   alias theo_fog_viewports create_viewports
  589.   def create_viewports
  590.     theo_fog_viewports
  591.     return unless use_fog?
  592.     create_mapfogs
  593.     create_global_fogs
  594.   end
  595.  
  596.   def create_mapfogs
  597.     @mapfogs = Mapfogs.new(@viewport1)
  598.   end
  599.  
  600.   def create_global_fogs
  601.     @fogs = PlaneFogs.new(@viewport1)
  602.   end
  603.  
  604.   alias theo_fog_update update
  605.   def update
  606.     theo_fog_update
  607.     return unless use_fog?
  608.     update_global_fogs
  609.     update_mapfogs
  610.   end
  611.  
  612.   def update_global_fogs
  613.     @fogs.update
  614.   end
  615.  
  616.   def update_mapfogs
  617.     @mapfogs.update
  618.   end
  619.  
  620.   alias theo_fog_dispose dispose
  621.   def dispose
  622.     theo_fog_dispose
  623.     return unless use_fog?
  624.     dispose_global_fogs
  625.     dispose_mapfogs
  626.   end
  627.  
  628.   def dispose_global_fogs
  629.     @fogs.dispose
  630.   end
  631.  
  632.   def dispose_mapfogs
  633.     @mapfogs.dispose    
  634.   end
  635.  
  636. end
Advertisement
Add Comment
Please, Sign In to add comment