khanhdu

Animated Parallax 1.0

Jan 24th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 12.51 KB | None | 0 0
  1. #==============================================================================
  2. #    Animated Parallax [VXA]
  3. #    Version: 1.0
  4. #    Author: modern algebra (rmrk.net)
  5. #    Date: December 20, 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 Ace
  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 83 and follow the
  31. #        instructions for setting the animation speed
  32. #``````````````````````````````````````````````````````````````````````````````
  33. #     If you need to change the speed at which parallax panels cycle (for
  34. #    instance, if you have also changed the parallax that is displaying), you  
  35. #    can do so by using the following code in a Script call:
  36. #
  37. #      change_parallax_animation_speed(x)
  38. #        x: the number of frames before cycling to the next panel. There are 60
  39. #          frames in one second.
  40. #
  41. #    Note: there cannot be a space between speed and (.
  42. #      change_parallax_animation_speed(x)     <- Correct
  43. #      change_parallax_animation_speed (x)    <- Incorrect
  44. #
  45. #    You can also change it to an array of times, such that you can make it so
  46. #   some frames remain up for longer than others. To do so, just place all of
  47. #   the speeds for each panel in order, separated by commas. Ie.
  48. #
  49. #      change_parallax_animation_speed(x1, x2, ..., xn)
  50. #
  51. #    The same rule as at line 41 applies.
  52. #
  53. #    EXAMPLES:
  54. #
  55. #      change_parallax_animation_speed(30)
  56. #        Each parallax panel will be up for half a second before switching.
  57. #
  58. #      change_parallax_animation_speed(15, 30, 60, 45)
  59. #        The first parallax panel will be up for one quarter of a second before
  60. #       switching to the second panel; the second panel will be up for half a
  61. #       second before switching to the third panel; the third panel will be up
  62. #       for one second before switching to the fourth panel; the fourth panel
  63. #       will be up for three quarters of a second before switching back to the
  64. #       first panel. Repeat.
  65. #==============================================================================
  66.  
  67. $imported = {} unless $imported
  68. $imported[:MA_AnimatedParallax] = true
  69.  
  70. #==============================================================================
  71. # ** Game Map
  72. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  73. #  Summary of Changes:
  74. #    new constants - MAAP_PARALLAX_ANIMATION_FRAMES; MAAP_PRELOAD_PARALLAXES
  75. #      MAAP_SUPPORTED_EXTENSIONS
  76. #    aliased methods - setup_parallax; change_parallax; update_parallax
  77. #    new methods - setup_parallax_frames; maap_check_extensions
  78. #==============================================================================
  79.  
  80. class Game_Map
  81.   MAAP_PARALLAX_ANIMATION_FRAMES = { # <- Don't touch
  82.   #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  83.   #    EDITABLE REGION
  84.   #|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  85.   #  MAAP_PARALLAX_ANIMATION_FRAMES - this constant allows you to set the
  86.   # speed at which the parallax switches to the next graphic in the animation
  87.   # series by individual maps. So if you want it to be every 20 frames in one
  88.   # map but every 35 in another map, this is where you do it. All you need to
  89.   # do is type in the following code:
  90.   #
  91.   #      map_id => frames,
  92.   #
  93.   # where map_id is the ID of the Map you want to set it for and frames is
  94.   # either (a) an integer for how many frames you want to show each panel
  95.   # before switching to the next; or (b) an array of integers where each entry
  96.   # of the array is the number of frames to keep the corresponding frame up
  97.   # before switching to the next. This allows you to vary the time each of the
  98.   # frames is left on before switching. There are 60 frames in a second.
  99.   #
  100.   #    EXAMPLES:
  101.   #      1 => 35,    Map 1 will cycle through parallax panels every 35 frames
  102.   #      2 => 40,    Map 2 will cycle through parallax panels every 40 frames
  103.   #      8 => [20, 5, 15],    Map 8 will keep the first panel of the animated
  104.   #                  parallax on for 20 frames before switching to the second
  105.   #                  panel which will be on for 5 frames before switching to
  106.   #                  the third panel which is on 15 frames before switching
  107.   #                  back to the first panel. Repeat.
  108.   #
  109.   #  Note that the comma is necessary! For any maps where you use animated
  110.   # parallaxes but do not include the map ID in this hash, then it will default
  111.   # to the value set below at: MAAP_PARALLAX_ANIMATION_FRAMES.default.
  112.     1 => 20,
  113.     8 => 40,
  114.   } # <- Don't touch
  115.   #  Changing the below value allows you to change the default speed of frame
  116.   # animation. Ie. the speed of frame animation in a map in which you have not
  117.   # directly set the speed via the above hash configuration.
  118.   MAAP_PARALLAX_ANIMATION_FRAMES.default = 30
  119.   #  Depending on the size of the parallaxes and how many panels you use in a
  120.   # map, there can be some lag when you load new panels. The following option
  121.   # allows you to decide whether all the parallax frames are loaded at once
  122.   # when the map is first entered or individually the first time each panel
  123.   # shows up. Generally, if your panels are very large (1MB+) then you should
  124.   # set it to true; if smaller files, then you should set it to false.
  125.   MAAP_PRELOAD_PARALLAXES = true
  126.   #|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  127.   #    END EDITABLE REGION
  128.   #///////////////////////////////////////////////////////////////////////////
  129.   MAAP_SUPPORTED_EXTENSIONS = ["png", "jpg", "bmp"]
  130.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  131.   # * Setup Parallax
  132.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  133.   alias ma_anmp_setuplax_6id3 setup_parallax
  134.   def setup_parallax(*args, &block)
  135.     ma_anmp_setuplax_6id3(*args, &block) # Run Original Method
  136.     setup_parallax_frames
  137.   end
  138.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  139.   # * Change Parallax
  140.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  141.   alias moda_ap_chngprlx_8uz2 change_parallax
  142.   def change_parallax(*args, &block)
  143.     moda_ap_chngprlx_8uz2(*args, &block) # Run Original Method
  144.     setup_parallax_frames
  145.   end
  146.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  147.   # * Update Parallax
  148.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  149.   alias maba_ap_updprx_9hv3 update_parallax
  150.   def update_parallax(*args, &block)
  151.     maba_ap_updprx_9hv3(*args, &block) # Run Original Method
  152.     # Use the timer if the parallax has more than one frame
  153.     if @maap_parallax_frames && @maap_parallax_frames.size > 1
  154.       @maap_parallax_frame_timer += 1
  155.       # Check if timer exceeded
  156.       if @maap_parallax_frame_timer >= @maap_frame_speed
  157.         @maap_parallax_frame_timer = 0 # Reset Timer
  158.         # Set parallax to next frame
  159.         @maap_parallax_index = (@maap_parallax_index + 1) % @maap_parallax_frames.size
  160.         @parallax_name = @maap_parallax_frames[@maap_parallax_index]
  161.         set_parallax_frame_speed(@maap_parallax_speed, @maap_parallax_index)
  162.       end
  163.     end
  164.   end
  165.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  166.   # * Set Parallax Animation Speed
  167.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  168.   def set_parallax_frame_speed(parallax_speed = MAAP_PARALLAX_ANIMATION_FRAMES[@map_id], frame = 0)
  169.     @maap_parallax_speed = parallax_speed
  170.     if @maap_parallax_speed.is_a?(Array)
  171.       @maap_frame_speed = [@maap_parallax_speed[frame], @maap_parallax_speed.compact[0]].compact[0]
  172.     else
  173.       @maap_frame_speed = @maap_parallax_speed
  174.     end
  175.     # Get the default setting, in case the time limit is incorrectly set
  176.     unless @maap_frame_speed.is_a?(Integer)
  177.       p "Error: Animated Parallax 1.0\nFrame Speed incorrectly set for #{@map_id}, frame #{frame + 1} - #{@parallax_name}"
  178.       @maap_frame_speed = MAAP_PARALLAX_ANIMATION_FRAMES.default
  179.       @maap_frame_speed = default.compact[0] if @maap_frame_speed.is_a?(Array)
  180.       @maap_frame_speed = 30 if !@maap_frame_speed
  181.     end
  182.   end
  183.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  184.   # * Setup Parallax Frames
  185.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  186.   def setup_parallax_frames
  187.     # Retain the names of old map's parallax, for disposal
  188.     last_map_bmps = @maap_parallax_frames.nil? ? [] : @maap_parallax_frames
  189.     # Initialize Data
  190.     @maap_parallax_index = 0
  191.     @maap_parallax_frames = [@parallax_name]
  192.     @maap_parallax_frame_timer = 0
  193.     set_parallax_frame_speed
  194.     # Collect all frames of the parallax animation
  195.     if @parallax_name[/_(\d+)$/] != nil
  196.       frame_id = $1.to_i + 1
  197.       base_name = @parallax_name.sub(/_\d+$/, "")
  198.       while maap_check_extensions("Graphics/Parallaxes/#{base_name}_#{frame_id}")
  199.         @maap_parallax_frames.push("#{base_name}_#{frame_id}")
  200.         frame_id += 1
  201.       end
  202.     end
  203.     # Dispose the cached bitmaps from the previous map
  204.     (last_map_bmps - @maap_parallax_frames).each { |bmp| (Cache.parallax(bmp)).dispose }
  205.     # Preload all the parallax bitmaps so no lag is experienced on first load
  206.     if MAAP_PRELOAD_PARALLAXES
  207.       (@maap_parallax_frames - last_map_bmps).each { |bmp| Cache.parallax(bmp) }
  208.     end
  209.     Graphics.frame_reset
  210.   end
  211.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  212.   # * Check Extensions
  213.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  214.   def maap_check_extensions (filepath)
  215.     MAAP_SUPPORTED_EXTENSIONS.any? { |ext| FileTest.exist?("#{filepath}.#{ext}") }
  216.   end
  217. end
  218.  
  219. #==============================================================================
  220. # ** Game_Interpreter
  221. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  222. #  Summary of Changes:
  223. #    new method - change_parallax_animation_speed
  224. #==============================================================================
  225.  
  226. class Game_Interpreter
  227.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  228.   # * Change Parallax Animation Speed
  229.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  230.   def change_parallax_animation_speed(*args)
  231.     if args.size <= 1
  232.       $game_map.set_parallax_frame_speed(*args)
  233.     else
  234.       $game_map.set_parallax_frame_speed(args)
  235.     end
  236.   end
  237. end
  238.  
  239. #==============================================================================
  240. # ** Spriteset Map
  241. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  242. #  Summary of Changes:
  243. #    aliased method - update_parallax
  244. #==============================================================================
  245.  
  246. class Spriteset_Map
  247.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  248.   # * Update Parallax
  249.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  250.   alias ma_animpara_updlax_7ig8 update_parallax
  251.   def update_parallax(*args, &block)
  252.     # Don't ever dispose the cached parallax pictures.
  253.     @parallax.bitmap = nil if @parallax_name != $game_map.parallax_name  
  254.     ma_animpara_updlax_7ig8(*args, &block) # Run Original Method
  255.   end
  256. end
Add Comment
Please, Sign In to add comment