Advertisement
DrDhoom

[RGSS2] Parallax Utils v2.0

Dec 1st, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.56 KB | None | 0 0
  1. #===============================================================================
  2. # Parallax Utils v2.0 (VX)
  3. #   by DrDhoom
  4. #   drd-workshop.blogspot.com
  5. #===============================================================================
  6.  
  7. module Dhoom
  8.   module Parallax_Utils  
  9.     PLayer = {} #Don't Change This Line!
  10.     MAP    = [] #Don't Change This Line!
  11. #-------------------------------------------------------------------------------
  12. # * :name
  13. # Filename format for that layer. If layer isn't animated, %d is Map ID.
  14. # If layer is animated, %1$d is Map ID and %2$d is layer frame.
  15. #-------------------------------------------------------------------------------
  16. # * :parz
  17. # Layer Z value.
  18. # Normal parallax Z = -100
  19. # Tileset, Event, and Player Z = 0
  20. # Weather Z = 50
  21. #-------------------------------------------------------------------------------
  22. # * :lockx
  23. # Lock layer X coordinate to Map X coordinate.
  24. #-------------------------------------------------------------------------------
  25. # * :locky
  26. # Lock layer Y coordinate to Map Y coordinate.
  27. #-------------------------------------------------------------------------------
  28. # * :scrollx
  29. # Scroll X speed. Doesn't work if :lockx is true.
  30. #-------------------------------------------------------------------------------
  31. # * :scrolly
  32. # Scroll Y speed. Doesn't work if :locky is true.
  33. #-------------------------------------------------------------------------------
  34. # * :autoscrollx
  35. # Auto scroll X for each frame. Doesn't work if :lockx is true.
  36. #-------------------------------------------------------------------------------
  37. # * :autoscrolly
  38. # Auto scroll Y for each frame. Doesn't work if :locky is true.
  39. #-------------------------------------------------------------------------------
  40. # * :blending
  41. # Set layer blending mode.
  42. # 0 = Normal
  43. # 1 = Addition/Overlay
  44. # 2 = Subtraction/Multiply
  45. #-------------------------------------------------------------------------------
  46. # * :switch
  47. # Layer will only be shown if the switch is ON. Set to false to make it always
  48. # shown.
  49. #-------------------------------------------------------------------------------
  50. # * :animated
  51. # Set to true if you want this layer to be animated.
  52. #-------------------------------------------------------------------------------
  53. # * :animation_wait
  54. # Set how long this layer to wait for each frame. This will only be used
  55. # if :animated is true.
  56. #-------------------------------------------------------------------------------    
  57.     PLayer["normal"] = {
  58.       :name => "%d_normal",
  59.       :parz => -100,
  60.       :lockx => true,
  61.       :locky => true,
  62.       :scrollx => 0,
  63.       :scrolly => 0,
  64.       :autoscrollx => 0,
  65.       :autoscrolly => 0,
  66.       :blending => 0,
  67.       :switch => false,
  68.       :animated => false,
  69.       :animation_wait => 0,
  70.     }
  71.    
  72.     PLayer["normalanimated"] = {
  73.       :name => "%1$d_normal_anim_%2$d",
  74.       :parz => -99,
  75.       :lockx => true,
  76.       :locky => true,
  77.       :scrollx => 0,
  78.       :scrolly => 0,
  79.       :blending => 0,
  80.       :switch => false,
  81.       :animated => true,
  82.       :animation_wait => 20,
  83.     }
  84.    
  85.     PLayer["overlay"] = {
  86.       :name => "%d_overlay",
  87.       :parz => 100,
  88.       :lockx => true,
  89.       :locky => true,
  90.       :scrollx => 0,
  91.       :scrolly => 0,
  92.       :blending => 1,
  93.       :switch => false,
  94.       :animated => false,
  95.       :animation_wait => 0,
  96.     }    
  97.    
  98. #-------------------------------------------------------------------------------
  99. # MAP[Map ID] = [PLayer Name, PLayer Name, ...]
  100. #-------------------------------------------------------------------------------
  101.     MAP[4] = ["normal","normalanimated","overlay"]
  102.   end
  103. end
  104.  
  105. class Game_Map
  106.  
  107.   include Dhoom::Parallax_Utils
  108.  
  109.   attr_reader   :parallax_layer
  110.   attr_reader   :parallax_frame
  111.   attr_reader   :parallax_current_frame
  112.   attr_reader   :parallax_lx
  113.   attr_reader   :parallax_ly
  114.   attr_reader   :parallax_lsx
  115.   attr_reader   :parallax_lsy
  116.  
  117.   alias dhoom_parutils_gmmap_calc_parallax_x calc_parallax_x
  118.   def calc_parallax_x(bitmap, name = nil)
  119.     return @display_x / 8 if !name.nil? && PLayer[name] && PLayer[name][:lockx]
  120.     return dhoom_parutils_gmmap_calc_parallax_x(bitmap)
  121.   end
  122.  
  123.   alias dhoom_parutils_gmmap_calc_parallax_y calc_parallax_y
  124.   def calc_parallax_y(bitmap, name = nil)
  125.     return @display_y / 8 if !name.nil? && PLayer[name] && PLayer[name][:locky]
  126.     return dhoom_parutils_gmmap_calc_parallax_y(bitmap)
  127.   end
  128.  
  129.   alias dhoom_parutils_gmmap_setup setup
  130.   def setup(map_id)
  131.     dhoom_parutils_gmmap_setup(map_id)
  132.     setup_layer_parallax
  133.   end
  134.  
  135.   def setup_layer_parallax
  136.     @parallax_layer = {}
  137.     @parallax_frame = {}
  138.     @parallax_current_frame = {}
  139.     @parallax_lx = {}
  140.     @parallax_ly = {}
  141.     @parallax_lsx = {}
  142.     @parallax_lsy = {}
  143.     @wait = {}
  144.     return if MAP[@map_id].nil?
  145.     MAP[@map_id].each do |name|
  146.       if PLayer[name][:animated]
  147.         @parallax_layer[name] = sprintf(PLayer[name][:name], @map_id, 1)
  148.         @parallax_frame[name] = get_frame(name)
  149.       else
  150.         @parallax_layer[name] = sprintf(PLayer[name][:name], @map_id)
  151.         @parallax_frame[name] = 1
  152.       end
  153.       @parallax_current_frame[name] = 1
  154.       @parallax_lx[name] = 0
  155.       @parallax_ly[name] = 0
  156.       @parallax_lsx[name] = PLayer[name][:autoscrollx]
  157.       @parallax_lsy[name] = PLayer[name][:autoscrolly]
  158.       @wait[name] = PLayer[name][:animation_wait]
  159.     end
  160.   end
  161.  
  162.   def get_frame(name)
  163.     frame = 1
  164.     text = sprintf(PLayer[name][:name], @map_id, frame)
  165.     while FileTest.exist?("Graphics/Parallaxes/#{text}.png") do
  166.       frame += 1
  167.       text = sprintf(PLayer[name][:name], @map_id, frame)
  168.     end
  169.     frame -= 1
  170.     return frame
  171.   end
  172.  
  173.   def get_parallaxes_name(name)
  174.     if PLayer[name][:animated]
  175.       return sprintf(PLayer[name][:name], @map_id, @parallax_current_frame[name])
  176.     else
  177.       return sprintf(PLayer[name][:name], @map_id)
  178.     end
  179.   end
  180.  
  181.   alias dhoom_parutils_gmmap_update_parallax update_parallax
  182.   def update_parallax
  183.     dhoom_parutils_gmmap_update_parallax
  184.     MAP[@map_id].each do |name|      
  185.       @parallax_lx[name] += @parallax_lsx[name] * 4 unless PLayer[name][:lockx]
  186.       @parallax_ly[name] += @parallax_lsy[name] * 4 unless PLayer[name][:locky]
  187.       if PLayer[name][:animated]
  188.         @wait[name] -= 1
  189.         if @wait[name] == 0
  190.           @parallax_current_frame[name] += 1
  191.           @parallax_current_frame[name] = 1 if @parallax_current_frame[name] > @parallax_frame[name]
  192.           @wait[name] = PLayer[name][:animation_wait]
  193.         end
  194.       end
  195.     end    
  196.   end
  197. end
  198.  
  199. class Spriteset_Map
  200.  
  201.   include Dhoom::Parallax_Utils
  202.  
  203.   alias dhoom_parutils_sprsmap_create_parallax create_parallax
  204.   def create_parallax
  205.     dhoom_parutils_sprsmap_create_parallax
  206.     @parallax_viewports = {}
  207.     @parallaxes_names = {}
  208.     @parallaxes = {}
  209.     $game_map.parallax_layer.each_key do |name|
  210.       @parallax_viewports[name] = Viewport.new(0, 0, 544, 416)
  211.       @parallax_viewports[name].z = PLayer[name][:parz]
  212.       @parallaxes[name] = Plane.new(@parallax_viewports[name])
  213.       @parallaxes[name].blend_type = PLayer[name][:blending]
  214.     end
  215.   end
  216.  
  217.   alias dhoom_parutils_sprsmap_update update
  218.   def update
  219.     dhoom_parutils_sprsmap_update
  220.     update_parallaxes
  221.   end
  222.  
  223.   def update_parallaxes
  224.     @parallaxes.each_key do |name|
  225.       if @parallaxes_names[name] != $game_map.get_parallaxes_name(name)
  226.         @parallaxes_names[name] = $game_map.get_parallaxes_name(name)
  227.         @parallaxes[name].bitmap = Cache.parallax(@parallaxes_names[name])        
  228.       end
  229.       @parallaxes[name].ox = $game_map.calc_parallax_x(@parallaxes[name].bitmap, name)
  230.       @parallaxes[name].oy = $game_map.calc_parallax_y(@parallaxes[name].bitmap, name)
  231.       @parallaxes[name].visible = PLayer[name][:switch] == false ? true : $game_switches[PLayer[name][:switch]]
  232.     end
  233.   end  
  234.  
  235.   alias dhoom_parutils_sprsmap_dispose_parallax dispose_parallax
  236.   def dispose_parallax
  237.     dhoom_parutils_sprsmap_dispose_parallax
  238.     @parallaxes.each_value do |parallax|
  239.       parallax.dispose unless parallax.nil?
  240.     end
  241.   end
  242.  
  243.   alias dhoom_parutils_sprsmap_update_viewports update_viewports
  244.   def update_viewports
  245.     dhoom_parutils_sprsmap_update_viewports
  246.     @parallax_viewports.each_value do |viewport|
  247.       viewport.ox = $game_map.screen.shake
  248.     end
  249.   end
  250.  
  251.   alias dhoom_parutils_sprsmap_dispose_viewports dispose_viewports
  252.   def dispose_viewports
  253.     dhoom_parutils_sprsmap_dispose_viewports
  254.     @parallax_viewports.each_value do |viewport|
  255.       viewport.dispose
  256.     end
  257.   end
  258. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement