Advertisement
TheSixth

Smooth Animation Module

Feb 17th, 2017
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.54 KB | None | 0 0
  1. #===============================================================================
  2. # * [ACE] Smooth Animation Module
  3. #===============================================================================
  4. # * Made by: Sixth (www.rpgmakervxace.net, www.forums.rpgmakerweb.com)
  5. # * Version: 1.0
  6. # * Updated: 07/10/2016
  7. # * Requires: -------
  8. #-------------------------------------------------------------------------------
  9. # * < Change Log >
  10. #-------------------------------------------------------------------------------
  11. # * Version 1.0 (07/10/2016)
  12. #   - Initial release.
  13. #-------------------------------------------------------------------------------
  14. # * < Description >
  15. #-------------------------------------------------------------------------------
  16. # * This script contains all the easing types of the animations.
  17. #   You can add new ones by yourself if you want.
  18. #-------------------------------------------------------------------------------
  19. # * < Installation >
  20. #-------------------------------------------------------------------------------
  21. # * Place this script between Materials and Main!
  22. #-------------------------------------------------------------------------------
  23. # * < Compatibility Info >
  24. #-------------------------------------------------------------------------------
  25. # * No known incompatibilities.
  26. #-------------------------------------------------------------------------------
  27. # * < Known Issues >
  28. #-------------------------------------------------------------------------------
  29. # * No known issues.
  30. #-------------------------------------------------------------------------------
  31. # * < Terms of Use >
  32. #-------------------------------------------------------------------------------
  33. # * Free to use for whatever purposes you want.
  34. # * Credit me (Sixth) in your game, pretty please! :P
  35. # * Most of this module uses Robert Penner's easing methods, so you should also
  36. #   credit him for this module! I guess...
  37. # * Posting modified versions of this script is allowed as long as you notice me
  38. #   about it with a link to it!
  39. #===============================================================================
  40. $imported = {} if $imported.nil?
  41. $imported["SixthABSSmoothAnimMod"] = true
  42. #===============================================================================
  43. # Settings:
  44. #
  45. # You can create your own type of animations by making new methods inside the
  46. # below module. After making your new methods, you can use them by using their
  47. # names either as a symbol or string in the script calls provided in the
  48. # Smooth Animation System script
  49. #
  50. # Do NOT make duplicate method names!
  51. #
  52. # The argument of these methods look like this:
  53. # b(ase):     Base (starting) value of the attribute.
  54. # c(hange):   Difference (change) to the target value.
  55. # t(ime):     Elapsed animation time.
  56. # d(uration): Total animation time.
  57. #
  58. # Won't explain how the already included methods work, but if you are
  59. # interested, you can learn about that here: http://robertpenner.com/easing/
  60. # The default methods are based on Robert Penner's Easing methods!
  61. #===============================================================================
  62.  
  63. module SmoothAnim
  64.  
  65.   # Random type selection
  66.   # Add all effects to the array below!
  67.   def self.rand_anim(*types)
  68.     if types.empty?
  69.       types = [
  70.         :linear,
  71.         :quad_in, :quad_out, :quad_in_out,
  72.         :cubic_in, :cubic_out, :cubic_in_out,
  73.         :quart_in, :quart_out, :quart_in_out,
  74.         :quint_in, :quint_out, :quint_in_out,
  75.         :sine_in, :sine_out, :sine_in_out,
  76.         :exp_in, :exp_out, :exp_in_out,
  77.         :circ_in, :circ_out, :circ_in_out,
  78.         :bounce_in, :bounce_out, :bounce_in_out,
  79.         :back_in, :back_out, :back_in_out,
  80.         :elastic_in, :elastic_out,
  81.         :sinus,
  82.       ]
  83.     end
  84.     return types.sample
  85.   end
  86.  
  87.   # Random changes - chaotic, not usable! It is here for testing purposes only!
  88.   def random(b, c, t, d)
  89.     method(rand_anim).call(b, c, t, d)
  90.   end
  91.  
  92.   # Linear (simple move/change - default)
  93.   def self.linear(b, c, t, d)
  94.     return c * t / d + b
  95.   end
  96.  
  97.   def self.quad_in(b, c, t, d)
  98.     return c*(t/=d)*t + b
  99.   end
  100.    
  101.   def self.quad_out(b, c, t, d)
  102.     return -c*(t/=d)*(t-2) + b
  103.   end
  104.  
  105.   def self.quad_in_out(b, c, t, d)
  106.     return c/2*t*t + b if ((t/=d/2) < 1)
  107.     return -c/2 * ((t-=1)*(t-2) - 1) + b
  108.   end
  109.  
  110.   def self.cubic_in(b, c, t, d)
  111.     return c*(t/=d)*t*t + b
  112.   end
  113.    
  114.   def self.cubic_out(b, c, t, d)
  115.     return c*((t=t/d-1)*t*t + 1) + b
  116.   end
  117.  
  118.   def self.cubic_in_out(b, c, t, d)
  119.     return c/2*t*t*t + b if ((t/=d/2) < 1)
  120.     return c/2*((t-=2)*t*t + 2) + b
  121.   end
  122.  
  123.   def self.quart_in(b, c, t, d)
  124.     return c*(t/=d)*t*t*t + b
  125.   end
  126.    
  127.   def self.quart_out(b, c, t, d)
  128.     return -c * ((t=t/d-1)*t*t*t - 1) + b
  129.   end
  130.  
  131.   def self.quart_in_out(b, c, t, d)
  132.     return c/2*t*t*t*t + b if ((t/=d/2) < 1)
  133.     return -c/2 * ((t-=2)*t*t*t - 2) + b
  134.   end
  135.          
  136.   def self.quint_in(b, c, t, d)
  137.     return c*(t/=d)*t*t*t*t + b
  138.   end
  139.  
  140.   def self.quint_out(b, c, t, d)
  141.     return c*((t=t/d-1)*t*t*t*t + 1) + b
  142.   end
  143.  
  144.   def self.quint_in_out(b, c, t, d)
  145.     return c/2*t*t*t*t*t + b if ((t/=d/2) < 1)
  146.     return c/2*((t-=2)*t*t*t*t + 2) + b
  147.   end  
  148.  
  149.   def self.sine_in(b, c, t, d)
  150.     return -c * Math.cos(t/d * (Math::PI/2)) + c + b
  151.   end
  152.    
  153.   def self.sine_out(b, c, t, d)
  154.     return c * Math.sin(t/d * (Math::PI/2)) + b
  155.   end
  156.  
  157.   def self.sine_in_out(b, c, t, d)
  158.     return -c/2 * (Math.cos(Math::PI*t/d) - 1) + b
  159.   end
  160.  
  161.   def self.exp_in(b, c, t, d)
  162.     return (t==0) ? b : c * (2 ** (10 * (t/d - 1))) + b
  163.   end
  164.    
  165.   def self.exp_out(b, c, t, d)
  166.     return (t==d) ? b+c : c * (-2**(-10 * t/d) + 1) + b
  167.   end
  168.    
  169.   def self.exp_in_out(b, c, t, d)
  170.     return b if t == 0
  171.     return b + c if t == d
  172.     return (c/2) * 2**(10 * (t-1)) + b if ((t /= d/2) < 1)
  173.     return (c/2) * (-2**(-10 * t-=1) + 2) + b
  174.   end
  175.  
  176.   def self.circ_in(b, c, t, d)
  177.     return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b
  178.   end
  179.  
  180.   def self.circ_out(b, c, t, d)
  181.     return c * Math.sqrt(1 - (t=t/d-1)*t) + b
  182.   end
  183.    
  184.   def self.circ_in_out(b, c, t, d)
  185.     return -c/2 * (Math.sqrt(1 - t*t) - 1) + b if ((t/=d/2) < 1)
  186.     return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b
  187.   end
  188.        
  189.   def self.bounce_in(b, c, t, d)
  190.     return c - bounce_out(0, c, d-t, d) + b
  191.   end
  192.  
  193.   def self.bounce_out(b, c, t, d)
  194.     if (t /= d) < (1/2.75)
  195.       return c * 7.5625*t*t + b
  196.     elsif t < (2/2.75)
  197.       return c * (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75) + b
  198.     elsif t < (2.5/2.75)
  199.       return c * (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375) + b
  200.     else
  201.       return c * (7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375) + b
  202.     end
  203.   end
  204.  
  205.   def self.bounce_in_out(b, c, t, d)
  206.     if(t < d/2)
  207.       return bounce_in(0, c, t*2, d) * 0.5 + b
  208.     else
  209.       return bounce_out(0, c, t*2 - d, d) * 0.5 + c*0.5 + b
  210.     end
  211.   end
  212.  
  213.   def self.back_in(b, c, t, d)
  214.     s = 1.70158
  215.     return c * (t/=d) * t * ((s+1) * t - s) + b
  216.   end
  217.  
  218.   def self.back_out(b, c, t, d)
  219.     s = 1.70158
  220.     return c * ((t=t/d-1) * t * ((s+1) * t + s) + 1) + b
  221.   end
  222.  
  223.   def self.back_in_out(b, c, t, d)
  224.     s = 1.70158
  225.     if (t /= d/2) < 1
  226.       return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b
  227.     else
  228.       return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b
  229.     end
  230.   end
  231.  
  232.   def self.elastic_in(b, c, t, d)
  233.     a, x, s = 5.0, d * 0.3, 0.0
  234.     return b if (t == 0)
  235.     return b + c if ((t /= d) >= 1)
  236.     if(a < c.abs)
  237.       a = c
  238.       s = x / 4.0
  239.     else
  240.       s = x / (2 * Math::PI) * Math.asin(c / a)
  241.     end
  242.     return -(a * (2 ** (10 * (t -= 1))) * Math.sin((t * d - s) * (2 * Math::PI) / x)) + b
  243.   end
  244.  
  245.   def self.elastic_out(b, c, t, d)
  246.     a, x, s = 0.1, d * 0.3, 0.0
  247.     return b if (t == 0)
  248.     return b + c if ((t /= d) >= 1)
  249.     if(a < c.abs)
  250.       a = c
  251.       s = x / 4.0
  252.     else
  253.       s = x / (2 * Math::PI) * Math.asin(c / a)
  254.     end
  255.     return a * (2 ** (-10 * t)) * Math.sin((t * d - s) * (2 * Math::PI) / x) + c + b
  256.   end
  257.  
  258.   # Custom animations:
  259.  
  260.   def self.sinus(b, c, t, d)
  261.     ts = (t /= d) * t
  262.     tc = ts * t
  263.     return b+c*(-17*tc*ts + 42.5*ts*ts + -51*tc + 34*ts + -7.5*t)
  264.   end  
  265.  
  266.   def self.high_out(b, c, t, d)
  267.     ts = (t /= d) * t
  268.     tc = ts * t
  269.     return b+c*(11.85*tc*ts + -40.85*ts*ts + 52.6*tc + -33.9*ts + 11.3*t)
  270.   end
  271.  
  272. end
  273. #==============================================================================
  274. # !!END OF SCRIPT - OHH, NOES!!
  275. #==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement