khanhdu

TSBS - Battle Camera Preview

Nov 8th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 20.97 KB | None | 0 0
  1. #==============================================================================
  2. # TSBS Addon - Battle Camera
  3. # Version : 0.8
  4. # Language : English
  5. # Requires : Theolized SBS version 1.3c
  6. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  7. # Contact :
  8. #------------------------------------------------------------------------------
  9. # *> http://www.rpgmakerid.com
  10. # *> http://www.rpgmakervxace.net
  11. # *> http://theolized.blogspot.com
  12. #==============================================================================
  13. ($imported = {})[:TSBS_Camera] = true
  14. #==============================================================================
  15. # Change Logs:
  16. # -----------------------------------------------------------------------------
  17. # 2013.08.28 - Finished initial version 0.8
  18. #==============================================================================
  19. =begin
  20.  
  21.   ===================
  22.   || Introduction ||
  23.   -------------------
  24.   This script adds battle camera effect where you could move the camera focus
  25.   or even zoom the battle screen.
  26.  
  27.   =================
  28.   || How to use ||
  29.   -----------------
  30.   Put this script below TSBS implementation part.
  31.   To call the camera effect, select one of these command format.
  32.  
  33.   1st Format --> [:camera, scope, [x, y], dur, zoom, (method)],
  34.   2nd Format --> [:camera, "Camera Key"],
  35.  
  36.   Note if you use the first format :
  37.   > Scope   : Scope position where camera should focus. There're 5 avalaible
  38.               options can be used. They're
  39.               [0: Target] [1: Self] [2: All Enemies] [3: All Allies]
  40.               [4: Everyone] [5: Screen]
  41.              
  42.   > [x, y]  : Relative position from the scope
  43.   > dur     : Camera move duration in frame
  44.   > zoom    : Camera zoom value. Should filled by float. e.g, 1.0 or 1.5
  45.   > method  : Movement method. Choose between :linear, :circle, or :cubic
  46.  
  47.   Example :
  48.   [:camera, 0, [0, 0], 56, 1.15],
  49.   [:camera, 0, [0, 0], 56, 1.15, :cubic],
  50.  
  51.   Note if you use the second format :
  52.   "Camera Key" is similar as action key. It stored in CAM_PRESETUP in
  53.   configuration part below.
  54.  
  55.   To reset the position of camera, call this command
  56.   Format --> [:cam_reset, dur, (method)],
  57.  
  58.   =====================
  59.   || Developer note ||
  60.   ---------------------
  61.   > May have a glitch if used together with realistic shadow
  62.   > Not yet tested with extensive testing. So, it might has glitches or bugs
  63.   > May continue to develop this script after upgrading TSBS to the next
  64.     version. Since the current structure of TSBS version(1.3c) isn't good
  65.     enough.
  66.  
  67.   ===================
  68.   || Terms of use ||
  69.   -------------------
  70.   Credit me, TheoAllen. You are free to edit this script by your own. As long
  71.   as you don't claim it yours. For commercial purpose, don't forget to give me
  72.   a free copy of the game.
  73.  
  74.   =====================
  75.   || Special Thanks ||
  76.   ---------------------
  77.   - Galenmereth / Tor Damian Design for easing movement algorithm
  78.   - Journey Battle System / Tankentai VXA as inspiration
  79.   - Anyone who requested this feature
  80.  
  81. =end
  82. #==============================================================================
  83. # * Editable region
  84. #==============================================================================
  85. module TSBS # <-- Do not touch
  86.  #============================================================================
  87.   DEFAULT_CAM_METHOD = :cubic
  88.  #----------------------------------------------------------------------------
  89.  # Default camera movement method. There're three avalaible movement methods.
  90.  # Choose between them
  91.  # > :linear
  92.  # > :cubic
  93.  # > :circle
  94.  #
  95.  # For constant movement, choose :linear
  96.  # For smoother movement, choose either :cubic or :circle
  97.  #============================================================================
  98.  # Camera pre setup. Made for easier call
  99.  #============================================================================
  100.   CAM_PRESETUP = {
  101. # "Camera Key"  => [scope,[x, y], dur, zoom, (method)]
  102.   "Everyone"    => [4    ,[0, 0],  56,  1.0],
  103.   "Screen"      => [5    ,[0, 0],  56,  1.0],
  104.  
  105.   } # <-- don't touch
  106.  
  107.  #============================================================================
  108.  # Camera command call. Used to call camera function like [:camera, ...]
  109.  #----------------------------------------------------------------------------
  110.   CAMERA_MOVE     = :camera
  111.   CAMERA_RESET    = :cam_reset
  112.  #=============================================================================
  113. end # <-- Do not touch
  114. #==============================================================================
  115. # * End of editable region
  116. #------------------------------------------------------------------------------
  117. # Below this line may dangerous to enter. There're many monster inside. Do not
  118. # enter unless you're pretty confident or have sufficient skills.
  119. #==============================================================================
  120. module TSBS
  121. #==============================================================================
  122. # ** TSBS::Camera
  123. #------------------------------------------------------------------------------
  124. #  This class handles camera metadata
  125. #==============================================================================
  126.   class Camera
  127.     attr_accessor :zoom
  128.     attr_accessor :x
  129.     attr_accessor :y
  130.     #--------------------------------------------------------------------------
  131.     # * Initialize
  132.     #--------------------------------------------------------------------------
  133.     def initialize
  134.       reset_camera
  135.       clear_movement_flag
  136.     end
  137.     #--------------------------------------------------------------------------
  138.     # * Reset camera metadata
  139.     #--------------------------------------------------------------------------
  140.     def reset_camera
  141.       @zoom = 1.0
  142.       @x = 0.0
  143.       @y = 0.0
  144.     end
  145.     #--------------------------------------------------------------------------
  146.     # * Clear movement flag
  147.     #--------------------------------------------------------------------------
  148.     def clear_movement_flag
  149.       @fiber = nil
  150.       @total_dur = 0
  151.       @duration = 0
  152.       @ori_x = 0.0
  153.       @ori_y = 0.0
  154.       @ori_zoom = 0.0
  155.       @target_x = 0.0
  156.       @target_y = 0.0
  157.       @target_zoom = 0.0
  158.     end
  159.     #--------------------------------------------------------------------------
  160.     # * Move camera
  161.     #--------------------------------------------------------------------------
  162.     def move_camera(battlers, x, y, dur, zoom, method)
  163.       clear_movement_flag
  164.       @duration = dur
  165.       if battlers
  166.         size = battlers.size
  167.         point_x = battlers.inject(0) {|r, battler| r + battler.rel_x}.to_f/size
  168.         point_y = battlers.inject(0) {|r, battler| r + battler.rel_y}.to_f/size
  169.       else
  170.         point_x = 0
  171.         point_y = 0
  172.       end
  173.       @ori_x = @x
  174.       @ori_y = @y
  175.       @ori_zoom = @zoom
  176.       @target_x = (point_x + x).to_f
  177.       @target_y = (point_y + y).to_f
  178.       @target_zoom = zoom
  179.       @fiber = Fiber.new { update_camera(method) }
  180.     end
  181.     #--------------------------------------------------------------------------
  182.     # * The adjustment position of X for an object
  183.     #--------------------------------------------------------------------------
  184.     def adjust_x(obj)
  185.       dist = Graphics.width/2 - obj.x
  186.       return (@x * @zoom) + (dist * (@zoom - 1.0))
  187.     end
  188.     #--------------------------------------------------------------------------
  189.     # * The adjustment position of Y for an object
  190.     #--------------------------------------------------------------------------
  191.     def adjust_y(obj)
  192.       dist = Graphics.height/2 - obj.y
  193.       return (@y * @zoom) + (dist * (@zoom - 1.0))
  194.     end
  195.     #--------------------------------------------------------------------------
  196.     # * Update. Called once per frame
  197.     #--------------------------------------------------------------------------
  198.     def update
  199.       @fiber.resume if @fiber
  200.     end
  201.     #--------------------------------------------------------------------------
  202.     # * Update camera movement
  203.     #--------------------------------------------------------------------------
  204.     def update_camera(method_name)
  205.       @duration.times do |t|
  206.         @x = send(method_name, t, @ori_x, @target_x - @ori_x, @duration)
  207.         @y = send(method_name, t, @ori_y, @target_y - @ori_y, @duration)
  208.         @zoom = send(method_name, t, @ori_zoom, @target_zoom - @ori_zoom,
  209.           @duration)
  210.         Fiber.yield
  211.       end
  212.       @x = @target_x
  213.       @y = @target_y
  214.       @zoom = @target_zoom
  215.       clear_movement_flag
  216.     end
  217.     # -------------------------------------------------------------------------
  218.     # * Easing movement method : Linear
  219.     # -------------------------------------------------------------------------
  220.     def linear(time, start, change, total_time)
  221.       return change * time / total_time + start
  222.     end
  223.     # -------------------------------------------------------------------------
  224.     # * Easing movement method : Circle
  225.     # -------------------------------------------------------------------------
  226.     def circle(time, start, change, total_time)
  227.       return change * Math.sqrt(1 - (time=time/total_time.to_f-1)*time) + start
  228.     end
  229.     # -------------------------------------------------------------------------
  230.     # * Easing movement method : Cubic
  231.     # -------------------------------------------------------------------------
  232.     def cubic(time, start, change, total_time)
  233.       time /= total_time.to_f
  234.       time -= 1
  235.       return change*(time*time*time + 1) + start
  236.     end
  237.   end
  238. end
  239.  
  240. #==============================================================================
  241. # ** DataManager
  242. #------------------------------------------------------------------------------
  243. #  This module manages the database and game objects. Almost all of the
  244. # global variables used by the game are initialized by this module.
  245. #==============================================================================
  246.  
  247. class << DataManager
  248.   # --------------------------------------------------------------------------
  249.   # Alias method : Create game objects
  250.   # --------------------------------------------------------------------------  
  251.   alias tsbs_cam_create_obj create_game_objects
  252.   def create_game_objects
  253.     tsbs_cam_create_obj
  254.     $tsbs_camera = TSBS::Camera.new
  255.   end
  256.  
  257. end
  258.  
  259. #==============================================================================
  260. # ** Game_BattleBack
  261. #------------------------------------------------------------------------------
  262. #  This class handles battleback metadata to simulate battleback replacement.
  263. # Instance of this class included within the Game_Temp class
  264. #==============================================================================
  265.  
  266. class Game_BattleBack
  267.   attr_accessor :x
  268.   attr_accessor :y
  269.   # --------------------------------------------------------------------------
  270.   # * Initialize
  271.   # --------------------------------------------------------------------------  
  272.   def initialize
  273.     @x = Graphics.width/2
  274.     @y = Graphics.height/2
  275.   end
  276.   # --------------------------------------------------------------------------
  277.   # * Screen X display
  278.   # --------------------------------------------------------------------------
  279.   def screen_x
  280.     @x - $tsbs_camera.adjust_x(self)
  281.   end
  282.   # --------------------------------------------------------------------------
  283.   # * Screen Y display
  284.   # --------------------------------------------------------------------------
  285.   def screen_y
  286.     @y - $tsbs_camera.adjust_y(self)
  287.   end
  288.  
  289. end
  290.  
  291. #==============================================================================
  292. # ** Game_Temp
  293. #------------------------------------------------------------------------------
  294. #  This class handles temporary data that is not included with save data.
  295. # The instance of this class is referenced by $game_temp.
  296. #==============================================================================
  297.  
  298. class Game_Temp
  299.   attr_reader :battleback
  300.   # --------------------------------------------------------------------------
  301.   # Alias method : Initialize
  302.   # --------------------------------------------------------------------------
  303.   alias tsbs_cam_init initialize
  304.   def initialize
  305.     tsbs_cam_init
  306.     @battleback = Game_BattleBack.new
  307.   end
  308.  
  309. end
  310.  
  311. #==============================================================================
  312. # ** Game_Battler
  313. #------------------------------------------------------------------------------
  314. #  A battler class with methods for sprites and actions added. This class
  315. # is used as a super class of the Game_Actor class and Game_Enemy class.
  316. #==============================================================================
  317.  
  318. class Game_Battler
  319.   # --------------------------------------------------------------------------
  320.   # New method : Relative X position from center
  321.   # --------------------------------------------------------------------------
  322.   def rel_x
  323.     return x - Graphics.width/2
  324.   end
  325.   # --------------------------------------------------------------------------
  326.   # New method: Relative Y position from center
  327.   # --------------------------------------------------------------------------
  328.   def rel_y
  329.     return y - Graphics.height/2
  330.   end
  331.   # --------------------------------------------------------------------------
  332.   # Alias method : Custom sequence handler
  333.   # --------------------------------------------------------------------------
  334.   alias tsbs_cam_custom_sequence custom_sequence_handler
  335.   def custom_sequence_handler
  336.     tsbs_cam_custom_sequence
  337.     case @acts[0]
  338.     when CAMERA_MOVE;   setup_camera_move
  339.     when CAMERA_RESET;  setup_camera_reset
  340.     end
  341.   end
  342.   # --------------------------------------------------------------------------
  343.   # New method : Setup camera move
  344.   # --------------------------------------------------------------------------
  345.   def setup_camera_move
  346.     if @acts[1].is_a?(String)
  347.       camera = CAM_PRESETUP[@acts[1]]
  348.       if camera.nil?
  349.         ErrorSound.play
  350.         text = "Camera error on : #{@used_sequence}\n" +
  351.         "Uninitialized camera constant \"#{@acts[1]}\""
  352.         msgbox text
  353.         exit
  354.       end
  355.       @acts = @act[0] + camera
  356.     end
  357.     return TSBS.error(@acts[0], 4, @used_sequence) if @acts.size < 5
  358.     case @acts[1]
  359.     when 0 # Current target
  360.       battlers = target_array
  361.     when 1 # Self
  362.       battlers = [self]
  363.     when 2 # All Enemies
  364.       battlers = opponents_unit.alive_members
  365.     when 3 # All Allies
  366.       battlers = friends_unit.alive_members
  367.     when 4 # Everyone
  368.       battlers = opponents_unit.alive_members + friends_unit.alive_members
  369.     when 5 # Screen
  370.       battlers = nil
  371.     else
  372.       battlers = nil
  373.     end
  374.     unless @acts[2].is_a?(Array)
  375.       ErrorSound.play
  376.       msgbox "Camera error on : #{@used_sequence}\n" +
  377.       "Second parameter should be array"
  378.       exit
  379.     end
  380.     point_x = @acts[2][0]
  381.     point_y = @acts[2][1]
  382.     dur = @acts[3]
  383.     zoom = @acts[4]
  384.     method = @acts[5] || DEFAULT_CAM_METHOD
  385.     unless $tsbs_camera.respond_to?(method)
  386.       ErrorSound.play
  387.       msgbox "Camera error on : #{@used_sequence}\nWrong method name"
  388.       exit
  389.     end
  390.     $tsbs_camera.move_camera(battlers, point_x, point_y, dur, zoom, method)
  391.   end
  392.   # --------------------------------------------------------------------------
  393.   # New method : Setup camera reset
  394.   # --------------------------------------------------------------------------
  395.   def setup_camera_reset
  396.     return TSBS.error(@acts[0], 1, @used_sequence) if @acts.size < 2
  397.     $tsbs_camera.move_camera(nil, 0, 0, @acts[1], 1.0, @acts[2] ||
  398.       DEFAULT_CAM_METHOD)
  399.   end
  400.   # --------------------------------------------------------------------------
  401.   # Alias method : Shadow Y positioning
  402.   # --------------------------------------------------------------------------
  403.   alias tsbs_cam_shadow_y shadow_y
  404.   def shadow_y
  405.     tsbs_cam_shadow_y - $tsbs_camera.adjust_y(self)
  406.   end
  407.  
  408. end
  409.  
  410. #==============================================================================
  411. # ** Game_Actor
  412. #------------------------------------------------------------------------------
  413. #  This class handles actors. It is used within the Game_Actors class
  414. # ($game_actors) and is also referenced from the Game_Party class ($game_party)
  415. #==============================================================================
  416.  
  417. class Game_Actor
  418.   alias tsbs_cam_screen_x screen_x
  419.   alias tsbs_cam_screen_y screen_y
  420.   # --------------------------------------------------------------------------
  421.   # Alias method : Screen X
  422.   # --------------------------------------------------------------------------
  423.   def screen_x
  424.     tsbs_cam_screen_x - $tsbs_camera.adjust_x(self)
  425.   end
  426.   # --------------------------------------------------------------------------
  427.   # Alias method : Screen Y
  428.   # --------------------------------------------------------------------------
  429.   def screen_y
  430.     tsbs_cam_screen_y - $tsbs_camera.adjust_y(self)
  431.   end
  432.  
  433. end
  434.  
  435. #==============================================================================
  436. # ** Game_Enemy
  437. #------------------------------------------------------------------------------
  438. #  This class handles enemies. It used within the Game_Troop class
  439. # ($game_troop).
  440. #==============================================================================
  441.  
  442. class Game_Enemy
  443.   alias tsbs_cam_screen_x screen_x
  444.   alias tsbs_cam_screen_y screen_y
  445.   # --------------------------------------------------------------------------
  446.   # Alias method : Screen X
  447.   # --------------------------------------------------------------------------
  448.   def screen_x
  449.     tsbs_cam_screen_x - $tsbs_camera.adjust_x(self)
  450.   end
  451.   # --------------------------------------------------------------------------
  452.   # Alias method : Screen Y
  453.   # --------------------------------------------------------------------------
  454.   def screen_y
  455.     tsbs_cam_screen_y - $tsbs_camera.adjust_y(self)
  456.   end
  457.  
  458. end
  459.  
  460. #==============================================================================
  461. # ** Sprite_BattlerIcon
  462. #------------------------------------------------------------------------------
  463. #  This sprite is used to display battler's Icon. It observes icon key from
  464. # Game_Battler class and automatically changes sprite display when triggered.
  465. #==============================================================================
  466.  
  467. class Sprite_BattlerIcon
  468.   # --------------------------------------------------------------------------
  469.   # Alias method : Update
  470.   # --------------------------------------------------------------------------
  471.   alias tsbs_cam_update update
  472.   def update
  473.     tsbs_cam_update
  474.     self.zoom_x = self.zoom_y = $tsbs_camera.zoom
  475.   end
  476.  
  477. end
  478.  
  479. #==============================================================================
  480. # ** Sprite_Battler
  481. #------------------------------------------------------------------------------
  482. #  This sprite is used to display battlers. It observes an instance of the
  483. # Game_Battler class and automatically changes sprite states.
  484. #==============================================================================
  485.  
  486. class Sprite_Battler
  487.   # --------------------------------------------------------------------------
  488.   # Alias method : Update
  489.   # --------------------------------------------------------------------------
  490.   alias tsbs_cam_update update
  491.   def update
  492.     tsbs_cam_update
  493.     zoom = $tsbs_camera.zoom
  494.     self.zoom_x = zoom
  495.     self.zoom_y = zoom
  496.   end
  497.  
  498. end
  499.  
  500. #==============================================================================
  501. # ** Spriteset_Battle
  502. #------------------------------------------------------------------------------
  503. #  This class brings together battle screen sprites. It's used within the
  504. # Scene_Battle class.
  505. #==============================================================================
  506.  
  507. class Spriteset_Battle
  508.   # --------------------------------------------------------------------------
  509.   # Alias method : Update
  510.   # --------------------------------------------------------------------------
  511.   alias tsbs_cam_update update
  512.   def update
  513.     tsbs_cam_update
  514.     @back1_sprite.x = @back2_sprite.x = $game_temp.battleback.screen_x
  515.     @back1_sprite.y = @back2_sprite.y = $game_temp.battleback.screen_y
  516.     @back1_sprite.zoom_x = @back2_sprite.zoom_x = $tsbs_camera.zoom
  517.     @back1_sprite.zoom_y = @back2_sprite.zoom_y = $tsbs_camera.zoom
  518.   end
  519.  
  520. end
  521.  
  522. #==============================================================================
  523. # ** Scene_Battle
  524. #------------------------------------------------------------------------------
  525. #  This class performs battle screen processing.
  526. #==============================================================================
  527.  
  528. class Scene_Battle
  529.   # --------------------------------------------------------------------------
  530.   # Alias method : Update basic
  531.   # --------------------------------------------------------------------------
  532.   alias tsbs_cam_update_basic update_basic
  533.   def update_basic
  534.     tsbs_cam_update_basic
  535.     $tsbs_camera.update
  536.   end
  537.  
  538. end
Advertisement
Add Comment
Please, Sign In to add comment