Advertisement
DarkSoul144

DSI Tween

May 28th, 2021
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. module DSI_Tween
  2.  
  3. def self.linear(t, b, c, d)
  4. return b + c * (t/d.to_f)
  5. end
  6.  
  7. def self.easeInOutQuart(t, b, c, d)
  8. if (t/=d/2.0) < 1
  9. return c/2*t*t*t*t + b
  10. end
  11. return -c/2 * ((t-=2)*t*t*t - 2) + b
  12. end
  13.  
  14. end
  15.  
  16. class Scene_Base
  17.  
  18. alias_method(:dsi_tween_update, :update)
  19.  
  20. def tween(obj, params, duration, &callback)
  21. @tween_objects ||= []
  22. tween_obj = {}
  23. tween_obj[:target] = obj
  24. tween_obj[:params] = []
  25. params.each do |data|
  26. begin_value = eval("tween_obj[:target].#{data[0].to_s}")
  27. change = data[1] - begin_value
  28. obj = [data[0].to_s, begin_value, change]
  29. tween_obj[:params] << obj
  30. end
  31. tween_obj[:duration] = duration
  32. tween_obj[:timer] = 0
  33. tween_obj[:callback] = callback if callback
  34. @tween_objects << tween_obj
  35. tween_obj[:easing] = :easeInOutQuart
  36. return tween_obj
  37. end
  38.  
  39. def update
  40. dsi_tween_update
  41. update_tween_objects
  42. end
  43.  
  44. def update_tween_objects
  45. return unless @tween_objects
  46. new_tweens = []
  47. @tween_objects.each do |tween_object|
  48. target = tween_object[:target]
  49. params = tween_object[:params]
  50. duration = tween_object[:duration]
  51. if tween_object[:timer] < duration
  52. params.each do |data|
  53. case tween_object[:easing]
  54. when :easeInOutQuart
  55. new_value = DSI_Tween.easeInOutQuart(tween_object[:timer], data[1], data[2], duration)
  56. when :linear
  57. new_value = DSI_Tween.linear(tween_object[:timer], data[1], data[2], duration)
  58. end
  59. eval("target.#{data[0]} = new_value")
  60. end
  61. tween_object[:timer] += 1
  62. new_tweens << tween_object
  63. else
  64. tween_object[:callback].call() if tween_object[:callback]
  65. end
  66. end
  67. @tween_objects = new_tweens
  68. end
  69.  
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement