From e3c8e3bcc266f56eef91226b425daab3421d0551 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Sun, 25 Apr 2021 14:58:12 +0000 Subject: [PATCH] Added a bunch of tweens to the Animator, replaced :ease_in_out tween for Intro state engine logo to :swing_to --- lib/cyberarm_engine/animator.rb | 167 ++++++++++++++++++++- lib/cyberarm_engine/builtin/intro_state.rb | 2 +- 2 files changed, 166 insertions(+), 3 deletions(-) diff --git a/lib/cyberarm_engine/animator.rb b/lib/cyberarm_engine/animator.rb index 5be93df..7ec8cf4 100644 --- a/lib/cyberarm_engine/animator.rb +++ b/lib/cyberarm_engine/animator.rb @@ -43,14 +43,177 @@ module CyberarmEngine Gosu::Color.from_ahsv(alpha, hue, saturation, value) end - # NOTE: Use this for future reference? https://github.com/danro/easing-js/blob/master/easing.js + # Tween functions based on those provided here: https://github.com/danro/easing-js/blob/master/easing.js + # Under MIT / BSD def tween_linear(t) t end - def tween_ease_in_out(t) + def tween_ease_in_quad(t) + t ** 2 + end + + def tween_ease_out_quad(t) + -((t - 1) ** 2) -1 + end + + def tween_ease_in_out_quad(t) + return 0.5 * (t ** 2) if (t /= 0.5) < 1 + return -0.5 * ((t -= 2) * t - 2) + end + + def tween_ease_in_cubic(t) + t ** 3 + end + + def tween_ease_out_cubic(t) + ((t - 1) ** 3) + 1 + end + + def tween_ease_in_out_cubic(t) + return 0.5 * (t ** 3) if ((t /= 0.5) < 1) + return 0.5 * ((t - 2) ** 3) + 2 + end + + def tween_ease_in_quart(t) + t ** 4 + end + + def tween_ease_out_quart(t) + -((t - 1) ** 4) - 1 + end + + def tween_ease_in_out_quart(t) + return 0.5 * (t ** 4) if ((t /= 0.5) < 1) + return -0.5 * ((t -= 2) * (t ** 3) - 2) + end + + def tween_ease_in_quint(t) + t ** 5 + end + + def tween_ease_out_quint(t) + ((t - 1) ** 5) + 1 + end + + def tween_ease_in_out_quint(t) + return 0.5 * (t ** 5) if ((t /= 0.5) < 1) + return 0.5 * ((t - 2) ** 5) + 2 + end + + def tween_ease_in(t) # sine + -Math.cos(t * (Math::PI / 2)) + 1 + end + + def tween_ease_out(t) # sine + Math.sin(t * (Math::PI / 2)) + end + + def tween_ease_in_out(t) # sine (-0.5 * (Math.cos(Math::PI * t) - 1)) end + + def tween_ease_in_expo(t) + (t == 0) ? 0 : 2 ** 10 * (t - 1) + end + + def tween_ease_out_expo(t) + (t == 1) ? 1 : -(2 ** -10 * t) + 1 + end + + def tween_ease_in_out_expo(t) + return 0 if (t == 0) + return 1 if (t == 1) + return 0.5 * (2 ** 10 * (t - 1)) if ((t /= 0.5) < 1) + return 0.5 * (-(2 ** -10 * (t -= 1)) + 2) + end + + def tween_ease_in_circ(t) + -(Math.sqrt(1 - (t * t)) - 1) + end + + def tween_ease_out_circ(t) + Math.sqrt(1 - ((t - 1) ** 2)) + end + + def tween_ease_in_out_circ(t) + return -0.5 * (Math.sqrt(1 - t * t) - 1) if ((t /= 0.5) < 1) + return 0.5 * (Math.sqrt(1 - (t -= 2) * t) + 1) + end + + def tween_ease_in_back(t) + s = 1.70158 + t * t * ((s + 1) * t - s) + end + + def tween_ease_out_back(t) + s = 1.70158 + (t = t - 1) * t * ((s + 1) * t + s) + 1 + end + + def tween_ease_in_out_back(t) + s = 1.70158 + return 0.5 * (t * t * (((s *= (1.525)) + 1) * t - s)) if ((t /= 0.5) < 1) + return 0.5 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + end + + def tween_elastic(t) + -1 * (4 ** (-8 * t)) * Math.sin((t * 6 - 1) * (2 * Math::PI) / 2) + 1 + end + + def tween_swing_from_to(t) + s = 1.70158 + return 0.5 * (t * t * (((s *= (1.525)) + 1) * t - s)) if (t /= 0.5) < 1 + return 0.5 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + end + + def tween_swing_from(t) + s = 1.70158; + t * t * ((s + 1) * t - s) + end + + def tween_swing_to(t) + s = 1.70158 + (t -= 1) * t * ((s + 1) * t + s) + 1 + end + + def tween_bounce(t) + if (t < (1 / 2.75)) + (7.5625 * t * t) + elsif (t < (2 / 2.75)) + (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75) + elsif (t < (2.5 / 2.75)) + (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375) + else + (7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375) + end + end + + def tween_bounce_past(t) + if (t < (1 / 2.75)) + # missing "2 -"? + (7.5625 * t * t) + elsif (t < (2 / 2.75)) + 2 - (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75) + elsif (t < (2.5 / 2.75)) + 2 - (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375) + else + 2 - (7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375) + end + end + + def tween_ease_from_to(t) + return 0.5 * (t ** 4) if ((t /= 0.5) < 1) + return -0.5 * ((t -= 2) * (t ** 3) - 2) + end + + def tween_ease_from(t) + t ** 4 + end + + def tween_ease_to(t) + t ** 0.25 + end end end diff --git a/lib/cyberarm_engine/builtin/intro_state.rb b/lib/cyberarm_engine/builtin/intro_state.rb index 7a16616..10bda76 100644 --- a/lib/cyberarm_engine/builtin/intro_state.rb +++ b/lib/cyberarm_engine/builtin/intro_state.rb @@ -33,7 +33,7 @@ module CyberarmEngine Animator.new(start_time: base_time + 500, duration: 1_000, from: 0.0, to: 1.0, tween: :ease_in_out), Animator.new(start_time: Gosu.milliseconds + @continue_after - 1_000, duration: 1_000, from: 0.0, to: 1.0, tween: :ease_in_out), - Animator.new(start_time: Gosu.milliseconds + 250, duration: 500, from: 0.0, to: 1.0, tween: :ease_in_out) # CyberarmEngine LOGO + Animator.new(start_time: Gosu.milliseconds + 250, duration: 500, from: 0.0, to: 1.0, tween: :swing_to) # CyberarmEngine LOGO ] end