Class: Yuki::Animation::TimedAnimation

Inherits:
Object
  • Object
show all
Defined in:
scripts/01100 Yuki/00100 Animation/00100 Animation.rb

Overview

Class calculating time offset for animation.

This class also manage parallel & sub animation. Example :

(TimedAnimation.new(1) | TimedAnimation.new(2) > TimedAnimation.new(3)).root
# Is equivalent to
TimedAnimation.new(1).parallel_play(TimedAnimation.new(2)).play_before(TimedAnimation.new(3)).root
# Which is equivalent to : play 1 & 2 in parallel and then play 3
# Note that if 2 has sub animation, its sub animation has to finish in order to see animation 3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time_to_process, distortion = :UNICITY_DISTORTION, time_source = :GENERIC_TIME_SOURCE) ⇒ TimedAnimation

Create a new TimedAnimation

Parameters:

  • time_to_process (Float)

    number of seconds (with generic time) to process the animation

  • distortion (#call, Symbol) (defaults to: :UNICITY_DISTORTION)

    callable taking one paramater (between 0 & 1) and convert it to another number (between 0 & 1) in order to distord time

  • time_source (#call, Symbol) (defaults to: :GENERIC_TIME_SOURCE)

    callable taking no parameter and giving the current time



61
62
63
64
65
66
67
68
# File 'scripts/01100 Yuki/00100 Animation/00100 Animation.rb', line 61

def initialize(time_to_process, distortion = :UNICITY_DISTORTION, time_source = :GENERIC_TIME_SOURCE)
  @time_to_process = time_to_process.to_f
  @distortion_param = distortion
  @time_source_param = time_source
  @sub_animation = nil
  @parallel_animations = []
  @root = self # We make self as default root so the animations will always have a root
end

Instance Attribute Details

#begin_timeTime? (readonly)

Get the begin time of the animation (if started)

Returns:

  • (Time, nil)


49
50
51
# File 'scripts/01100 Yuki/00100 Animation/00100 Animation.rb', line 49

def begin_time
  @begin_time
end

#end_timeTime? (readonly)

Get the end time of the animation (if started)

Returns:

  • (Time, nil)


52
53
54
# File 'scripts/01100 Yuki/00100 Animation/00100 Animation.rb', line 52

def end_time
  @end_time
end

#parallel_animationsArray<TimedAnimation> (readonly)

Returns animation playing in parallel.

Returns:



41
42
43
# File 'scripts/01100 Yuki/00100 Animation/00100 Animation.rb', line 41

def parallel_animations
  @parallel_animations
end

#rootTimedAnimation

Returns the root animation (to retreive the right animation to play when building animation using operators).

Returns:

  • (TimedAnimation)

    the root animation (to retreive the right animation to play when building animation using operators)



46
47
48
# File 'scripts/01100 Yuki/00100 Animation/00100 Animation.rb', line 46

def root
  @root
end

#sub_animationTimedAnimation? (readonly)

Returns animation that plays after.

Returns:



43
44
45
# File 'scripts/01100 Yuki/00100 Animation/00100 Animation.rb', line 43

def sub_animation
  @sub_animation
end

#time_source#call? (readonly)

Get the time source of the animation (if started)

Returns:

  • (#call, nil)


55
56
57
# File 'scripts/01100 Yuki/00100 Animation/00100 Animation.rb', line 55

def time_source
  @time_source
end

Instance Method Details

#done?Boolean

Note:

should always be called after start

Indicate if the animation is done

Returns:

  • (Boolean)


91
92
93
94
# File 'scripts/01100 Yuki/00100 Animation/00100 Animation.rb', line 91

def done?
  private_done? && @parallel_animations.all?(&:done?) && (@sub_animation ? @sub_animation.done? : true) &&
    @played_until_end
end

#in_parallel_of(other) ⇒ TimedAnimation Also known as: >>

Add this animation in parallel of another animation

Parameters:

Returns:



133
134
135
136
# File 'scripts/01100 Yuki/00100 Animation/00100 Animation.rb', line 133

def in_parallel_of(other)
  other.parallel_add(self)
  return other
end

#parallel_add(other) ⇒ self Also known as: <<, |, parallel_play

Add a parallel animation

Parameters:

Returns:

  • (self)


121
122
123
124
# File 'scripts/01100 Yuki/00100 Animation/00100 Animation.rb', line 121

def parallel_add(other)
  @parallel_animations << other
  return self
end

#play_before(other) ⇒ TimedAnimation Also known as: >

Add a sub animation

Parameters:

Returns:



143
144
145
146
147
148
149
150
151
# File 'scripts/01100 Yuki/00100 Animation/00100 Animation.rb', line 143

def play_before(other)
  if @sub_animation
    @sub_animation.play_before(other)
  else
    @sub_animation = other
  end
  other.root = root
  return other
end

#resolver=(resolver)

Define the resolver (and transmit it to all the childs / parallel)

Parameters:

  • resolver (#call)

    callable that takes 1 parameter and return an object



157
158
159
160
161
# File 'scripts/01100 Yuki/00100 Animation/00100 Animation.rb', line 157

def resolver=(resolver)
  @resolver = resolver
  @sub_animation&.resolver = resolver
  @parallel_animations.each { |animation| animation.resolver = resolver }
end

#start(begin_offset = 0)

Start the animation (initialize it)

Parameters:

  • begin_offset (Float) (defaults to: 0)

    offset that prevents the animation from starting before now + begin_offset seconds



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'scripts/01100 Yuki/00100 Animation/00100 Animation.rb', line 72

def start(begin_offset = 0)
  # Resolve the distortion & time source
  @distortion = DISTORTIONS[@distortion_param] || resolve(@distortion_param)
  @time_source = TIME_SOURCES[@time_source_param] || resolve(@time_source_param)
  # @type [Time] time when the animation started
  @begin_time = @time_source.call + begin_offset
  # @type [Time] time when the animation ends
  @end_time = @begin_time + @time_to_process
  # Start all the parallel animation
  @parallel_animations.each { |animation| animation.start(begin_offset) }
  # Start the sub animation
  @sub_animation&.start(begin_offset + @time_to_process)
  # Boolean telling if the animation has been processed until the end (to prevent some display error)
  @played_until_end = false
end

#update

Note:

should always be called after start

Update the animation internal time and call update_internal with a parameter between 0 & 1 indicating the progression of the animation



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'scripts/01100 Yuki/00100 Animation/00100 Animation.rb', line 99

def update
  return unless private_began?
  return if done?

  @parallel_animations.each(&:update)
  # Update the sub animation if the current animation is actually done
  if private_done?
    unless @played_until_end
      update_internal(@distortion.call(1))
      @played_until_end = true
    end
    return unless @parallel_animations.all?(&:done?)

    return @sub_animation&.update
  end
  # Calculate the time progression value, apply it the distortion and send it to update_internal
  update_internal(@distortion.call((@time_source.call - @begin_time) / @time_to_process))
end