Guest User

Untitled

a guest
Jul 30th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 38.30 KB | None | 0 0
  1. #==============================================================================
  2. # ** TDD Ease Module
  3. #------------------------------------------------------------------------------
  4. # Version:  1.0.2
  5. # Date:     07/31/2014
  6. # Author:   Galenmereth / Tor Damian Design
  7. #
  8. # Changelog
  9. # =========
  10. # 07/31/2014 - Fixed @interpreter bug in Game_CharacterBase extension
  11. # Introduced the TDD module namespace and Ease_Object instead of using a hash
  12. #
  13. # Description
  14. # ===========
  15. # This module is used to apply an easing algorithm to an object's parameters
  16. # over X amount of frames. Easing methods can be extended through adding
  17. # static methods to the Easing module. The default easing method is
  18. # Easing::LINEAR and is identical to the default easing provided in VXAce
  19. #
  20. # How to use in event editor:
  21. # ===========================
  22. # Included with this script is an extension to the Game_Picture class that lets
  23. # you change the easing method of the event editor commands Move Picture and
  24. # Tint Picture.
  25. #
  26. # Before calling Move Picture and Tint Picture for a picture in the event editor,
  27. # make a script call like this:
  28. #   Game_Picture.easing = Easing::ELASTIC_OUT
  29. # This will set the easing method to the ELASTIC_OUT algorithm. When you erase
  30. # any picture, the easing method is reset to the default (Easing::LINEAR by
  31. # default)
  32. #
  33. # You can provide different easing methods for different events as well, by
  34. # setting Game_Picture.easing between each call. It is remembered for each
  35. # event call the moment it starts.
  36. #
  37. # If you wish to set the default easing, you use:
  38. #   Game_Picture.easing_default = Easing::QUAD_IN
  39. #
  40. # This is the list of included easing methods (the part that go after Easing::)
  41. #
  42. #
  43. #
  44. #
  45. ###### CALLS:
  46. #
  47. #
  48. #
  49. #
  50. #### SCRIPT CALLS:
  51. #
  52. #
  53. #
  54. #
  55. ### Game_Picture.easing_default = Easing::setting      Set all tag 4 pics
  56. #
  57. ### Game_Pictue.easing = Easing::setting               Setup tag 4 pics
  58. #
  59. #
  60. #
  61. #
  62. #### MOVE ROUTE SCRIPT CALLS:
  63. #
  64. #
  65. #
  66. #
  67. ### ease_opacity(opacity, frames, Easing::setting)   Opacity tag 4 events
  68. #
  69. ### ease_moveto(x, y, frames, Easing::setting)       Move tag 4 events
  70. #
  71. ### ease_moveto_char(char, frames, Easing::setting)  Move to char tag 4 events
  72. #
  73. #
  74. #
  75. #
  76. ###### SETTINGS:
  77. #
  78. #
  79. #
  80. #
  81. # * LINEAR (default, and like the default in VXAce)
  82. #
  83. # * BACK_IN
  84. # * BACK_IN_OUT
  85. # * BACK_OUT
  86. #
  87. # * BOUNCE_IN
  88. # * BOUNCE_IN_OUT
  89. # * BOUNCE_OUT
  90. #
  91. # * CIRC_IN
  92. # * CIRC_IN_OUT
  93. # * CIRC_OUT
  94. #
  95. # * CUBIC_IN
  96. # * CUBIC_IN_OUT
  97. # * CUBIC_OUT
  98. #
  99. # * ELASTIC_IN
  100. # * ELASTIC_IN_OUT
  101. # * ELASTIC_OUT
  102. #
  103. # * EXPO_IN
  104. # * EXPO_IN_OUT
  105. # * EXPO_OUT
  106. #
  107. # * QUAD_IN
  108. # * QUAD_IN_OUT
  109. # * QUAD_OUT
  110. #
  111. # Credit:
  112. # =======
  113. # - Galenmereth / Tor Damian Design
  114. #
  115. # License:
  116. # ========
  117. # Free for non-commercial and commercial use. Credit greatly appreciated but
  118. # not required. Share script freely with everyone, but please retain this
  119. # description area unless you change the script completely. Thank you.
  120. #==============================================================================
  121.  
  122. #/////////////////////////////////////////////////////////////////////////
  123. #// DANGER - BELOW IS ARCANE UNLESS YOU KNOW WHAT YOU'RE DOING - DANGER //
  124. #/////////////////////////////////////////////////////////////////////////
  125.  
  126. $imported = {} if $imported.nil?
  127. $imported["TDD Easing Core"] = true
  128. module TDD
  129.   module Ease
  130.     @@easings = []
  131.    
  132.     #--------------------------------------------------------------------------
  133.     # * Ease Parameters To Given Attribute Values
  134.     # Params:
  135.     # =======
  136.     # - target (Object)
  137.     #     An object which has the attributes listed in attributes and which
  138.     #     you want to effect with an easing FROM its current values TO
  139.     #     the values given in the attributes hash
  140.     # - frames (Integer)
  141.     #     The amount of frames to apply the easing over
  142.     # - attributes (Hash)
  143.     #     Hash of attributes and target values for the attributes you wish to
  144.     #     affect on the target object
  145.     # - options (Hash)
  146.     #     Hash of optional options:
  147.     #       :easing           =>  Easing method to use (default = Easing::LINEAR)
  148.     #       :observers        =>  Array of observer objects (default = nil)
  149.     #       :call_on_update   =>  Method to call on observers every update tick
  150.     #                             (default = false)
  151.     #       :call_on_complete =>  Method to call on observers when easing is
  152.     #                             complete (default = false)
  153.     # Example:
  154.     # ========
  155.     # target_obj = {:x => 0, :y => 0}
  156.     # target_attributes = {:x => 250, :y => 100}
  157.     # options = {
  158.     #   :easing         => Easing::BOUNCE_IN,
  159.     #   :observers      => [self],
  160.     #   :call_on_update => :update (can also be writtens as "update")
  161.     # }
  162.     # TDD::Ease.to(target_obj, 60, target_attributes, options)
  163.     #
  164.     # This would ease the movement of target_object from 0x and 0y to 250x and
  165.     # 100y over 60 frames (1 second) using the Easing::BOUNCE_IN easing method.
  166.     # Every easing update (each frame) would call the method update on the
  167.     # observer objects (self, in this case).
  168.     #
  169.     # Comments:
  170.     # =========
  171.     # Depending on how your objects or classes are set up, you may need to use
  172.     # an "intermediary" object (like the target_obj hash in the example) to
  173.     # hold the attributes, then apply the values form this "intermediary" object
  174.     # to the attributes of the class when the :call_on_update method is called.
  175.     # The reason for this is that you may not necessarily want the easing
  176.     # function to write directly to a caller class. Look at the Game_Picture
  177.     # extension for an example of this necessity, where I didn't want to make
  178.     # any of the read-only attributes writable to implement easing.
  179.     #--------------------------------------------------------------------------
  180.     def self.to(target, frames, attributes={}, options={})
  181.       register_ease(:to, target, frames, attributes, options)
  182.     end
  183.  
  184.     #--------------------------------------------------------------------------
  185.     # * Ease Parameters From Given Attribute Values To Current Attribute Values
  186.     # Params:
  187.     # =======
  188.     # - target (Object)
  189.     #     An object which has the attributes listed in attributes and which
  190.     #     you want to effect with an easing FROM its current values TO
  191.     #     the values given in the attributes hash
  192.     # - frames (Integer)
  193.     #     The amount of frames to apply the easing over
  194.     # - attributes (Hash)
  195.     #     Hash of attributes and target values for the attributes you wish to
  196.     #     affect on the target object
  197.     # - options (Hash)
  198.     #     Hash of optional options:
  199.     #       :easing           =>  Easing method to use (default = Easing::LINEAR)
  200.     #       :observers        =>  Array of observer objects (default = nil)
  201.     #       :call_on_update   =>  Method to call on observers every update tick
  202.     #                             (default = false)
  203.     #       :call_on_complete =>  Method to call on observers when easing is
  204.     #                             complete (default = false)
  205.     # Example:
  206.     # ========
  207.     # target_obj = {:x => 0, :y => 0}
  208.     # origin_attributes = {:x => 250, :y => 100}
  209.     # options = {
  210.     #   :easing         => Easing::BOUNCE_IN,
  211.     #   :observers      => [self],
  212.     #   :call_on_update => :update (can also be writtens as "update")
  213.     # }
  214.     # TDD::Ease.to(target_obj, 60, origin_attributes, options)
  215.     #
  216.     # This would ease the movement of target_object from 250x and 100y to 0x and
  217.     # 0y over 60 frames (1 second) using the Easing::BOUNCE_IN easing method.
  218.     # Every easing update (each frame) would call the method update on the
  219.     # observer objects (self, in this case).
  220.     #
  221.     # You might scratch your head and wonder "well, gosh, what's the point of this
  222.     # when we already have the to method?"
  223.     # This method is useful in instances where it's easier or more convenient to place
  224.     # an object where you want it to end up, then set it to ease from an origin
  225.     # point.
  226.     #
  227.     # Comments:
  228.     # =========
  229.     # Look at comments for the "to" method
  230.     #--------------------------------------------------------------------------
  231.     def self.from(target, frames, attributes={}, options={})
  232.       register_ease(:from, target, frames, attributes, options)
  233.     end
  234.  
  235.     #--------------------------------------------------------------------------
  236.     # * Updates All Easings Every Engine Frame Tick
  237.     #
  238.     # Comments:
  239.     # =========
  240.     # Called by Scene_Base when the extension is in place for it.
  241.     #--------------------------------------------------------------------------
  242.     def self.update
  243.       @@easings.each_with_index do |ease, index|
  244.         target = ease.target
  245.         ease.attributes.each_pair do |attribute, value|
  246.           attribute_origin = ease.attributes_origin[attribute]
  247.           case ease.method
  248.           when :to
  249.             from = attribute_origin
  250.             to = value
  251.           when :from
  252.             from = value
  253.             to = attribute_origin
  254.           end
  255.           # Move instantly if frames is 1
  256.           if ease.frames == 1
  257.             target[attribute] = to
  258.           else
  259.             target[attribute] = Easing.send(ease.easing, ease.frame, from, to - from, ease.frames)
  260.           end
  261.         end
  262.  
  263.         ease.observers.each{|o| o.send(ease.call_on_update, ease)} if ease.call_on_update
  264.  
  265.         ease.frame += 1
  266.         if ease.frame > ease.frames
  267.           @@easings.delete_at(index)
  268.           ease.observers.each{|o| o.send(ease.call_on_complete, ease)} if ease.call_on_complete
  269.         end
  270.       end
  271.     end
  272.  
  273.     private
  274.     #--------------------------------------------------------------------------
  275.     # * Register An Ease Object in Queue
  276.     #--------------------------------------------------------------------------
  277.     def self.register_ease(method, target, frames, attributes, options)
  278.       @@easings << TDD::Ease_Object.new(method, target, frames, attributes, options)
  279.     end
  280.  
  281.   end
  282. end
  283. #==============================================================================
  284. # ** TDD Ease Object
  285. #------------------------------------------------------------------------------
  286. # Version:  1.0.0
  287. # Date:     08/12/2013
  288. # Author:   Galenmereth / Tor Damian Design
  289. #
  290. # Description
  291. # ===========
  292. # This object is used to store the easing info for each active ease in the
  293. # system
  294. #
  295. # Credit:
  296. # =======
  297. # - Galenmereth / Tor Damian Design
  298. #
  299. # License:
  300. # ========
  301. # Free for non-commercial and commercial use. Credit greatly appreciated but
  302. # not required. Share script freely with everyone, but please retain this
  303. # description area unless you change the script completely. Thank you.
  304. #==============================================================================
  305. module TDD
  306.   class Ease_Object
  307.     attr_reader   :method
  308.     attr_reader   :target
  309.     attr_reader   :frames
  310.     attr_reader   :attributes
  311.     attr_reader   :attributes_origin
  312.  
  313.     attr_accessor :frame
  314.    
  315.     def initialize(method, target, frames, attributes={}, options={})
  316.       @method     = method
  317.       @target     = target
  318.       @frames     = frames
  319.       @attributes = attributes
  320.       @options    = default_options.merge(options)
  321.  
  322.       # Current frame starts at 0
  323.       @frame = 0
  324.  
  325.       # Set origin of attributes for ease depending on method
  326.       @attributes_origin = {}
  327.       @attributes.each_pair do |attr, val|
  328.         case method
  329.         when :to
  330.           @attributes_origin[attr] = target[attr]
  331.         when :from
  332.           @attributes_origin[attr] = value
  333.           attributes[attr] = target[attr]
  334.         end
  335.       end
  336.     end
  337.  
  338.     def easing
  339.       @options[:easing]
  340.     end
  341.  
  342.     def observers
  343.       @options[:observers]
  344.     end
  345.  
  346.     def call_on_update
  347.       @options[:call_on_update]
  348.     end
  349.  
  350.     def call_on_complete
  351.       @options[:call_on_complete]
  352.     end
  353.  
  354.     private
  355.     def default_options
  356.       {
  357.         :easing => Easing::LINEAR,
  358.         :observers => [],
  359.         :call_on_update => false,
  360.         :call_on_complete => false
  361.       }
  362.     end
  363.   end
  364. end
  365. #==============================================================================
  366. # ** Easing EXTENSION
  367. #------------------------------------------------------------------------------
  368. # Extended for: TDD Easing Script
  369. # ===============================
  370. # This extension adds 3 new easing methods to the Easing module:
  371. # * BACK_IN
  372. # * BACK_OUT
  373. # * BACK_IN_OUT
  374. #
  375. # How to use:
  376. # ===========
  377. # Use Game_Picture.easing = Easing::BACK_IN (or any of the other three methods
  378. # listed above) to apply before performing moving or tinting of the Game_Picture
  379. # class.
  380. #
  381. # Credit:
  382. # =======
  383. # - Galenmereth / Tor Damian Design
  384. #
  385. # License:
  386. # ========
  387. # Free for non-commercial and commercial use. Credit greatly appreciated but
  388. # not required. Share script freely with everyone, but please retain this
  389. # description area unless you change the script completely. Thank you.
  390. #==============================================================================
  391. module Easing
  392.   BACK_IN     = "back_in"
  393.   BACK_OUT    = "back_out"
  394.   BACK_IN_OUT = "back_in_out"
  395.  
  396.   # This is the intensity of the back "sling" effect. Higher = stronger
  397.   SLING       = 1.70158
  398.  
  399.   def self.back_in(t, b, c, d)
  400.     s = SLING
  401.     d = d.to_f
  402.     return c*(t/=d)*t*((s+1)*t - s) + b
  403.   end
  404.  
  405.   def self.back_out(t, b, c, d)
  406.     s = SLING
  407.     d = d.to_f
  408.     return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b
  409.   end
  410.  
  411.   def self.back_in_out(t, b, c, d)
  412.     s = SLING
  413.     c = c.to_f
  414.     d = d.to_f
  415.     if ((t/=d/2) < 1)
  416.       return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b
  417.     else
  418.       return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b
  419.     end
  420.   end
  421. end
  422. #==============================================================================
  423. # ** Easing EXTENSION
  424. #------------------------------------------------------------------------------
  425. # Extended for: TDD Easing Script
  426. # ===============================
  427. # This extension adds 3 new easing methods to the Easing module:
  428. # * BOUNCE_IN
  429. # * BOUNCE_OUT
  430. # * BOUNCE_IN_OUT
  431. #
  432. # How to use:
  433. # ===========
  434. # Use Game_Picture.easing = Easing::BOUNCE_IN (or any of the other three methods
  435. # listed above) to apply before performing moving or tinting of the Game_Picture
  436. # class.
  437. #
  438. # Credit:
  439. # =======
  440. # - Galenmereth / Tor Damian Design
  441. #
  442. # License:
  443. # ========
  444. # Free for non-commercial and commercial use. Credit greatly appreciated but
  445. # not required. Share script freely with everyone, but please retain this
  446. # description area unless you change the script completely. Thank you.
  447. #==============================================================================
  448. module Easing
  449.   BOUNCE_IN     = "bounce_in"
  450.   BOUNCE_OUT    = "bounce_out"
  451.   BOUNCE_IN_OUT = "bounce_in_out"
  452.  
  453.   def self.bounce_in(t, b, c, d)
  454.     return c - self::bounce_out(d-t, 0, c, d) + b
  455.   end
  456.  
  457.   def self.bounce_out(t, b, c, d)
  458.     if (t/=d.to_f) < (1/2.75)
  459.       return c*(7.5625*t*t) + b
  460.     elsif t < (2/2.75)
  461.       return c*(7.5625*(t-=(1.5/2.75))*t + 0.75) + b
  462.     elsif t < (2.5/2.75)
  463.       return c*(7.5625*(t-=(2.25/2.75))*t + 0.9375) + b
  464.     else
  465.       return c*(7.5625*(t-=(2.625/2.75))*t + 0.984375) + b
  466.     end
  467.   end
  468.  
  469.   def self.bounce_in_out(t, b, c, d)
  470.     if t < d.to_f/2
  471.       return bounce_in(t*2, 0, c, d) * 0.5 + b
  472.     else
  473.       return bounce_out(t*2-d, 0, c, d) * 0.5 + c*0.5 + b
  474.     end
  475.   end
  476. end
  477. #==============================================================================
  478. # ** Easing EXTENSION
  479. #------------------------------------------------------------------------------
  480. # Extended for: TDD Easing Script
  481. # ===============================
  482. # This extension adds 3 new easing methods to the Easing module:
  483. # * CIRC_IN
  484. # * CIRC_OUT
  485. # * CIRC_IN_OUT
  486. #
  487. # How to use:
  488. # ===========
  489. # Use Game_Picture.easing = Easing::CIRC_IN (or any of the other three methods
  490. # listed above) to apply before performing moving or tinting of the Game_Picture
  491. # class.
  492. #
  493. # Credit:
  494. # =======
  495. # - Galenmereth / Tor Damian Design
  496. #
  497. # License:
  498. # ========
  499. # Free for non-commercial and commercial use. Credit greatly appreciated but
  500. # not required. Share script freely with everyone, but please retain this
  501. # description area unless you change the script completely. Thank you.
  502. #==============================================================================
  503. module Easing
  504.   CIRC_IN     = "circ_in"
  505.   CIRC_OUT    = "circ_out"
  506.   CIRC_IN_OUT = "circ_in_out"
  507.  
  508.   def self.circ_in(t, b, c, d)
  509.     return -c * (Math.sqrt(1 - (t/=d.to_f)*t) - 1) + b
  510.   end
  511.  
  512.   def self.circ_out(t, b, c, d)
  513.     return c * Math.sqrt(1 - (t=t/d.to_f-1)*t) + b
  514.   end
  515.  
  516.   def self.circ_in_out(t, b, c, d)
  517.     d = d.to_f
  518.     c = c.to_f
  519.     if (t/=d/2) < 1
  520.       return -c/2 * (Math.sqrt(1 - t*t) - 1) + b
  521.     else
  522.       return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b
  523.     end
  524.   end
  525. end
  526. #==============================================================================
  527. # ** Easing EXTENSION
  528. #------------------------------------------------------------------------------
  529. # Extended for: TDD Easing Script
  530. # ===============================
  531. # This extension adds 3 new easing methods to the Easing module:
  532. # * CUBIC_IN
  533. # * CUBIC_OUT
  534. # * CUBIC_IN_OUT
  535. #
  536. # How to use:
  537. # ===========
  538. # Use Game_Picture.easing = Easing::CUBIC_IN (or any of the other three methods
  539. # listed above) to apply before performing moving or tinting of the Game_Picture
  540. # class.
  541. #
  542. # Credit:
  543. # =======
  544. # - Galenmereth / Tor Damian Design
  545. #
  546. # License:
  547. # ========
  548. # Free for non-commercial and commercial use. Credit greatly appreciated but
  549. # not required. Share script freely with everyone, but please retain this
  550. # description area unless you change the script completely. Thank you.
  551. #==============================================================================
  552. module Easing
  553.   CUBIC_IN      = "cubic_in"
  554.   CUBIC_OUT     = "cubic_out"
  555.   CUBIC_IN_OUT  = "cubic_in_out"
  556.  
  557.   def self.cubic_in(t, b, c, d)
  558.     t /= d.to_f
  559.     return c*t*t*t + b
  560.   end
  561.  
  562.   def self.cubic_out(t, b, c, d)
  563.     t /= d.to_f
  564.     t -= 1
  565.     return c*(t*t*t + 1) + b
  566.   end
  567.  
  568.   def self.cubic_in_out(t, b, c, d)
  569.     c = c.to_f
  570.     t /= d.to_f/2
  571.     return c/2*t*t*t + b if t < 1
  572.     t -= 2
  573.     return c/2*(t*t*t + 2) + b
  574.   end
  575. end
  576. #==============================================================================
  577. # ** Easing EXTENSION
  578. #------------------------------------------------------------------------------
  579. # Extended for: TDD Easing Script
  580. # ===============================
  581. # This extension adds 3 new easing methods to the Easing module:
  582. # * ELASTIC_IN
  583. # * ELASTIC_OUT
  584. # * ELASTIC_IN_OUT
  585. #
  586. # How to use:
  587. # ===========
  588. # Use Game_Picture.easing = Easing::ELASTIC_IN (or any of the other three methods
  589. # listed above) to apply before performing moving or tinting of the Game_Picture
  590. # class.
  591. #
  592. # Credit:
  593. # =======
  594. # - Galenmereth / Tor Damian Design
  595. #
  596. # License:
  597. # ========
  598. # Free for non-commercial and commercial use. Credit greatly appreciated but
  599. # not required. Share script freely with everyone, but please retain this
  600. # description area unless you change the script completely. Thank you.
  601. #==============================================================================
  602. module Easing
  603.   ELASTIC_IN      = "elastic_in"
  604.   ELASTIC_OUT     = "elastic_out"
  605.   ELASTIC_IN_OUT  = "elastic_in_out"
  606.  
  607.   def self.elastic_in(t, b, c, d)
  608.     d=d.to_f
  609.     c=c.to_f
  610.     s=1.70158
  611.     p=0
  612.     a=c
  613.     return b if t==0
  614.     return b+c if ((t/=d)==1)
  615.     p=d*0.3 if p==0
  616.     if a < c.abs
  617.       a=c
  618.       s=p/4
  619.     else
  620.       s = p/(2*Math::PI) * Math.asin(c/(a.nonzero? || 1))
  621.     end
  622.     return -(a*(2**(10*(t-=1))) * Math.sin( (t*d-s)*(2*Math::PI)/p )) + b
  623.   end
  624.  
  625.   def self.elastic_out(t, b, c, d)
  626.     d=d.to_f
  627.     c=c.to_f
  628.     s=1.70158
  629.     p=0
  630.     a=c
  631.     return b if t==0
  632.     return b+c if ((t/=d)==1)
  633.     p = d*0.3 if p==0
  634.     if a < c.abs
  635.       a=c
  636.       s=p/4
  637.     else
  638.       s = p/(2*Math::PI) * Math.asin(c/(a.nonzero? || 1))
  639.     end
  640.     return a*(2**(-10*t)) * Math.sin( (t*d-s)*(2*Math::PI)/p ) + c + b
  641.   end
  642.  
  643.   def self.elastic_in_out(t, b, c, d)
  644.     d=d.to_f
  645.     c=c.to_f
  646.     s=1.70158
  647.     p=0
  648.     a=c
  649.     return b if t==0
  650.     return b+c if ((t/=d/2)==2)
  651.     p=d*(0.3*1.5) if p==0
  652.     if a < c.abs
  653.       a=c
  654.       s=p/4
  655.     else
  656.       s = p/(2*Math::PI) * Math.asin(c/(a.nonzero? || 1))
  657.     end
  658.     return a*(2**(-10*(t-=1))) * Math.sin( (t*d-s)*(2*Math::PI)/p )*0.5 + c + b
  659.   end
  660.  
  661. end
  662.  
  663.  
  664. #==============================================================================
  665. # ** Easing EXTENSION
  666. #------------------------------------------------------------------------------
  667. # Extended for: TDD Easing Script
  668. # ===============================
  669. # This extension adds 3 new easing methods to the Easing module:
  670. # * EXPO_IN
  671. # * EXPO_OUT
  672. # * EXPO_IN_OUT
  673. #
  674. # How to use:
  675. # ===========
  676. # Use Game_Picture.easing = Easing::EXPO_IN (or any of the other three methods
  677. # listed above) to apply before performing moving or tinting of the Game_Picture
  678. # class.
  679. #
  680. # Credit:
  681. # =======
  682. # - Galenmereth / Tor Damian Design
  683. #
  684. # License:
  685. # ========
  686. # Free for non-commercial and commercial use. Credit greatly appreciated but
  687. # not required. Share script freely with everyone, but please retain this
  688. # description area unless you change the script completely. Thank you.
  689. #==============================================================================
  690. module Easing
  691.   EXPO_IN     = "expo_in"
  692.   EXPO_OUT    = "expo_out"
  693.   EXPO_IN_OUT = "expo_in_out"
  694.  
  695.   def self.expo_in(t, b, c, d)
  696.     return (t==0) ? b : c * (2**(10 * (t/d.to_f - 1))) + b
  697.   end
  698.  
  699.   def self.expo_out(t, b, c, d)
  700.     return (t==d) ? b+c : c * (-(2**(-10 * t/d.to_f)) + 1) + b
  701.   end
  702.  
  703.   def self.expo_in_out(t, b, c, d)
  704.     c = c.to_f
  705.     d = d.to_f
  706.     return b if t==0
  707.     return b+c if t==d
  708.     if (t/=d/2) < 1
  709.       return c/2 * (2**(10 * (t - 1))) + b
  710.     else
  711.       return c/2 * (-(2**(-10 * (t-=1))) + 2) + b
  712.     end
  713.   end
  714. end
  715. #==============================================================================
  716. # ** Easing EXTENSION
  717. #------------------------------------------------------------------------------
  718. # Extended for: TDD Easing Script
  719. # ===============================
  720. # This extension adds the LINEAR easing method to the Easing module. This is the
  721. # default easing method, which is identical to the default easing performed in
  722. # VXAce
  723. #
  724. # How to use:
  725. # ===========
  726. # Use Game_Picture.easing = Easing::LINEAR (or any of the other three methods
  727. # listed above) to apply before performing moving or tinting of the Game_Picture
  728. # class.
  729. #
  730. # Credit:
  731. # =======
  732. # - Galenmereth / Tor Damian Design
  733. #
  734. # License:
  735. # ========
  736. # Free for non-commercial and commercial use. Credit greatly appreciated but
  737. # not required. Share script freely with everyone, but please retain this
  738. # description area unless you change the script completely. Thank you.
  739. #==============================================================================
  740. module Easing
  741.   LINEAR      = "linear"
  742.  
  743.   # t = Current time (frame)
  744.   # b = Start value
  745.   # c = Desired change in value
  746.   # d = Duration total (frames)
  747.   # Returns: Value modified by t
  748.   def self.linear(t, b, c, d)
  749.     return c*t/d.to_f + b
  750.   end
  751. end
  752. #==============================================================================
  753. # ** Easing EXTENSION
  754. #------------------------------------------------------------------------------
  755. # Extended for: TDD Easing Script
  756. # ===============================
  757. # This extension adds 3 new easing methods to the Easing module:
  758. # * QUAD_IN
  759. # * QUAD_OUT
  760. # * QUAD_IN_OUT
  761. #
  762. # How to use:
  763. # ===========
  764. # Use Game_Picture.easing = Easing::QUAD_IN (or any of the other three methods
  765. # listed above) to apply before performing moving or tinting of the Game_Picture
  766. # class.
  767. #
  768. # Credit:
  769. # =======
  770. # - Galenmereth / Tor Damian Design
  771. #
  772. # License:
  773. # ========
  774. # Free for non-commercial and commercial use. Credit greatly appreciated but
  775. # not required. Share script freely with everyone, but please retain this
  776. # description area unless you change the script completely. Thank you.
  777. #==============================================================================
  778. module Easing
  779.   QUAD_IN       = "quad_in"
  780.   QUAD_OUT      = "quad_out"
  781.   QUAD_IN_OUT   = "quad_in_out"
  782.  
  783.   def self.quad_in(t, b, c, d)
  784.     t /= d.to_f
  785.     return c*t*t + b
  786.   end
  787.  
  788.   def self.quad_out(t, b, c, d)
  789.     c = c.to_f
  790.     t /= d.to_f
  791.     return -c * t*(t-2) + b;
  792.   end
  793.  
  794.   def self.quad_in_out(t, b, c, d)
  795.     c = c.to_f
  796.     t /= d.to_f/2
  797.     return c/2*t*t + b if t < 1
  798.     t -= 1
  799.     return -c/2 * (t*(t-2) - 1) + b
  800.   end
  801. end
  802. #==============================================================================
  803. # ** Scene_Base EXTENSION
  804. #------------------------------------------------------------------------------
  805. # Version:  1.0.1
  806. # Date:     08/12/2013
  807. # Author:   Galenmereth / Tor Damian Design
  808. #
  809. # Extended for: TDD Easing Script
  810. # ===============================
  811. # This calls update on the new Ease module, so that all applied easings are
  812. # updated each frame tick. Nothing else.
  813. #
  814. # Credit:
  815. # =======
  816. # - Galenmereth / Tor Damian Design
  817. #
  818. # License:
  819. # ========
  820. # Free for non-commercial and commercial use. Credit greatly appreciated but
  821. # not required. Share script freely with everyone, but please retain this
  822. # description area unless you change the script completely. Thank you.
  823. #==============================================================================
  824. class Scene_Base
  825.   #--------------------------------------------------------------------------
  826.   # * ALIAS Frame Update
  827.   # Comments:
  828.   #   Ease.update is called first so that the actual drawing performed by the
  829.   #   original update_basic calls are performed after attributes have been
  830.   #   set by active easing procedures.
  831.   #--------------------------------------------------------------------------
  832.   alias_method :tdd_easing_scene_update_basic_extension, :update_basic
  833.   def update_basic
  834.     TDD::Ease.update
  835.     tdd_easing_scene_update_basic_extension
  836.   end
  837. end
  838. #==============================================================================
  839. # ** Game_CharacterBase EXTENSION
  840. #------------------------------------------------------------------------------
  841. # Version:  1.0.2
  842. # Date:     05/16/2014
  843. # Author:   Galenmereth / Tor Damian Design
  844. #
  845. # Extended for: TDD Easing Script
  846. # ===============================
  847. # ADD INFO
  848. #
  849. # How to use:
  850. # ===========
  851. # ADD HOW TO
  852. #
  853. # Credit:
  854. # =======
  855. # - Galenmereth / Tor Damian Design
  856. #
  857. # License:
  858. # ========
  859. # Free for non-commercial and commercial use. Credit greatly appreciated but
  860. # not required. Share script freely with everyone, but please retain this
  861. # description area unless you change the script completely. Thank you.
  862. #==============================================================================
  863. $imported = {} if $imported.nil?
  864. # Is TDD Ease installed?
  865. raise "You need the TDD Easing Script to use this extension!" unless  $imported["TDD Easing Core"] || (Plugins && Plugins.has_plugin?("TDD Easing Core"))
  866. $imported["TDD Easing Core<-Game_CharacterBase"] = true
  867.  
  868. class Game_CharacterBase
  869.   @easing = false
  870.   @@interpreter = Game_Interpreter.new
  871.   #--------------------------------------------------------------------------
  872.   # * NEW Move Character To Other Character
  873.   # Params:
  874.   # =======
  875.   # - char (integer)
  876.   #     An integer noting the character to move to.
  877.   #     -1 -> player
  878.   #     0 -> the current object calling this method
  879.   #     1-x -> event id on the current map
  880.   # - frames (integer)
  881.   #     How many frames the easing should last for
  882.   # - easing (:symbol or "string")
  883.   #     The easing method to apply. Default is :linear
  884.   #--------------------------------------------------------------------------
  885.   def ease_moveto_char(char, frames, easing = :linear)
  886.     char = @@interpreter.get_character(char)
  887.     ease_moveto(char.x, char.y, frames, easing)
  888.   end
  889.  
  890.   #--------------------------------------------------------------------------
  891.   # * NEW Move To Position
  892.   # Params:
  893.   # =======
  894.   # - x (integer or "string")
  895.   #     The x position to move to.
  896.   #     if integer then absolute X position on map
  897.   #     if string then relative to current position. Examples:
  898.   #       "0" -> 0 from current x pos
  899.   #       "-5" -> -5 from current x pos
  900.   #       "5" or "+5" -> +5 from current x pos
  901.   # - y (integer or "string")
  902.   #     The y position to move to. Same rules as x param
  903.   # - frames (integer)
  904.   #     How many frames the easing should last for
  905.   #  - easing (:symbol or "string")
  906.   #     The easing method to apply. Default is :linear
  907.   #--------------------------------------------------------------------------
  908.   def ease_moveto(x, y, frames, easing = :linear)
  909.     x = @real_x + x.to_i if x.is_a? String
  910.     y = @real_y + y.to_i if y.is_a? String
  911.     @wait_count = frames - 1
  912.     @easing = true
  913.     easing_container = {
  914.       x: @real_x,
  915.       y: @real_y
  916.     }
  917.     target_attributes = {
  918.       x: x,
  919.       y: y
  920.     }
  921.     TDD::Ease.to(easing_container, frames, target_attributes, {
  922.       easing: easing,
  923.       observers: [self],
  924.       call_on_update: :ease_moveto_update,
  925.       call_on_complete: :ease_moveto_complete
  926.       })
  927.   end
  928.  
  929.   #--------------------------------------------------------------------------
  930.   # * NEW Ease To Opacity
  931.   # Params:
  932.   # =======
  933.   # - opacity (integer or "string")
  934.   #     Target opacity to ease to. If string, then relative to current opacity
  935.   #     value. See ease_moveto's x param for more on how to use relative
  936.   #     string values.
  937.   # - frames (integer)
  938.   #     How many frames the easing should last for
  939.   #  - easing (:symbol or "string")
  940.   #     The easing method to apply. Default is :linear
  941.   #--------------------------------------------------------------------------
  942.   def ease_opacity(opacity, frames, easing = :linear)
  943.     opacity = @opacity + opacity.to_i if opacity.is_a? String
  944.     @wait_count = frames - 1
  945.     @easing = true
  946.     easing_container = {opacity: @opacity}
  947.     target_attributes = {opacity: opacity}
  948.     TDD::Ease.to(easing_container, frames, target_attributes, {
  949.       easing: easing,
  950.       observers: [self],
  951.       call_on_update: :ease_opacity_update,
  952.       call_on_complete: :ease_opacity_complete
  953.       })
  954.   end
  955.  
  956.   #--------------------------------------------------------------------------
  957.   # * NEW Update Display Position (used by ease_moveto ease)
  958.   #--------------------------------------------------------------------------
  959.   def ease_moveto_update(ease_obj)
  960.     last_real_x = @real_x
  961.     last_real_y = @real_y
  962.     easing_container = ease_obj.target
  963.     @real_x = easing_container[:x]
  964.     @real_y = easing_container[:y]
  965.     increase_steps
  966.     update_scroll(last_real_x, last_real_y) if self.instance_of? Game_Player
  967.   end
  968.  
  969.   #--------------------------------------------------------------------------
  970.   # * NEW Finalize And Update Map Position (used by ease_moveto ease)
  971.   #--------------------------------------------------------------------------
  972.   def ease_moveto_complete(ease_obj)
  973.     @x = @real_x
  974.     @y = @real_y
  975.     @easing = false
  976.   end
  977.  
  978.   #--------------------------------------------------------------------------
  979.   # * NEW Update Opacity (used by ease_opacity ease)
  980.   #--------------------------------------------------------------------------
  981.   def ease_opacity_update(ease_obj)
  982.     @opacity = ease_obj.target[:opacity]
  983.   end
  984.  
  985.   #--------------------------------------------------------------------------
  986.   # * NEW Finalize Opacity Ease (used by ease_opacity ease)
  987.   #--------------------------------------------------------------------------
  988.   def ease_opacity_complete(ease_obj)
  989.     @easing = false
  990.   end
  991.  
  992.   #--------------------------------------------------------------------------
  993.   # * NEW Check If Easing?
  994.   #--------------------------------------------------------------------------
  995.   def easing?
  996.     @easing
  997.   end
  998.  
  999.   #--------------------------------------------------------------------------
  1000.   # * ALIAS Moving?
  1001.   #--------------------------------------------------------------------------
  1002.   alias :tdd_easing_moving? :moving?
  1003.   def moving?
  1004.     return easing? ? false : tdd_easing_moving?
  1005.   end
  1006.  
  1007. end
  1008. #==============================================================================
  1009. # ** Game_Picture EXTENSION
  1010. #------------------------------------------------------------------------------
  1011. # Version:  1.0.1
  1012. # Date:     08/12/2013
  1013. # Author:   Galenmereth / Tor Damian Design
  1014. #
  1015. # Extended for: TDD Easing Script
  1016. # ===============================
  1017. # This extension changes the default transition easing for Move Picture and
  1018. # Tint Picture, so that it uses the TDD Easing script motion instead.
  1019. #
  1020. # How to use:
  1021. # ===========
  1022. # Use Game_Picture.easing= to set an easing method before calling move or
  1023. # start_tone_change, either through scripts or through the use of Move Picture
  1024. # and Tint Picture event calls. Look at self.easing and self.easing_default
  1025. # for more details
  1026. #
  1027. # Credit:
  1028. # =======
  1029. # - Galenmereth / Tor Damian Design
  1030. #
  1031. # License:
  1032. # ========
  1033. # Free for non-commercial and commercial use. Credit greatly appreciated but
  1034. # not required. Share script freely with everyone, but please retain this
  1035. # description area unless you change the script completely. Thank you.
  1036. #==============================================================================
  1037. class Game_Picture
  1038.   # Global default easing method; can be set using Game_Picture.easing_default=
  1039.   @@easing_method_default = Easing::LINEAR
  1040.  
  1041.   # By default, the current easing method is set to the default
  1042.   @@easing_method = @@easing_method_default
  1043.  
  1044.   # List of variables to use for easing movement
  1045.   @@easing_move_attributes = %w(
  1046.     x
  1047.     y
  1048.     zoom_x
  1049.     zoom_y
  1050.     opacity
  1051.     duration)
  1052.  
  1053.   # List of variables to use for easing tone change
  1054.   @@easing_tint_attributes = %w(
  1055.     red
  1056.     green
  1057.     blue
  1058.     gray
  1059.     )
  1060.  
  1061.   #--------------------------------------------------------------------------
  1062.   # * ALIAS Move Picture
  1063.   #--------------------------------------------------------------------------
  1064.   alias_method :tdd_easing_move_extension, :move
  1065.   def move(origin, x, y, zoom_x, zoom_y, opacity, blend_type, duration)
  1066.     tdd_easing_move_extension(origin, x, y, zoom_x, zoom_y, opacity, blend_type, duration)
  1067.  
  1068.     target_attributes = {}
  1069.     easing_container = {}
  1070.     @@easing_move_attributes.each do |attr|
  1071.       easing_container[attr] = instance_variable_get("@#{attr}")
  1072.       target_attributes[attr] = eval(attr)
  1073.     end
  1074.     TDD::Ease.to(easing_container, duration, target_attributes, {
  1075.       :easing => @@easing_method,
  1076.       :observers => [self],
  1077.       :call_on_update => :update_move
  1078.       })
  1079.   end
  1080.  
  1081.   #--------------------------------------------------------------------------
  1082.   # * ALIAS Start Changing Color Tone
  1083.   #--------------------------------------------------------------------------
  1084.   alias_method :tdd_easing_start_tone_change_extension, :start_tone_change
  1085.   def start_tone_change(tone, duration)
  1086.     tdd_easing_start_tone_change_extension(tone, duration)
  1087.  
  1088.     easing_container = {}
  1089.     target_attributes = {}
  1090.     @@easing_tint_attributes.each do |attr|
  1091.       easing_container[attr] = @tone.send(attr)
  1092.       target_attributes[attr] = @tone_target.send(attr)
  1093.     end
  1094.  
  1095.     TDD::Ease.to(easing_container, duration, target_attributes, {
  1096.       :easing => @@easing_method,
  1097.       :observers => [self],
  1098.       :call_on_update => :update_tone_change
  1099.       })
  1100.   end
  1101.  
  1102.   #--------------------------------------------------------------------------
  1103.   # * ALIAS Erase Picture
  1104.   # Comments:
  1105.   #   The class static variable @@easing_method is reset to
  1106.   #   @@easing_method_default upon erasure.
  1107.   #--------------------------------------------------------------------------
  1108.   alias_method :tdd_easing_erase_extension, :erase
  1109.   def erase
  1110.     tdd_easing_erase_extension
  1111.     @@easing_method = @@easing_method_default
  1112.   end
  1113.  
  1114.   #--------------------------------------------------------------------------
  1115.   # * OVERWRITE Frame Update
  1116.   # Comments:
  1117.   #   update_move and update_tone_change left commented out to make it clear
  1118.   #   that these have been disabled
  1119.   #--------------------------------------------------------------------------
  1120.   def update
  1121.     # update_move
  1122.     # update_tone_change
  1123.     update_rotate
  1124.   end
  1125.  
  1126.   #--------------------------------------------------------------------------
  1127.   # * ALIAS Update Picture Move
  1128.   # Comments:
  1129.   #   In case other scripts want to call update_move for any reason,
  1130.   #   I make it check for the ease_obj from the easing; if it's not there,
  1131.   #   it uses the default update_move method
  1132.   #-------------------------------------------------------------------------
  1133.   alias_method :tdd_easing_update_move_extension, :update_move
  1134.   def update_move(ease_obj = nil)
  1135.     unless ease_obj
  1136.       tdd_easing_update_move_extension
  1137.       return
  1138.     end
  1139.     easing_container = ease_obj.target
  1140.     @@easing_move_attributes.each do |attr|
  1141.       instance_variable_set("@#{attr}", easing_container[attr])
  1142.     end
  1143.   end
  1144.  
  1145.   #--------------------------------------------------------------------------
  1146.   # * ALIAS Update Color Tone Change
  1147.   # Comments:
  1148.   #   In case other scripts want to call update_tone_change for any reason,
  1149.   #   I make it check for the ease_obj from the easing; if it's not there,
  1150.   #   it uses the default update_tone_change method
  1151.   #--------------------------------------------------------------------------
  1152.   alias_method :tdd_easing_update_tone_change_extension, :update_tone_change
  1153.   def update_tone_change(ease_obj)
  1154.     unless ease_obj
  1155.       tdd_easing_update_tone_change_extension
  1156.       return
  1157.     end
  1158.     easing_container = ease_obj.target
  1159.     @@easing_tint_attributes.each do |attr|
  1160.       @tone.send("#{attr}=", easing_container[attr])
  1161.     end
  1162.   end
  1163.  
  1164.   #--------------------------------------------------------------------------
  1165.   # * NEW Static Method Set Easing
  1166.   #--------------------------------------------------------------------------
  1167.   def self.easing=(easing_method)
  1168.     @@easing_method = easing_method
  1169.   end
  1170.  
  1171.   #--------------------------------------------------------------------------
  1172.   # * NEW Static Method Set Easing Default for Game_Picture class
  1173.   #--------------------------------------------------------------------------
  1174.   def self.easing_default=(easing_method)
  1175.     @@easing_method_default = easing_method
  1176.   end
  1177. end
Advertisement
Add Comment
Please, Sign In to add comment