LiTTleDRAgo

[RGSS3] Galv's Cam Control (edited)

Jan 11th, 2014
178
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #------------------------------------------------------------------------------#
  2. #  Galv's Cam Control
  3. #------------------------------------------------------------------------------#
  4. #  For: RPGMAKER VX ACE
  5. #  Version 1.4
  6. #  edited by LiTTleDRAgo
  7. #------------------------------------------------------------------------------#
  8. #  2013-03-18 - Version 1.4 - fixed a bug where it crashed if you didn't turn
  9. #                           - off following an event when transferring to a map
  10. #                           - without that event.
  11. #  2013-03-18 - Version 1.3 - fixed a bug with parallax motion stopping
  12. #  2013-03-04 - Version 1.2 - re-written script to scroll to targets
  13. #  2012-10-24 - Version 1.1 - updated alias naming for compatability
  14. #  2012-10-20 - Version 1.0 - release
  15. #------------------------------------------------------------------------------#
  16. #  This script allows some control over where the camera is through use of
  17. #  script calls. You can fix the camera to a location on the map or follow an
  18. #  event's movements.
  19. #------------------------------------------------------------------------------#
  20. #  SCRIPT CALLS:
  21. #------------------------------------------------------------------------------#
  22. #
  23. #  cam_follow(event_id,speed)   # Camera follows event instead of player
  24. #  cam_center(speed)            # Moves camera back to player
  25. #  cam_set(x,y,speed)      # Position the camera centered on x, y coordinates
  26. #
  27. # # NOTE: speed can be 1 to 6 or you can leave it out of the script call to
  28. #         default to 6. If you want to instantly snap the camera without it
  29. #         scrolling, make speed 0
  30. #
  31. #------------------------------------------------------------------------------#
  32. #  EXAMPLES:
  33. #  cam_set(10,5)         # Moves camera to map co-ordinates x 10, y5 at speed 6
  34. #  cam_set(10,5,2)       # Moves camera to map co-ordinates x 10, y5 at speed 2
  35. #  cam_follow(4)         # Moves camera to event 4's location and follows it.
  36. #  cam_follow(4,0)       # SNAPS camera to event 4's location and follows it.
  37. #  cam_center            # Moves camera to player at speed 6
  38. #  cam_center(0)         # Snaps camera to player instantly
  39. #------------------------------------------------------------------------------#
  40. # NO OPTIONS FOR YOU TO EDIT. SORRY.
  41. #------------------------------------------------------------------------------#
  42.  
  43. $imported = {} if $imported.nil?
  44. $imported["Camera_Control"] = true
  45.  
  46. class Game_Map
  47.   attr_accessor :cam_target
  48.    
  49.   alias_method :galv_cam_control_gm_setup, :setup
  50.   def setup(*args)
  51.     @cam_target = 0
  52.     galv_cam_control_gm_setup(*args)
  53.     @scroll_blocked = false
  54.   end
  55.    
  56.   unless $imported[:drg_diagonal_scroll]
  57.     @@scroll = method_defined?(:do_scroll) ? :do_scroll : :update
  58.     alias_method :update_scrolling_straight, :"#{@@scroll}"
  59.    
  60.     define_method(:"#{@@scroll}") do |*args|
  61.       update_scrolling_diagonal || 0 && update_scrolling_straight(*args)
  62.     end
  63.    
  64.     def update_scrolling_diagonal
  65.       if scrolling?
  66.         distance = 2 ** @scroll_speed
  67.         distance = scroll_distance if defined?(scroll_distance)
  68.         case @scroll_direction
  69.         when 1 then [scroll_down(distance), scroll_left(distance) ]
  70.         when 3 then [scroll_down(distance), scroll_right(distance)]
  71.         when 7 then [scroll_up(distance),   scroll_left(distance) ]
  72.         when 9 then [scroll_up(distance),   scroll_right(distance)]
  73.         end
  74.       end
  75.     end
  76.   end
  77.  
  78.   def scroll_to_target(x,y,speed)
  79.     x1, y1, dir = @display_x.to_i, @display_y.to_i, 0
  80.     dir = 1 if dir == 0 && y1 < y && x1 > x && scroll_can_move?(2) && scroll_can_move?(4)
  81.     dir = 7 if dir == 0 && y1 > y && x1 > x && scroll_can_move?(8) && scroll_can_move?(4)
  82.     dir = 9 if dir == 0 && y1 > y && x1 < x && scroll_can_move?(8) && scroll_can_move?(6)
  83.     dir = 3 if dir == 0 && y1 < y && x1 < x && scroll_can_move?(2) && scroll_can_move?(6)
  84.     dir = 2 if dir == 0 && y1 < y && scroll_can_move?(2)
  85.     dir = 8 if dir == 0 && y1 > y && scroll_can_move?(8)
  86.     dir = 6 if dir == 0 && x1 < x && scroll_can_move?(6)
  87.     dir = 4 if dir == 0 && x1 > x && scroll_can_move?(4)
  88.     start_scroll(dir, 1, speed) if dir > 0
  89.     @scroll_blocked = dir == 0
  90.     Fiber.yield while scrolling?
  91.   end
  92.    
  93.   def cannot_scroll?
  94.     @scroll_blocked == true
  95.   end
  96.    
  97.   def scroll_can_move?(dir)
  98.     case dir
  99.     when 2 then (@display_y % @map.height) < height - (Graphics.height / 32)
  100.     when 4 then (@display_x % @map.width)  > 0
  101.     when 6 then (@display_x % @map.width)  < width  - (Graphics.width  / 32)
  102.     when 8 then (@display_y % @map.height) > 0
  103.     end
  104.   end
  105.    
  106.   def set_event_display_pos(x, y)
  107.     x = [0, [x, width  - screen_tile_x].min].max unless loop_horizontal?
  108.     y = [0, [y, height - screen_tile_y].min].max unless loop_vertical?
  109.     @display_x = (x + width) % width
  110.     @display_y = (y + height) % height
  111.   end
  112.    
  113. end # Game_Map
  114.  
  115. class Game_Player < Game_Character
  116.  
  117.   alias_method :galv_cam_control_gp_update, :update
  118.   alias_method :galv_cam_control_gp_update_scroll, :update_scroll
  119.  
  120.   def update(*args)
  121.     if (target = $game_map.cam_target).is_a?(Integer) && target > 0
  122.       e = $game_map.interpreter.get_character(t)
  123.     elsif target.is_a?(Game_Character)
  124.       e = target
  125.     end
  126.     e && $game_map.set_event_display_pos(e.real_x-center_x, e.real_y-center_y)
  127.     galv_cam_control_gp_update(*args)
  128.   end
  129.    
  130.   def update_scroll(*args)
  131.     return if ($game_map.cam_target ||= 0) != 0
  132.     galv_cam_control_gp_update_scroll(*args)
  133.   end
  134. end # Game_Player < Game_Character
  135.  
  136. class Game_Interpreter
  137.  
  138.   def cam_center(speed = 6)
  139.     cam_set($game_player.x, $game_player.y, speed)
  140.     $game_map.cam_target = 0
  141.   end
  142.    
  143.   def cam_set(x,y,speed = 6)
  144.     $game_map.cam_target = -1
  145.     _x = (x - $game_player.center_x).round
  146.     _y = (y - $game_player.center_y).round
  147.     speed > 0 && scroll_to_target(x,y,speed)
  148.     $game_map.set_display_pos(_x, _y)
  149.   end
  150.    
  151.   def scroll_to_target(x,y,speed)
  152.     _x = (x - $game_player.center_x).round
  153.     _y = (y - $game_player.center_y).round
  154.     loop do
  155.       $game_map.scroll_to_target(_x, _y, speed)
  156.       finish = $game_map.display_x == _x && $game_map.display_y == _y
  157.       break if finish || $game_map.cannot_scroll?
  158.     end
  159.   end
  160.    
  161.   def scroll_to_event(id,speed)
  162.     char = id.is_a?(Game_Character) ? id : get_character(id)
  163.     char && scroll_to_target(char.x,char.y,speed)
  164.   end
  165.    
  166.   def cam_follow(event_id, speed = 6)
  167.     $game_map.cam_target = -1
  168.     char = event_id.is_a?(Game_Character) ? event_id : get_character(event_id)
  169.     char && speed > 0 && scroll_to_event(char,speed)
  170.     char && ($game_map.cam_target = char)
  171.   end
  172. end # Game_Interpreter
RAW Paste Data