CyberForte

DoW Smooth Scroll

Oct 10th, 2017
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 13.74 KB | None | 0 0
  1. #===============================================================
  2. # ● [VX] ◦ Smooth Scrolling ◦ □
  3. # * Nice scrolling effect like an action game~! *
  4. # *   Ported from XP Version (by Toby Zerner)   *
  5. #--------------------------------------------------------------
  6. # ◦ by Woratana [[email protected]]
  7. # ◦ Thaiware RPG Maker Community
  8. # ◦ Released on: 08/02/2009
  9. # ◦ Version: 1.0
  10. #--------------------------------------------------------------
  11. # ◦ Compatibility:
  12. #--------------------------------------------------------------
  13. # □ This script will rewrite 6 method(s):
  14. #     Game_Map.start_scroll
  15. #     Game_Map.scroll_up
  16. #     Game_Map.scroll_down
  17. #     Game_Map.scroll_left
  18. #     Game_Map.scroll_right
  19. #     Game_Map.update_scroll
  20. #
  21. # □ This script will alias 1 method(s):
  22. #     Game_Map.setup_scroll
  23. #
  24. # □ This script would crash with other custom scripts that rewrite same method(s)
  25. #
  26. # □ This script may not work with maps that use loop.
  27. #--------------------------------------------------------------
  28. # ◦ Installation:
  29. #--------------------------------------------------------------
  30. # 1) This script should be placed JUST AFTER ▼ Materials.
  31. #
  32. # □ Like this:
  33. # ▼ Materials
  34. # * Smooth Scrolling
  35. # ...
  36. # ...
  37. # ▼ Main Process
  38. # Main
  39. #
  40. # 2) Setup this script in Setup Part below.
  41. #
  42. #--------------------------------------------------------------
  43. # ◦ How to use:
  44. #--------------------------------------------------------------
  45. # □ Just place this script and try moving player in your game.
  46. #
  47. #=================================================================
  48.  
  49. class Game_System
  50.  
  51.   #=================================================================
  52.   # ++ Setup Part
  53.   #-----------------------------------------------------------------
  54.   DEFAULT_DECELERATION_START = 4 # (default: 4)
  55.   # How fast before it will start decelerating (Higher = Start Faster)
  56.   # You can change this value in game by call script:
  57.   #   $game_system.decel_start = (value)
  58.  
  59.   DEFAULT_DECELERATION_SPEED = 4 # (default: 4)
  60.   # How fast it will decelerate each frame (Higher = Decelerate Faster)
  61.   # You can change this value in game by call script:
  62.   #   $game_system.decel_speed = (value)
  63.   #-----------------------------------------------------------------
  64.  
  65.   def decel_start
  66.     @decel_start ||= DEFAULT_DECELERATION_START
  67.     return @decel_start
  68.   end
  69.  
  70.   def decel_speed
  71.     @decel_speed ||= DEFAULT_DECELERATION_SPEED
  72.     return @decel_speed
  73.   end
  74.  
  75.   def decel_start=(value)
  76.     @decel_start = value
  77.   end
  78.  
  79.   def decel_speed=(value)
  80.     @decel_speed = value
  81.   end
  82. end
  83.  
  84. class Game_Map
  85.   #--------------------------------------------------------------------------
  86.   # * Scroll Setup
  87.   #--------------------------------------------------------------------------
  88.   alias wora_smoscr_gammap_setscr setup_scroll unless $@
  89.   def setup_scroll(*args)
  90.     wora_smoscr_gammap_setscr(*args)
  91.     # Initialize smooth scrolling variables
  92.     # scroll_remain: the number of pixels * 4 the still need to be scrolled
  93.     @scroll_remain_x = 0
  94.     @scroll_remain_y = 0
  95.     # scroll_take: the number of pixels * 4 that are being scrolled every frame
  96.     #   i.e. scrolling speed
  97.     @scroll_take_x = 0
  98.     @scroll_take_y = 0
  99.     # scroll_decel: variables for calculating decelaration
  100.     @scroll_decel_x = 0
  101.     @scroll_decel_y = 0
  102.   end
  103.   #--------------------------------------------------------------------------
  104.   # * Start Scroll
  105.   #--------------------------------------------------------------------------
  106.   def start_scroll(direction, distance, speed)
  107.     # Set scrolling variables
  108.     distance          = distance.ceil * 256
  109.     @scroll_direction = direction
  110.     @scroll_speed     = speed
  111.     @scroll_rest      = distance
  112.     # Execute scrolling
  113.     case @scroll_direction
  114.     when 2  # Down
  115.       scroll_down(@scroll_rest)
  116.     when 4  # Left
  117.       scroll_left(@scroll_rest)
  118.     when 6  # Right
  119.       scroll_right(@scroll_rest)
  120.     when 8  # Up
  121.       scroll_up(@scroll_rest)
  122.     end
  123.   end
  124.   #--------------------------------------------------------------------------
  125.   # * Scroll Down
  126.   #--------------------------------------------------------------------------
  127.   def scroll_down(distance)
  128.     # Ceil the distance
  129.     distance = distance.ceil
  130.     # If the map is scrolling from an event command, then use that scroll speed
  131.     if scrolling? then @scroll_take_y = 2 ** @scroll_speed
  132.     # If the map is not scrolling
  133.     else
  134.       # Make sure the distance is always divisible by 4
  135.       if distance.ceil % 4 == 0 then @scroll_take_y = distance.ceil
  136.       elsif distance.ceil % 4 <= 2 then @scroll_take_y = distance.ceil - distance.ceil % 4
  137.       else @scroll_take_y = distance.ceil + (4 - (distance.ceil % 4)) end
  138.     end
  139.     # If scrolling coordinates are inside the map's boundaries
  140.     unless @display_y + @scroll_remain_y + distance > (self.height - 13) * 256
  141.       # Add onto the amount left to be scrolled
  142.       @scroll_remain_y += distance
  143.     end
  144.   end
  145.   #--------------------------------------------------------------------------
  146.   # * Scroll Left
  147.   #--------------------------------------------------------------------------
  148.   def scroll_left(distance)
  149.     # Ceil the distance
  150.     distance = distance.ceil
  151.     # If the map is scrolling from an event command, then use that scroll speed
  152.     if scrolling? then @scroll_take_x = 2 ** @scroll_speed
  153.     # If the map is not scrolling
  154.     else
  155.       # Make sure the distance is always divisible by 4
  156.       if distance.ceil % 4 == 0 then @scroll_take_x = distance.ceil
  157.       elsif distance.ceil % 4 <= 2 then @scroll_take_x = distance.ceil - distance.ceil % 4
  158.       else @scroll_take_x = distance.ceil + (4 - (distance.ceil % 4)) end
  159.     end
  160.     # If scrolling coordinates are inside the map's boundaries
  161.     unless @display_x - @scroll_remain_x - distance < 0
  162.       # Add onto the amount left to be scrolled
  163.       @scroll_remain_x -= distance
  164.     end
  165.   end
  166.   #--------------------------------------------------------------------------
  167.   # * Scroll Right
  168.   #--------------------------------------------------------------------------
  169.   def scroll_right(distance)
  170.     # Ceil the distance
  171.     distance = distance.ceil
  172.     # If the map is scrolling from an event command, then use that scroll speed
  173.     if scrolling? then @scroll_take_x = 2 ** @scroll_speed
  174.     # If the map is not scrolling
  175.     else
  176.       # Make sure the distance is always divisible by 4
  177.       if distance.ceil % 4 == 0 then @scroll_take_x = distance.ceil
  178.       elsif distance.ceil % 4 <= 2 then @scroll_take_x = distance.ceil - distance.ceil % 4
  179.       else @scroll_take_x = distance.ceil + (4 - (distance.ceil % 4)) end
  180.     end
  181.     # If scrolling coordinates are inside the map's boundaries
  182.     unless @display_x + @scroll_remain_x + distance > (self.width - 17) * 256
  183.       # Add onto the amount left to be scrolled
  184.       @scroll_remain_x += distance
  185.     end
  186.   end
  187.   #--------------------------------------------------------------------------
  188.   # * Scroll Up
  189.   #--------------------------------------------------------------------------
  190.   def scroll_up(distance)
  191.     # Ceil the distance
  192.     distance = distance.ceil
  193.     # If the map is scrolling from an event command, then use that scroll speed
  194.     if scrolling? then @scroll_take_y = 2 ** @scroll_speed
  195.     # If the map is not scrolling
  196.     else
  197.       # Make sure the distance is always divisible by 4
  198.       if distance.ceil % 4 == 0 then @scroll_take_y = distance.ceil
  199.       elsif distance.ceil % 4 <= 2 then @scroll_take_y = distance.ceil - distance.ceil % 4
  200.       else @scroll_take_y = distance.ceil + (4 - (distance.ceil % 4)) end
  201.     end
  202.     # If scrolling coordinates are inside the map's boundaries
  203.     unless @display_y - @scroll_remain_y - distance < 0
  204.       # Add onto the amount left to be scrolled
  205.       @scroll_remain_y -= distance
  206.     end
  207.   end
  208.   #--------------------------------------------------------------------------
  209.   # * Update Scroll
  210.   #--------------------------------------------------------------------------
  211.   def update_scroll
  212.     # If the map is still scrolling
  213.     if @scroll_rest > 0 then @scroll_rest -= 2 ** @scroll_speed end
  214.    
  215.     # If the x axis needs to be scrolled to the right
  216.     if @scroll_remain_x > 0
  217.       # If the amount to be scrolled is close enough to 0 to decelerate
  218.       if @scroll_remain_x <= @scroll_take_x * $game_system.decel_start
  219.         old_display_x = @display_x
  220.         # Add onto the deceleration variable
  221.         @scroll_decel_x += $game_system.decel_speed
  222.         # Work out how much to scroll
  223.         distance = [@scroll_take_x - @scroll_decel_x, 4].max
  224.         # If the scrolling coordinates are within the map's boundaries
  225.         unless @display_x + distance > (self.width - 17) * 256
  226.           @display_x += distance
  227.           @parallax_x += distance
  228.         end
  229.         # Subtract the amount that was scrolled
  230.         @scroll_remain_x += old_display_x - @display_x
  231.         if @scroll_remain_x < 0 then @scroll_remain_x = 0 end
  232.       # Otherwise, scroll at a normal speed
  233.       else
  234.         # Reset the deceleration variable
  235.         @scroll_decel_x = 0
  236.         # If the scrolling coordinates are out of range
  237.         if @display_x + @scroll_take_x > (self.width - 17) * 256
  238.           @display_x = (self.width - 20) * 256
  239.           @scroll_remain_x = 0
  240.         # Otherwise, scroll normally
  241.         else
  242.           @display_x += @scroll_take_x
  243.           @parallax_x += @scroll_take_x
  244.           @scroll_remain_x -= @scroll_take_x
  245.         end
  246.       end
  247.      
  248.     # If the x axis needs to be scrolled to the left
  249.     elsif @scroll_remain_x < 0
  250.       # If the amount to be scrolled is close enough to 0 to decelerate
  251.       if @scroll_remain_x >= -@scroll_take_x * $game_system.decel_start
  252.         old_display_x = @display_x
  253.         # Add onto the deceleration variable
  254.         @scroll_decel_x += $game_system.decel_speed
  255.         # Work out how much to scroll
  256.         distance = [@scroll_take_x - @scroll_decel_x, 4].max
  257.         # If the scrolling coordinates are within the map's boundaries
  258.         unless @display_x - distance < 0
  259.           @display_x -= distance
  260.           @parallax_x -= distance
  261.         end
  262.         # Subtract the amount that was scrolled
  263.         @scroll_remain_x += old_display_x - @display_x
  264.         if @scroll_remain_x > 0 then @scroll_remain_x = 0 end
  265.       # Otherwise, scroll at a normal speed
  266.       else
  267.         # Reset the deceleration variable
  268.         @scroll_decel_x = 0
  269.         # If the scrolling coordinates are out of range
  270.         if @display_x - @scroll_take_x < 0
  271.           @display_x = 0
  272.           @scroll_remain_x = 0
  273.         # Otherwise, scroll normally
  274.         else
  275.           @display_x -= @scroll_take_x
  276.           @parallax_x -= @scroll_take_x
  277.           @scroll_remain_x += @scroll_take_x
  278.         end
  279.       end
  280.    
  281.     # If no x scrolling needs to be done, reset the deceleration variable
  282.     else @scroll_decel_x = 0 end
  283.    
  284.     # If the y axis needs to be scrolled downwards
  285.     if @scroll_remain_y > 0
  286.       # If the amount to be scrolled is close enough to 0 to decelerate
  287.       if @scroll_remain_y <= @scroll_take_y * $game_system.decel_start
  288.         old_display_y = @display_y
  289.         # Add onto the deceleration variable
  290.         @scroll_decel_y += $game_system.decel_speed
  291.         # Work out how much to scroll
  292.         distance = [@scroll_take_y - @scroll_decel_y, 4].max
  293.         # If the scrolling coordinates are within the map's boundaries
  294.         unless @display_y + distance > (self.height - 13) * 256
  295.           @display_y += distance
  296.           @parallax_y += distance
  297.         end
  298.         # Subtract the amount that was scrolled
  299.         @scroll_remain_y += old_display_y - @display_y
  300.         if @scroll_remain_y < 0 then @scroll_remain_y = 0 end
  301.       # Otherwise, scroll at a normal speed
  302.       else
  303.         # Reset the deceleration variable
  304.         @scroll_speed_accel_y = 0
  305.         # If the scrolling coordinates are out of range
  306.         if @display_y + @scroll_take_y > (self.height - 13) * 256
  307.           @display_y = (self.height - 15) * 256
  308.           @scroll_remain_y = 0
  309.         # Otherwise, scroll normally
  310.         else
  311.           @display_y += @scroll_take_y
  312.           @parallax_y += @scroll_take_y
  313.           @scroll_remain_y -= @scroll_take_y
  314.         end
  315.       end
  316.    
  317.     # If the y axis needs to be scrolled upwards
  318.     elsif @scroll_remain_y < 0
  319.       # If the amount to be scrolled is close enough to 0 to decelerate
  320.       if @scroll_remain_y >= -@scroll_take_y * $game_system.decel_start
  321.         old_display_y = @display_y
  322.         # Add onto the deceleration variable
  323.         @scroll_decel_y += $game_system.decel_speed
  324.         # Work out how much to scroll
  325.         distance = [@scroll_take_y - @scroll_decel_y, 4].max
  326.         # If the scrolling coordinates are within the map's boundaries
  327.         unless @display_y - distance < 0
  328.           @display_y -= distance
  329.           @parallax_y -= distance
  330.         end
  331.         # Subtract the amount that was scrolled
  332.         @scroll_remain_y += old_display_y - @display_y
  333.         if @scroll_remain_y > 0 then @scroll_remain_y = 0 end
  334.       # Otherwise, scroll at a normal speed
  335.       else
  336.         # Reset the deceleration variable
  337.         @scroll_speed_accel_y = 0
  338.         # If the scrolling coordinates are out of range
  339.         if @display_y - @scroll_take_y < 0
  340.           @display_y = 0
  341.           @scroll_remain_y = 0
  342.         # Otherwise, scroll normally
  343.         else
  344.           @display_y -= @scroll_take_y
  345.           @parallax_y -= @scroll_take_y
  346.           @scroll_remain_y += @scroll_take_y
  347.         end
  348.       end
  349.    
  350.     # If no y scrolling needs to be done, reset the deceleration variable
  351.     else
  352.       @scroll_decel_y = 0
  353.     end
  354.   end
  355. end
Advertisement
Add Comment
Please, Sign In to add comment