modern_algebra

Animated Parallax 2.1

Sep 9th, 2011
597
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #==============================================================================
  2. #    Animated Parallax
  3. #    Version 2.1
  4. #    Author: modern algebra (rmrk.net)
  5. #    Date: September 9, 2011
  6. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  7. #  Description:
  8. #
  9. #    This script allows you to set an animated parallax background by having
  10. #   multiple frames and switching between them at a user-defined speed. By
  11. #   default, this script only supports .png, .jpg, and .bmp file formats for
  12. #   the animated parallax panels (as they are the only ones I know RMVX
  13. #   supports).
  14. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  15. #  Instructions:
  16. #    
  17. #    The script operates by having multiple parallax backgrounds and switching
  18. #   between them at a speed set by you, unique for each map
  19. #
  20. #    Thus, if you want to use an animated parallax, you need to do a few things:
  21. #      (a) Make or find the parallax backgrounds you want to use and import
  22. #        them into your game. Then, label them all the same with the one
  23. #        distinction that at the end of each should have a _1, _2, etc...
  24. #          Example Naming:
  25. #            BlueSky_1, BlueSky_2, BlueSky_3, etc...
  26. #      (b) Set the parallax background to any given map that you want the
  27. #        animated parallaxes for. Be sure to set it to the first one you want
  28. #        in succession, so BlueSky_1, not BlueSky_2 or _3. If you do set it to
  29. #        BlueSky_2, then it will only animate between images _2 and _3.
  30. #      (c) Scroll down to the EDITABLE REGION at line 48 and follow the
  31. #        instructions for setting the animation speed
  32. #==============================================================================
  33.  
  34. $imported = {} unless $imported
  35. $imported["MAAnimatedParallax"] = true
  36. $imported["MAAnimatedParallax2.1"] = true
  37.  
  38. #==============================================================================
  39. # ** Game_Map
  40. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  41. #  Summary of Changes:
  42. #    aliased methods - update_parallax; setup_parallax
  43. #    new method - maap_check_extensions; setup_parallax_frames
  44. #==============================================================================
  45.  
  46. class Game_Map
  47.   MAAP_PARALLAX_ANIMATION_FRAMES = { # <- Don't touch
  48.   #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  49.   #    EDITABLE REGION
  50.   #|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  51.   #  MAAP_PARALLAX_ANIMATION_FRAMES - this constant allows you to set the
  52.   # speed at which the parallax switches to the next graphic in the animation
  53.   # series by individual maps. So if you want it to be every 20 frames in one
  54.   # map but every 35 in another map, this is where you do it. All you need to
  55.   # do is type in the following code:
  56.   #
  57.   #      map_id => frames,
  58.   # where map_id is the ID of the Map you want to set it for and frames is
  59.   # either (a) an integer for how many frames you want to show each panel
  60.   # before switching to the next; or (b) an array of integers where each entry
  61.   # of the array is the number of frames to keep the corresponding frame up
  62.   # before switching to the next. This allows you to vary the time each of the
  63.   # frames is left on before switching. There are 60 frames in a second.
  64.   #
  65.   #    EXAMPLES:
  66.   #      1 => 35,    Map 1 will cycle through parallax panels every 35 frames
  67.   #      2 => 40,    Map 2 will cycle through parallax panels every 40 frames
  68.   #      8 => [20, 5, 15],    Map 8 will keep the first panel of the animated
  69.   #                  parralax on for 20 frames before switching to the second
  70.   #                  panel which will be on for 5 frames before switching to
  71.   #                  the third panel which is on 15 frames before switching
  72.   #                  back to the first panel.
  73.   #
  74.   #  Note that the comma is necessary! For any maps where you use animated
  75.   # parallaxes but do not include the map ID in this hash, then it will default
  76.   # to the value at line 83.
  77.     2 => 40,
  78.     8 => 20,
  79.   } # <- Don't touch
  80.   #  Changing the below value allows you to change the default speed of frame
  81.   # animation. Ie. the speed of frame animation in a map in which you have not
  82.   # directly set the speed via the above hash configuration.
  83.   MAAP_PARALLAX_ANIMATION_FRAMES.default = 30
  84.   #  Depending on the size of the parallaxes and how many panels you use in a
  85.   # map, there can be some lag when you load new panels. The following option
  86.   # allows you to decide whether all the parallax frames are loaded at once
  87.   # when the map is first entered or individually the first time each panel
  88.   # shows up. Generally, if your panels are very large (1MB+) then you should
  89.   # set it to true; if smaller files, then you should set it to false.
  90.   MAAP_PRELOAD_PARALLAXES = true
  91.   #|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  92.   #    END EDITABLE REGION
  93.   #///////////////////////////////////////////////////////////////////////////
  94.   MAAP_SUPPORTED_EXTENSIONS = ["png", "jpg", "bmp"]
  95.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  96.   # * Setup Parallax
  97.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  98.   alias ma_ap_stuppara_5tc1 setup_parallax
  99.   def setup_parallax (*args, &block)
  100.     ma_ap_stuppara_5tc1 (*args, &block) # Run Original Method
  101.     setup_parallax_frames
  102.   end
  103.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  104.   # * Update Parallax
  105.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  106.   alias mlg_ap_updparal_4fg2 update_parallax
  107.   def update_parallax (*args, &block)
  108.     mlg_ap_updparal_4fg2 (*args, &block) # Run Original Method
  109.     # Use the timer if the parallax has more than one frame
  110.     if @maap_parallax_frames && @maap_parallax_frames.size > 1
  111.       @maap_parallax_frame_timer += 1
  112.       if @maap_parallax_frame_timer % @maap_parallax_frame_limit == 0
  113.         @maap_parallax_index = (@maap_parallax_index + 1) % @maap_parallax_frames.size
  114.         @parallax_name = @maap_parallax_frames[@maap_parallax_index]
  115.         if MAAP_PARALLAX_ANIMATION_FRAMES[@map_id].is_a? (Array) && MAAP_PARALLAX_ANIMATION_FRAMES[@map_id].size > @maap_parallax_index
  116.           @maap_parallax_frame_limit = MAAP_PARALLAX_ANIMATION_FRAMES[@map_id][@maap_parallax_index]
  117.         end
  118.       end
  119.     end
  120.   end
  121.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  122.   # * Setup Parallax Frames
  123.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  124.   def setup_parallax_frames
  125.     # Dispose the cached bitmaps from the previous map
  126.     last_map_bmps = @maap_parallax_frames.nil? ? [] : @maap_parallax_frames
  127.     @maap_parallax_index = 0
  128.     @maap_parallax_frames = [@parallax_name]
  129.     @maap_parallax_frame_timer = 0
  130.     if MAAP_PARALLAX_ANIMATION_FRAMES[@map_id].is_a? (Array) && MAAP_PARALLAX_ANIMATION_FRAMES[@map_id].size > 0
  131.       @maap_parallax_frame_limit = MAAP_PARALLAX_ANIMATION_FRAMES[@map_id][0]
  132.     else
  133.       @maap_parallax_frame_limit = MAAP_PARALLAX_ANIMATION_FRAMES[@map_id]
  134.     end
  135.     if @parallax_name[/_(\d+)$/] != nil
  136.       frame_id = $1.to_i + 1
  137.       base_name = @parallax_name.sub (/_\d+$/) { "" }
  138.       while maap_check_extensions ("Graphics/Parallaxes/#{base_name}_#{frame_id}")
  139.         @maap_parallax_frames.push ("#{base_name}_#{frame_id}")
  140.         frame_id += 1
  141.       end
  142.     end
  143.     (last_map_bmps - @maap_parallax_frames).each { |bmp| (Cache.parallax (bmp)).dispose }
  144.     # Preload all the parallax bitmaps so no lag is experienced on first load
  145.     if MAAP_PRELOAD_PARALLAXES
  146.       (@maap_parallax_frames - last_map_bmps).each { |bmp| Cache.parallax (bmp) }
  147.       Graphics.frame_reset
  148.     end
  149.   end
  150.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  151.   # * Check Extensions
  152.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  153.   def maap_check_extensions (filepath)
  154.     MAAP_SUPPORTED_EXTENSIONS.each { |ext|
  155.       return true if FileTest.exist? ("#{filepath}.#{ext}") }
  156.     return false
  157.   end
  158. end
  159.  
  160. #==============================================================================
  161. # ** Spriteset Map
  162. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  163. #  Summary of Changes:
  164. #    aliased method - update_parallax
  165. #==============================================================================
  166.  
  167. class Spriteset_Map
  168.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  169.   # * Update Parallax
  170.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  171.   alias malg_animparlx_upd_4rg1 update_parallax
  172.   def update_parallax (*args, &block)
  173.     # Don't ever dispose the cached parallax pictures.
  174.     @parallax.bitmap = nil if @parallax_name != $game_map.parallax_name  
  175.     malg_animparlx_upd_4rg1 (*args, &block) # Run Original Method
  176.   end
  177. end
RAW Paste Data