Class: CyberarmEngine::Animator

Inherits:
Object
  • Object
show all
Defined in:
lib/cyberarm_engine/animator.rb

Instance Method Summary collapse

Constructor Details

#initialize(start_time:, duration:, from:, to:, tween: :linear, &block) ⇒ Animator

Returns a new instance of Animator.



3
4
5
6
7
8
9
10
# File 'lib/cyberarm_engine/animator.rb', line 3

def initialize(start_time:, duration:, from:, to:, tween: :linear, &block)
  @start_time = start_time
  @duration = duration
  @from = from.dup
  @to = to.dup
  @tween = tween
  @block = block
end

Instance Method Details

#color_hsv_transition(from = @from, to = @to, tween = @tween) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/cyberarm_engine/animator.rb', line 37

def color_hsv_transition(from = @from, to = @to, tween = @tween)
  hue = transition(from.hue, to.hue, tween)
  saturation = transition(from.saturation, to.saturation, tween)
  value = transition(from.value, to.value, tween)
  alpha = transition(from.alpha, to.alpha, tween)

  Gosu::Color.from_ahsv(alpha, hue, saturation, value)
end

#color_transition(from = @from, to = @to, _tween = @tween) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/cyberarm_engine/animator.rb', line 28

def color_transition(from = @from, to = @to, _tween = @tween)
  r = transition(from.red, to.red)
  g = transition(from.green, to.green)
  b = transition(from.blue, to.blue)
  a = transition(from.alpha, to.alpha)

  Gosu::Color.rgba(r, g, b, a)
end

#complete?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/cyberarm_engine/animator.rb', line 20

def complete?
  progress >= 1.0
end

#progressObject



16
17
18
# File 'lib/cyberarm_engine/animator.rb', line 16

def progress
  ((Gosu.milliseconds - @start_time) / @duration.to_f).clamp(0.0, 1.0)
end

#transition(from = @from, to = @to, tween = @tween) ⇒ Object



24
25
26
# File 'lib/cyberarm_engine/animator.rb', line 24

def transition(from = @from, to = @to, tween = @tween)
  from + (to - from) * send("tween_#{tween}", progress)
end

#tween_bounce(t) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/cyberarm_engine/animator.rb', line 181

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

#tween_bounce_past(t) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/cyberarm_engine/animator.rb', line 193

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

#tween_ease_from(t) ⇒ Object



211
212
213
# File 'lib/cyberarm_engine/animator.rb', line 211

def tween_ease_from(t)
  t ** 4
end

#tween_ease_from_to(t) ⇒ Object



206
207
208
209
# File 'lib/cyberarm_engine/animator.rb', line 206

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

#tween_ease_in(t) ⇒ Object

sine



105
106
107
# File 'lib/cyberarm_engine/animator.rb', line 105

def tween_ease_in(t) # sine
  -Math.cos(t * (Math::PI / 2)) + 1
end

#tween_ease_in_back(t) ⇒ Object



145
146
147
148
# File 'lib/cyberarm_engine/animator.rb', line 145

def tween_ease_in_back(t)
  s = 1.70158
  t * t * ((s + 1) * t - s)
end

#tween_ease_in_circ(t) ⇒ Object



132
133
134
# File 'lib/cyberarm_engine/animator.rb', line 132

def tween_ease_in_circ(t)
  -(Math.sqrt(1 - (t * t)) - 1)
end

#tween_ease_in_cubic(t) ⇒ Object



66
67
68
# File 'lib/cyberarm_engine/animator.rb', line 66

def tween_ease_in_cubic(t)
  t ** 3
end

#tween_ease_in_expo(t) ⇒ Object



117
118
119
# File 'lib/cyberarm_engine/animator.rb', line 117

def tween_ease_in_expo(t)
  (t == 0) ? 0 : 2 ** 10 * (t - 1)
end

#tween_ease_in_out(t) ⇒ Object

sine



113
114
115
# File 'lib/cyberarm_engine/animator.rb', line 113

def tween_ease_in_out(t) # sine
  (-0.5 * (Math.cos(Math::PI * t) - 1))
end

#tween_ease_in_out_back(t) ⇒ Object



155
156
157
158
159
# File 'lib/cyberarm_engine/animator.rb', line 155

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

#tween_ease_in_out_circ(t) ⇒ Object



140
141
142
143
# File 'lib/cyberarm_engine/animator.rb', line 140

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

#tween_ease_in_out_cubic(t) ⇒ Object



74
75
76
77
# File 'lib/cyberarm_engine/animator.rb', line 74

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

#tween_ease_in_out_expo(t) ⇒ Object



