Advertisement
QuasiXi

Quasi Screen Scrolling

Aug 13th, 2014
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.46 KB | None | 0 0
  1. #==============================================================================
  2. #** Quasi Screen Scrolling v1.3
  3. #==============================================================================
  4. # Better screen scrolling.
  5. # Allows smooth diagonal scrolling
  6. # Can also scroll to a player/event
  7. #==============================================================================
  8. # Instructions:
  9. #  Paste this script below Materials and above Main!
  10. #
  11. #  To do a diagonal scroll, you must do the following script call:
  12. #
  13. #  $game_map.start_scroll(direction, distance, speed)
  14. #
  15. #  -Direction is equal to a number 1 2 3 4 6 7 8 9:
  16. #    7 Up/Left    8 Up      9 Up/Right
  17. #    4 Left                 6 Right
  18. #    1 Down/Left  2 Down    3 Down/Right
  19. #   (Can look at number pad for visual help.)
  20. #  -Distance is the number of tiles you want to scroll
  21. #    For diagonal you need to place this in bracets [X, Y] and keep the values
  22. #    possitive!
  23. #  -Speed is how fast it should scroll ( default is 4 (**NEW** now it's 60) )
  24. #  -- **NEW** Speed was changed to how many frames the scroll should take,
  25. #             ( 1 second = 60 frames )
  26. #  Example setup:
  27. #   $game_map.start_scroll(7,[6,4],4)
  28. #  This will scroll up 4 tiles, and scroll left 6 tiles because direction is 7.
  29. #
  30. #  Another new script call is to move the screen to the player or an event!
  31. #
  32. #  $game_map.scroll_to(object, speed)
  33. #
  34. #  object is the event id you want to scroll to!  Place 0 if you want to scroll
  35. #  to the player.
  36. #
  37. #  $game_map.scroll_to(0,4)
  38. #  will scroll back to the player
  39. #
  40. #  $game_map.scroll_to(1,4)
  41. #  will scroll to event 1's location
  42. #
  43. #------------------------------------------------------------------------------
  44. # Change Log
  45. #------------------------------------------------------------------------------
  46. # v1.3 9/2/14
  47. #  - Changed speed to work in pixels per frame.  Changed event map scroll 4 speed
  48. #    to take 60 frames for the scroll, 1 speed is 8x slower, 2 is 4x slower, ect.
  49. # v1.2 9/2/14
  50. #  - Changed how speed works, for better scrolling, may have to use script calls
  51. #    instead of event scrolling for higher speeds.
  52. # v1.1 8/24/14
  53. #  - Added $game_map.start_angled(angle, distance in pixels, speed)
  54. #==============================================================================#
  55. # ** NOTE **
  56. #  diagonal scrolling may sometimes only scroll in one direction!  But that is
  57. # because you are near the edge of the screen, and it can't scroll in the other
  58. # direction!
  59. #
  60. #==============================================================================#
  61. # By Quasi (http://quasixi.wordpress.com/)
  62. #  - 8/13/14
  63. #==============================================================================#
  64. #   ** Stop! Do not edit anything below, unless you know what you      **
  65. #   ** are doing!                                                      **
  66. #==============================================================================#
  67. #==============================================================================
  68. # ** Game_Map
  69. #------------------------------------------------------------------------------
  70. #  This class handles maps. It includes scrolling and passage determination
  71. # functions. The instance of this class is referenced by $game_map.
  72. #==============================================================================
  73.  
  74. class Game_Map
  75.   alias qsetup setup_scroll
  76.   #--------------------------------------------------------------------------
  77.   # * Scroll Setup
  78.   #--------------------------------------------------------------------------
  79.   def setup_scroll
  80.     qsetup
  81.     @slope_rest = 0
  82.     @slope_speed = 0
  83.     @scroll_rest2 = 0
  84.     @scroll_speed2 = 0
  85.   end
  86.   #--------------------------------------------------------------------------
  87.   # * Start Scroll
  88.   #--------------------------------------------------------------------------
  89.   def start_angled(angle, distance, speed)
  90.     # convert distance to grid numbers
  91.     distance = distance
  92.    
  93.     # Grab direction from angle
  94.     if angle == 0 || angle == 360
  95.       dir = 6
  96.     elsif angle > 0 && angle < 90
  97.       dir = 9
  98.     elsif angle == 90
  99.       dir = 8
  100.     elsif angle > 90 && angle < 180
  101.       dir = 7
  102.     elsif angle == 180
  103.       dir = 4
  104.     elsif angle > 180 && angle < 270
  105.       dir = 1
  106.     elsif angle == 270
  107.       dir = 2
  108.     elsif angle > 270 && angle < 360
  109.       dir = 3
  110.     end
  111.     @scroll_direction = dir
  112.    
  113.     # Get X/Y Distance
  114.     radian = (angle/180.0) * Math::PI
  115.     x = distance * Math.cos(radian)
  116.     y = distance * Math.sin(radian)
  117.     @scroll_rest = x.abs
  118.     @scroll_rest2 = y.abs
  119.    
  120.     @slope_rest = distance
  121.     @slope_speed = distance/speed
  122.    
  123.     @scroll_speed = @scroll_rest/speed
  124.     @scroll_speed2 = @scroll_rest2/speed
  125.   end
  126.   #--------------------------------------------------------------------------
  127.   # * Start Scroll
  128.   #--------------------------------------------------------------------------
  129.   def start_scroll(direction, distance, speed)
  130.     @scroll_direction = direction
  131.    
  132.     distx = direction == 4 ? distance : direction == 6 ? distance : 0
  133.     disty = direction == 8 ? distance : direction == 2 ? distance : 0
  134.    
  135.     @scroll_rest = distance.is_a?(Array) ? distance[0] : distx
  136.     @scroll_rest2 = distance.is_a?(Array) ? distance[1] : disty
  137.    
  138.     check_scroll_limit(direction) if @scroll_rest2 != 0 && @scroll_rest != 0
  139.    
  140.     distance = Math.hypot(@scroll_rest, @scroll_rest2)
  141.    
  142.     @slope_rest = distance
  143.     @slope_speed = distance / speed.to_f
  144.    
  145.     @scroll_speed =  @scroll_rest / speed.to_f
  146.     @scroll_speed2 = @scroll_rest2 / speed.to_f
  147.   end
  148.   #--------------------------------------------------------------------------
  149.   # * Checks if screen can scroll X/Y distance
  150.   #   if not, get the dif.
  151.   # ** Note sure if this works in v1.3
  152.   #--------------------------------------------------------------------------
  153.   def check_scroll_limit(dir)
  154.     dif_x = @display_x + (@scroll_rest * (dir == 7 || dir == 1 ? -1 : 1))
  155.     dif_y = @display_y + (@scroll_rest2 * (dir == 7 || dir == 9 ? -1 : 1))
  156.     @scroll_rest = @display_x if dif_x < 0 && !loop_horizontal?
  157.     @scroll_rest2 = @display_y if dif_y < 0 && !loop_vertical?
  158.   end
  159.   #--------------------------------------------------------------------------
  160.   # * Determine if Scrolling
  161.   #--------------------------------------------------------------------------
  162.   def scroll_to(obj, speed)
  163.     obj = obj == 0 ? $game_player : @events[obj]
  164.    
  165.     center_x = ((@display_x+(Graphics.width/2)/32).round)
  166.     center_y = ((@display_y+(Graphics.height/2)/32).round)
  167.    
  168.     if loop_horizontal? && obj.x < @display_x - (width - screen_tile_x) / 2
  169.       dist_x = obj.x - center_x + @map.width
  170.     else
  171.       dist_x = obj.x - center_x
  172.     end
  173.     if loop_vertical? && obj.y < @display_y - (height - screen_tile_y) / 2
  174.       dist_y = obj.y - center_y + @map.height
  175.     else
  176.       dist_y = obj.y - center_y
  177.     end
  178.    
  179.     x_dir = dist_x <=> 0
  180.     y_dir = dist_y <=> 0
  181.    
  182.     return if x_dir == 0 && y_dir == 0
  183.     dir = 1 if x_dir == -1 && y_dir == 1
  184.     dir = 2 if x_dir == 0 && y_dir == 1
  185.     dir = 3 if x_dir == 1 && y_dir == 1
  186.     dir = 4 if x_dir == -1 && y_dir == 0
  187.     dir = 6 if x_dir == 1 && y_dir == 0
  188.     dir = 7 if x_dir == -1 && y_dir == -1
  189.     dir = 8 if x_dir == 0 && y_dir == -1
  190.     dir = 9 if x_dir == 1 && y_dir == -1
  191.    
  192.     dist = [dist_x.abs, dist_y.abs]
  193.     dist = dist_x.abs if dist_y == 0
  194.     dist = dist_y.abs if dist_x == 0
  195.     start_scroll(dir, dist, speed)
  196.   end
  197.   #--------------------------------------------------------------------------
  198.   # * Determine if Scrolling
  199.   #--------------------------------------------------------------------------
  200.   def scrolling?
  201.     @slope_rest.round(8) > 0
  202.   end
  203.   #--------------------------------------------------------------------------
  204.   # * Update Scroll
  205.   #--------------------------------------------------------------------------
  206.   def update_scroll
  207.     return unless scrolling?
  208.    
  209.     do_scroll(@scroll_direction, @scroll_speed, @scroll_speed2)
  210.     @slope_rest -= @slope_speed
  211.   end
  212.   #--------------------------------------------------------------------------
  213.   # * Execute Scroll
  214.   #--------------------------------------------------------------------------
  215.   def do_scroll(direction, distance, distance2 = nil)
  216.     #qdo_scroll(direction, distance)
  217.     case direction
  218.     when 1,2
  219.       scroll_down (distance2)
  220.       scroll_left(distance)
  221.     when 3,6
  222.       scroll_down (distance2)
  223.       scroll_right(distance)
  224.     when 7,4
  225.       scroll_up (distance2)
  226.       scroll_left(distance)
  227.     when 9,8
  228.       scroll_up (distance2)
  229.       scroll_right(distance)
  230.     end
  231.   end
  232. end
  233.  
  234. #==============================================================================
  235. # ** Game_Interpreter
  236. #------------------------------------------------------------------------------
  237. #  An interpreter for executing event commands. This class is used within the
  238. # Game_Map, Game_Troop, and Game_Event classes.
  239. #==============================================================================
  240.  
  241. class Game_Interpreter
  242.   #--------------------------------------------------------------------------
  243.   # *OVERWRITE* Scroll Map
  244.   #--------------------------------------------------------------------------
  245.   def command_204
  246.     return if $game_party.in_battle
  247.     Fiber.yield while $game_map.scrolling?
  248.     speed = {1=>8.0,2=>4.0,3=>2.0,4=>1,5=>1/2.0,6=>1/4.0}
  249.     $game_map.start_scroll(@params[0], @params[1], 60*speed[@params[2]])
  250.   end
  251. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement