Class: Stagecraft::Animation::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/stagecraft/animation/action.rb

Constant Summary collapse

LOOPS =
%i[repeat once ping_pong].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clip, loop: :repeat, fade_in: 0.0) ⇒ Action

Returns a new instance of Action.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/stagecraft/animation/action.rb', line 11

def initialize(clip, loop: :repeat, fade_in: 0.0)
  @clip = clip
  @loop = loop.to_sym
  raise ArgumentError, "invalid animation loop #{loop.inspect}" unless LOOPS.include?(@loop)

  @time = 0.0
  @weight = fade_in.to_f.positive? ? 0.0 : 1.0
  @fade_duration = [Float(fade_in), 0.0].max
  @fade_elapsed = 0.0
  @time_scale = 1.0
  @enabled = true
  @clamp_when_finished = true
  @ping_pong_phase = 0.0
end

Instance Attribute Details

#clamp_when_finishedObject

Returns the value of attribute clamp_when_finished.



9
10
11
# File 'lib/stagecraft/animation/action.rb', line 9

def clamp_when_finished
  @clamp_when_finished
end

#clipObject (readonly)

Returns the value of attribute clip.



8
9
10
# File 'lib/stagecraft/animation/action.rb', line 8

def clip
  @clip
end

#enabledObject

Returns the value of attribute enabled.



9
10
11
# File 'lib/stagecraft/animation/action.rb', line 9

def enabled
  @enabled
end

#loopObject (readonly)

Returns the value of attribute loop.



8
9
10
# File 'lib/stagecraft/animation/action.rb', line 8

def loop
  @loop
end

#timeObject (readonly)

Returns the value of attribute time.



8
9
10
# File 'lib/stagecraft/animation/action.rb', line 8

def time
  @time
end

#time_scaleObject

Returns the value of attribute time_scale.



9
10
11
# File 'lib/stagecraft/animation/action.rb', line 9

def time_scale
  @time_scale
end

#weightObject

Returns the value of attribute weight.



9
10
11
# File 'lib/stagecraft/animation/action.rb', line 9

def weight
  @weight
end

Instance Method Details

#advance(dt) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/stagecraft/animation/action.rb', line 26

def advance(dt)
  return time unless enabled

  elapsed = Float(dt)
  update_fade(elapsed)
  duration = clip.duration
  return @time = 0.0 if duration.zero?

  delta = elapsed * time_scale
  return advance_ping_pong(delta, duration) if loop == :ping_pong

  @time += delta
  apply_loop(duration)
  time
end

#stopObject



42
43
44
45
# File 'lib/stagecraft/animation/action.rb', line 42

def stop
  @enabled = false
  self
end