125
126
127
128
129
130
# File 'lib/cyberarm_engine/animator.rb', line 125

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

#tween_ease_in_out_quad(t) ⇒ Object



61
62
63
64
# File 'lib/cyberarm_engine/animator.rb', line 61

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

#tween_ease_in_out_quart(t) ⇒ Object



87
88
89
90
# File 'lib/cyberarm_engine/animator.rb', line 87

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

#tween_ease_in_out_quint(t) ⇒ Object



100
101
102
103
# File 'lib/cyberarm_engine/animator.rb', line 100

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

#tween_ease_in_quad(t) ⇒ Object



53
54
55
# File 'lib/cyberarm_engine/animator.rb', line 53

def tween_ease_in_quad(t)
  t ** 2
end

#tween_ease_in_quart(t) ⇒ Object



79
80
81
# File 'lib/cyberarm_engine/animator.rb', line 79

def tween_ease_in_quart(t)
  t ** 4
end

#tween_ease_in_quint(t) ⇒ Object



92
93
94
# File 'lib/cyberarm_engine/animator.rb', line 92

def tween_ease_in_quint(t)
  t ** 5
end

#tween_ease_out(t) ⇒ Object

sine



109
110
111
# File 'lib/cyberarm_engine/animator.rb', line 109

def tween_ease_out(t) # sine
  Math.sin(t * (Math::PI / 2))
end

#tween_ease_out_back(t) ⇒ Object



150
151
152
153
# File 'lib/cyberarm_engine/animator.rb', line 150

def tween_ease_out_back(t)
  s = 1.70158
  (t = t - 1) * t * ((s + 1) * t + s) + 1
end

#tween_ease_out_circ(t) ⇒ Object



136
137
138
# File 'lib/cyberarm_engine/animator.rb', line 136

def tween_ease_out_circ(t)
  Math.sqrt(1 - ((t - 1) ** 2))
end

#tween_ease_out_cubic(t) ⇒ Object



70
71
72
# File 'lib/cyberarm_engine/animator.rb', line 70

def tween_ease_out_cubic(t)
  ((t - 1) ** 3) + 1
end

#tween_ease_out_expo(t) ⇒ Object



121
122
123
# File 'lib/cyberarm_engine/animator.rb', line 121

def tween_ease_out_expo(t)
  (t == 1) ? 1 : -(2 ** -10 * t) + 1
end

#tween_ease_out_quad(t) ⇒ Object



57
58
59
# File 'lib/cyberarm_engine/animator.rb', line 57

def tween_ease_out_quad(t)
  -((t - 1) ** 2) -1
end

#tween_ease_out_quart(t) ⇒ Object



83
84
85
# File 'lib/cyberarm_engine/animator.rb', line 83

def tween_ease_out_quart(t)
  -((t - 1) ** 4) - 1
end

#tween_ease_out_quint(t) ⇒ Object



96
97
98
# File 'lib/cyberarm_engine/animator.rb', line 96

def tween_ease_out_quint(t)
  ((t - 1) ** 5) + 1
end

#tween_ease_to(t) ⇒ Object



215
216
217
# File 'lib/cyberarm_engine/animator.rb', line 215

def tween_ease_to(t)
  t ** 0.25
end

#tween_elastic(t) ⇒ Object



161
162
163
# File 'lib/cyberarm_engine/animator.rb', line 161

def tween_elastic(t)
  -1 * (4 ** (-8 * t)) * Math.sin((t * 6 - 1) * (2 * Math::PI) / 2) + 1
end

#tween_linear(t) ⇒ Object

Tween functions based on those provided here: github.com/danro/easing-js/blob/master/easing.js Under MIT / BSD



49
50
51
# File 'lib/cyberarm_engine/animator.rb', line 49

def tween_linear(t)
  t
end

#tween_swing_from(t) ⇒ Object



171
172
173
174
# File 'lib/cyberarm_engine/animator.rb', line 171

def tween_swing_from(t)
  s = 1.70158;
  t * t * ((s + 1) * t - s)
end

#tween_swing_from_to(t) ⇒ Object



165
166
167
168
169
# File 'lib/cyberarm_engine/animator.rb', line 165

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

#tween_swing_to(t) ⇒ Object



176
177
178
179
# File 'lib/cyberarm_engine/animator.rb', line 176

def tween_swing_to(t)
  s = 1.70158
  (t -= 1) * t * ((s + 1) * t + s) + 1
end

#updateObject



12
13
14
# File 'lib/cyberarm_engine/animator.rb', line 12

def update
  @block.call(self, @from, @to) if @block
end