Class: CyberarmEngine::Animator

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

Constant Summary collapse

DEFAULT_TWEEN =
:linear

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Animator.



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

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

Instance Method Details

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



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

def color_hsv_transition(from, to, tween = DEFAULT_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, to, _tween = DEFAULT_TWEEN) ⇒ Object



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

def color_transition(from, to, _tween = DEFAULT_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
  (@start_time.to_f + (Gosu.milliseconds - @start_time)) / (@start_time + @duration.to_f)
end

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



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

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

#tween_linear(t) ⇒ Object

NOTE: Use this for future reference? github.com/danro/easing-js/blob/master/easing.js



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

def tween_linear(t)
  t
end

#tween_sine(t) ⇒ Object



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

def tween_sine(t)
  Math.sin(t) * t
end

#updateObject



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

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