Advertisement
Ocedic

OC Motion Battle Camera 1.7

Mar 27th, 2013
4,739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 17.84 KB | None | 0 0
  1. # *****************************************************************************
  2. #
  3. # OC BATTLE CAMERA
  4. # Author: Ocedic, KMS
  5. # Site: http://ocedic.wordpress.com/
  6. # Version: 1.7
  7. # Last Updated: 3/27/13
  8. #
  9. # Updates:
  10. # 1.7 - Fixed a crash when escaping
  11. # 1.6 - Added a sprite move variance confirguation which let's you decrease the
  12. #       amount of sprite movement during camera movements
  13. # 1.5 - In SBSs, camera will now focus on party members when targeting them with
  14. #       friendly spells or items
  15. # 1.4 - Enemy sprite zoom can now enabled and disabled, sprite y control now
  16. #       affects both enemies and actors
  17. # 1.3 - Resolution support is now automatic, added compatibility with Victor
  18. #       Animated Battlers and generic SBS support
  19. # 1.2 - Added new configuration settings
  20. # 1.1 - Removed some extraneous code, sprites now move based on camera target
  21. # 1.0 - First release
  22. #
  23. # *****************************************************************************
  24.  
  25. $imported = {} if $imported.nil?
  26.  
  27. #==============================================================================
  28. # ▼ Introduction
  29. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  30. # This is an adaptation of KGC's Motion Battle Camera, which adds a moving
  31. # camera that follows the flow of battle. I used a lot of code by KGC, but
  32. # made my own modifications to get it working in Ace.
  33. #==============================================================================
  34. # ▼ Instructions
  35. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  36. # Plug and play script. Simply paste it above Main. Settings can be adjusted
  37. # in configuration section.
  38. #==============================================================================
  39. # ▼ Compatibility
  40. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  41. # This script works with YEA Battle System and Victor's Animated Battlers. It
  42. # also works with Jet's Viewed Battle System and Symphony, but you'll have to
  43. # enable the SIDEVIEW_SYSTEM switch in configuration. Note that Symphony must
  44. # disable shadows to work at the moment.
  45. # Other SBSs may work depending on their implementation, just enable the switch
  46. # and try.
  47. # Just make sure that it's placed below them.
  48. #==============================================================================
  49. # ▼ Terms of Use
  50. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  51. # Free for non-commercial use. Credit must be given to both Ocedic and KMS.
  52. # This script is not for commercial use.
  53. #==============================================================================
  54.  
  55. #==============================================================================
  56. # ■ Configuration
  57. #------------------------------------------------------------------------------
  58. #  Change customizable settings here
  59. #==============================================================================
  60. module OC
  61.   module OBC
  62.  
  63. #==============================================================================
  64. # * Enemy Sprite Zoom *
  65. #------------------------------------------------------------------------------
  66. #   Controls whether enemy sprites are zoomed. When enabled, enemies are zoomed
  67. #   out the higher their Y-axis, creating a pseudo-3D effect. Set this to false
  68. #   to disable to behavior.
  69. #==============================================================================
  70. # * Sprite Move Variance *
  71. #------------------------------------------------------------------------------
  72. #   This controls how much sprites move. Higher numbers mean less movement. In
  73. #   certain SBSs it may look better to set this to 2. Do not set it to zero.
  74. #==============================================================================
  75. # * Camera Move Speed *
  76. #------------------------------------------------------------------------------
  77. #   Adjusts the camera speed, higher value is faster
  78. #==============================================================================
  79.     ENEMY_SPRITE_ZOOM = true
  80.     SPRITE_MOVE_VARIANCE = 1
  81.     CAMERA_SPEED_INIT = 24
  82.    
  83. #==============================================================================
  84. # * Sideview Battle System *
  85. #------------------------------------------------------------------------------
  86. #   Currently, only a handful of sideview battle systems are supported (see
  87. #   above.) If you're using a supported SBS, it should work automatically. If
  88. #   you're using a different SBS, try setting this to true and it may work
  89. #   depending on how the system is implemented.
  90. #==============================================================================
  91. # * Sideview: Move Actor Sprite Y-Axis *
  92. #------------------------------------------------------------------------------
  93. #   When using a SBS, choose whether to move the actor's sprite along the Y-axis
  94. #   relative to the camera. Set it to false if you wish to disable this behavior
  95. #   which may look weird on some systems like Jet's Viewed Battle.
  96. #==============================================================================
  97.     SIDEVIEW_SYSTEM = false
  98.     SBS_SPRITE_Y = true
  99.  
  100.   end
  101. end
  102.  
  103. #==============================================================================
  104. # ■ Game_Enemy
  105. #==============================================================================
  106.  
  107. class Game_Enemy < Game_Battler
  108. #==============================================================================
  109. # * Accessors *
  110. #==============================================================================
  111.   attr_accessor :origin_x, :origin_y
  112.  
  113. #==============================================================================
  114. # * Alias: Initialize *
  115. #==============================================================================
  116.   alias oc_game_enemy_initialize_naslo initialize
  117.   def initialize(index, enemy_id)
  118.     oc_game_enemy_initialize_naslo(index, enemy_id)
  119.     @origin_x = 0
  120.     @origin_y = 0
  121.   end
  122.  
  123. #==============================================================================
  124. # * New Def: Zoom *
  125. #------------------------------------------------------------------------------
  126. #  Returns the magnification ratio
  127. #==============================================================================
  128.   def zoom
  129.       n = (1.00 + SceneManager.scene.camera.z / 512.00) * ((@origin_y - 304) / 256.00 + 1)
  130.     return n
  131.   end
  132.  
  133. end #class game_enemy
  134.  
  135. #==============================================================================
  136. # ■ Game_Actor
  137. #==============================================================================
  138.  
  139. class Game_Actor < Game_Battler
  140.  
  141. #==============================================================================
  142. # * Accessors *
  143. #==============================================================================
  144.   attr_accessor :origin_x, :origin_y
  145.  
  146.   alias oc_game_actor_initialize_23kls initialize
  147.   def initialize(actor_id)
  148.     oc_game_actor_initialize_23kls(actor_id)
  149.     @origin_x = 0
  150.     @origin_y = 0
  151.   end  
  152.  
  153. end #class game_actor  
  154.  
  155. #==============================================================================
  156. # ■ Game_Troop
  157. #==============================================================================
  158.  
  159. class Game_Troop < Game_Unit
  160.  
  161. #==============================================================================
  162. # * Overwrite: Setup *
  163. #==============================================================================
  164.   def setup(troop_id)
  165.     clear
  166.     @troop_id = troop_id
  167.     @enemies = []
  168.     troop.members.each do |member|
  169.       next unless $data_enemies[member.enemy_id]
  170.       enemy = Game_Enemy.new(@enemies.size, member.enemy_id)
  171.       enemy.hide if member.hidden
  172.       enemy.screen_x = enemy.origin_x = member.x + (Graphics.width - 544) / 2
  173.       enemy.screen_y = enemy.origin_y = member.y + (Graphics.height - 416)
  174.       @enemies.push(enemy)
  175.     end
  176.     init_screen_tone
  177.     make_unique_names
  178.   end
  179.  
  180. end #class game_troop
  181.  
  182. #==============================================================================
  183. # ** Sprite_Battler **
  184. #------------------------------------------------------------------------------
  185. #  This sprite is used to display battlers. It observes an instance of the
  186. # Game_Battler class and automatically changes sprite states.
  187. #==============================================================================
  188. class Sprite_Battler < Sprite_Base
  189.  
  190. #==============================================================================
  191. # * Alias: Update *
  192. #==============================================================================  
  193.   alias oc_sprite_battler_update_sfdkl update
  194.   def update
  195.     oc_sprite_battler_update_sfdkl
  196.  
  197.     return if @battler == nil || !@battler.is_a?(Game_Enemy) || !SceneManager.scene_is?(Scene_Battle)
  198.     self.zoom_x = self.zoom_y = @battler.zoom if OC::OBC::ENEMY_SPRITE_ZOOM
  199.   end
  200.  
  201. end #class sprite_battler
  202.  
  203. #==============================================================================
  204. # ■ Spriteset_Battle
  205. #==============================================================================
  206.  
  207. class Spriteset_Battle
  208.  
  209. #==============================================================================
  210. # * Alias: Update *
  211. #==============================================================================  
  212.   alias oc_spriteset_battle_update_pkwer update
  213.   def update
  214.     oc_spriteset_battle_update_pkwer
  215.     return if !SceneManager.scene_is?(Scene_Battle)
  216.    
  217.     cx, cy, cz = SceneManager.scene.camera.x, SceneManager.scene.camera.y, SceneManager.scene.camera.z
  218.     bx, by = @back2_sprite.x + Graphics.width / 2, @back2_sprite.y + 304
  219.     if bx != cx || by != cy || @bz != cz
  220.       # Adjust zoom
  221.       if Graphics.width > 544 && Graphics.height > 416
  222.         zoom = cz / 1024.00 + 1
  223.       else
  224.         zoom = cz / 416.00 + 1
  225.       end
  226.       @back1_sprite.zoom_x = @back2_sprite.zoom_x = zoom * 2.0
  227.       @back1_sprite.zoom_y = @back2_sprite.zoom_y = zoom * 2.0
  228.      
  229.       # Adjust coordinates
  230.       @back1_sprite.x = -cx * zoom / 2 - Graphics.width / 2 + @back1_sprite.ox * 2
  231.       @back1_sprite.y = -cy * zoom / 2 - Graphics.height / 3 + @back1_sprite.oy * 2
  232.       @back2_sprite.x = -cx * zoom / 2 - Graphics.width / 2 + @back2_sprite.ox * 2
  233.       @back2_sprite.y = -cy * zoom / 2 - Graphics.height / 3 + @back2_sprite.oy * 2
  234.       @bz = cz
  235.     end
  236.    
  237.     # Adjusts sprites' coordinates based on position relative to camera
  238.     if OC::OBC::ENEMY_SPRITE_ZOOM
  239.       @enemy_sprites.each { |e| e.x -= SceneManager.scene.camera.x * e.battler.zoom / OC::OBC::SPRITE_MOVE_VARIANCE}
  240.       @enemy_sprites.each { |e| e.y -= SceneManager.scene.camera.y * e.battler.zoom / OC::OBC::SPRITE_MOVE_VARIANCE} if OC::OBC::SBS_SPRITE_Y
  241.     else
  242.       @enemy_sprites.each { |e| e.x -= SceneManager.scene.camera.x / OC::OBC::SPRITE_MOVE_VARIANCE}
  243.       @enemy_sprites.each { |e| e.y -= SceneManager.scene.camera.y / OC::OBC::SPRITE_MOVE_VARIANCE} if OC::OBC::SBS_SPRITE_Y
  244.     end
  245.     if $imported[:ve_actor_battlers] || OC::OBC::SIDEVIEW_SYSTEM
  246.       @actor_sprites.each { |e| e.x -= SceneManager.scene.camera.x / OC::OBC::SPRITE_MOVE_VARIANCE}
  247.       @actor_sprites.each { |e| e.y -= SceneManager.scene.camera.y / OC::OBC::SPRITE_MOVE_VARIANCE} if OC::OBC::SBS_SPRITE_Y
  248.     end
  249.   end
  250.  
  251. end #class spriteset_battle
  252.  
  253. #==============================================================================
  254. # ■ New Class: Camera
  255. #------------------------------------------------------------------------------
  256. #  Handles the movable battle camera
  257. #==============================================================================
  258.  
  259. class Camera
  260.  
  261. #==============================================================================
  262. # * Accessors *
  263. #==============================================================================
  264.   attr_reader :x, :y, :z
  265.   attr_accessor :camera_speed
  266.  
  267. #==============================================================================
  268. # * Initialize *
  269. #==============================================================================
  270.   def initialize
  271.     @x, @y, @z = 0, 0, 0
  272.     @camera_speed = OC::OBC::CAMERA_SPEED_INIT
  273.     @move_x, @move_y, @move_z = 0, 0, 0
  274.   end
  275.  
  276. #==============================================================================
  277. # * Move *
  278. #------------------------------------------------------------------------------
  279. #  Repositions the battle camera
  280. #==============================================================================
  281.   def move(x, y, z)
  282.     @move_x = x - @x - Graphics.width / 2
  283.     @move_y = y - @y - Graphics.height / 3
  284.     @move_z = z - @z
  285.   end
  286.  
  287. #==============================================================================
  288. # * Move_Target *
  289. #------------------------------------------------------------------------------
  290. #  Positions the camera to focus on an enemy
  291. #==============================================================================
  292.   def move_target(target)
  293.     return if target == nil && !target.is_a?(Game_Enemy)
  294.     target_x = target.origin_x
  295.     target_y = target.origin_y - 144
  296.     target_z = (304 - target.origin_y) * 4
  297.     move(target_x, target_y, target_z)
  298.   end
  299.  
  300. #==============================================================================
  301. # * Move_Center *
  302. #------------------------------------------------------------------------------
  303. #  Positions the camera to the center
  304. #==============================================================================
  305.   def move_center
  306.     @move_x = -@x
  307.     @move_y = -@y
  308.     @move_z = -@z
  309.   end
  310.  
  311. #==============================================================================
  312. # * Update *
  313. #==============================================================================
  314.   def update
  315.     # X-coordinate movement
  316.     mv = [[@move_x.abs * @camera_speed / 160, 1].max, @camera_speed].min
  317.     if @move_x > 0
  318.       @x += mv
  319.       @move_x = [@move_x - mv, 0].max
  320.     elsif @move_x < 0
  321.       @x -= mv
  322.       @move_x = [@move_x + mv, 0].min
  323.     end
  324.    
  325.     # Y-coordinate movement
  326.     mv = [[@move_y.abs * @camera_speed / 160, 1].max, @camera_speed].min
  327.     if @move_y > 0
  328.       @y += mv
  329.       @move_y = [@move_y - mv, 0].max
  330.     elsif @move_y < 0
  331.       @y -= mv
  332.       @move_y = [@move_y + mv, 0].min
  333.     end
  334.    
  335.     # Z-coordinate movement
  336.     mv = [[@move_z.abs * @camera_speed / 96, 1].max, @camera_speed * 2].min
  337.     if @move_z > 0
  338.       @z += mv
  339.       @move_z = [@move_z - mv, 0].max
  340.     elsif @move_z < 0
  341.       @z -= mv
  342.       @move_z = [@move_z + mv, 0].min
  343.     end
  344.   end
  345.  
  346. end #class camera
  347.  
  348. #==============================================================================
  349. # ■ BattleManager
  350. #==============================================================================
  351.  
  352. class << BattleManager
  353.  
  354.   #--------------------------------------------------------------------------
  355.   # * Alias method: init_members
  356.   #--------------------------------------------------------------------------
  357.   alias oc_battlemanager_init_members_9jsla init_members
  358.   def init_members
  359.     oc_battlemanager_init_members_9jsla
  360.     if $imported[:ve_actor_battlers] || OC::OBC::SIDEVIEW_SYSTEM
  361.       $game_party.members.each do |member|
  362.         member.origin_x = member.screen_x
  363.         member.origin_y = member.screen_y
  364.       end
  365.     end
  366.   end  
  367.  
  368. end #class battlemanager
  369.  
  370. #==============================================================================
  371. # ■ Window BattleActor
  372. #==============================================================================
  373. class Window_BattleActor < Window_BattleStatus
  374.  
  375. #==============================================================================
  376. # * New Def: Ally *
  377. #------------------------------------------------------------------------------
  378. #  Returns the magnification ratio
  379. #==============================================================================
  380.   def ally
  381.     $game_party.alive_members[@index]
  382.   end  
  383.  
  384. end #class window_battleactor
  385.  
  386. #==============================================================================
  387. # ■ Scene_Battle
  388. #==============================================================================
  389.  
  390. class Scene_Battle < Scene_Base
  391.  
  392. #==============================================================================
  393. # * Accessors *
  394. #==============================================================================
  395.   attr_reader :camera
  396.  
  397. #==============================================================================
  398. # * Alias: Start *
  399. #==============================================================================
  400.   alias oc_scene_battle_start_ojmwe start
  401.   def start
  402.     @camera = Camera.new
  403.     oc_scene_battle_start_ojmwe
  404.   end
  405.  
  406. #==============================================================================
  407. # * Alias: Update_Basic *
  408. #==============================================================================
  409.  
  410.   alias oc_scene_battle_update_m32ls update_basic
  411.   def update_basic
  412.     @camera.update
  413.     oc_scene_battle_update_m32ls
  414.    
  415.     if @subject.is_a?(Game_Enemy) && $imported[:ve_actor_battlers] == nil && OC::OBC::SIDEVIEW_SYSTEM == false
  416.       # If the target is an enemy, camera will zoom on it
  417.       @camera.move_target(@subject)
  418.     else
  419.       # If enemy selection window is up, camera will zoom on target
  420.       return if @enemy_window.nil?
  421.       if @enemy_window.active
  422.         if $imported["YEA-BattleEngine"]
  423.           if @enemy_window.select_all?
  424.             @camera.move_center
  425.           else
  426.             @camera.move_target(@enemy_window.enemy)
  427.           end
  428.         else
  429.           @camera.move_target(@enemy_window.enemy)
  430.         end
  431.       else
  432.         if ($imported[:ve_actor_battlers] || OC::OBC::SIDEVIEW_SYSTEM == true) && !@actor_window.nil? && @actor_window.active
  433.           @camera.move_target(@actor_window.ally)
  434.         else
  435.           # If enemy selection window is not up, camera will zoom out
  436.           @camera.move(Graphics.width / 2, Graphics.height / 3, -48)
  437.         end
  438.       end
  439.     end
  440.   end
  441.  
  442. end #class scene_battle
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement