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
# File 'lib/cyberarm_engine/animator.rb', line 4

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

Instance Method Details

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



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

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



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

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)


18
19
20
# File 'lib/cyberarm_engine/animator.rb', line 18

def complete?
  progress >= 1.0
end

#progressObject



14
15
16
# File 'lib/cyberarm_engine/animator.rb', line 14

def progress
  (@start_time.to_f + (Gosu.milliseconds - @start_time)) / (@start_time + @duration.to_f)
end

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



22
23
24
# File 'lib/cyberarm_engine/animator.rb', line 22

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



46
47
48
# File 'lib/cyberarm_engine/animator.rb', line 46

def tween_linear(t)
  t
end

#tween_sine(t) ⇒ Object



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

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

#updateObject



10
11
12
# File 'lib/cyberarm_engine/animator.rb', line 10

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