Class: Ray::Animation

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/ray/animation.rb,
lib/ray/animation/sequence.rb,
lib/ray/animation/combination.rb,
lib/ray/animation/block_animation.rb,
lib/ray/animation/circular_motion.rb,
lib/ray/animation/color_variation.rb,
lib/ray/animation/float_variation.rb,
lib/ray/animation/sprite_animation.rb,
lib/ray/animation/vector_variation.rb

Overview

Animations are changes progressively applied to an object. This class should be subclassed by the actual animations.

Creating an animation object

Subclasses create a helper method in Ray::Helper to create the animation:

translation(:from => [0, 0], :to => [120, 120], :duration => 30)
rotation(:from => 10, :to => 360, :duration => 2)

Then, the animation isn’t running yet. It is needed to call #start with the target of the animation:

@translation.start some_sprite

You’d then need to call #update regularily to apply the changes. For instance, in a scene, you’d do:

always { @translation.update }

(See also AnimationList)

Animations can be paused (#pause) and resumed (#resume) if needed.

They also raise an :animation_end event you could use to call another event:

on :animation_end, @translation do
  @rotation.start some_sprite
end

Creating a custom animation class

class MyOwnAnimation < Ray::Animation
  # Creates the helper method
  register_for :my_own_animation

  # hash is the argument passed to the helper method.
  # target is still nil.
  def setup(hash)
    @some_param = hash[:param]
    self.duration = 10 # You *need* to set this
  end

  # the animation has just started, target is set.
  def setup_target
  end

  # update has been called
  def update_target
    # progression is a float between 0 and 1.
    target.x += @some_param * progression
  end

  # the animation has just ended.
  def end_animation
  end

  # Not required. You can get an animation cancelling the first one
  # using this in most animation classes.
  def -@
    my_own_animation(:param => -@some_param)
  end
end

Defined Under Namespace

Classes: BlockAnimation, CircularMotion, ColorVariation, Combination, FloatVariation, Sequence, SpriteAnimation, VectorVariation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

#create_event_runner, #disable_event_group, effect_generator, #enable_event_group, #event_runner, #event_runner=, font, holding?, image, image_target, mouse_pos, music, #remove_event_group, #rotation, #scale_variation, sound, sound_buffer, sprite, text, #translation

Methods included from Matchers

key, key_mod, where

Methods included from DSL::EventListener

#add_hook, #current_event_group, #current_event_group=, #event_group, #listener_runner, #listener_runner=, #on

Methods included from DSL::EventRaiser

#raise_event, #raiser_runner, #raiser_runner=

Constructor Details

#initializeAnimation

Returns a new instance of Animation.



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ray/animation.rb', line 75

def initialize
  @duration = 0

  @start_time = nil
  @pause_time = nil
  @end_time   = nil

  @running = false

  @target = nil
end

Instance Attribute Details

#durationFloat

Returns Duration of the animation, in seconds.

Returns:

  • (Float)

    Duration of the animation, in seconds.



234
235
236
# File 'lib/ray/animation.rb', line 234

def duration
  @duration
end

#end_timeTime (readonly)

Returns Time when the animation should end.

Returns:

  • (Time)

    Time when the animation should end.



237
238
239
# File 'lib/ray/animation.rb', line 237

def end_time
  @end_time
end

#targetObject (readonly)

Returns The target of the animation.

Returns:

  • (Object)

    The target of the animation.



231
232
233
# File 'lib/ray/animation.rb', line 231

def target
  @target
end

Class Method Details

.register_for(name) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ray/animation.rb', line 63

def self.register_for(name)
  klass = self

  Ray::Helper.send :define_method, name do |*args|
    animation = klass.new
    animation.event_runner = event_runner
    animation.setup(*args)

    animation
  end
end

Instance Method Details

#+(other) ⇒ Ray::Animation::Combination

Returns Combination of self and the argument.

Returns:



214
215
216
# File 'lib/ray/animation.rb', line 214

def +(other)
  animation_combination(self, other)
end

#-(other) ⇒ Ray::Animation::Combination

Returns Combination of self and the oppsite of the argument.

Returns:



220
221
222
# File 'lib/ray/animation.rb', line 220

def -(other)
  animation_combination(self, -other)
end

#bounce!Ray::Animation

Registers an event making the animation bounce: when animation is completed, -animation is run on the same target, and when -animation is done running, animation starts again.

Returns:

  • (Ray::Animation)

    The reversed animation, which you need to update as well.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/ray/animation.rb', line 166

def bounce!
  reverse = -self
  reverse.event_runner = event_runner

  on :animation_end, self do
    reverse.start @target
  end

  on :animation_end, reverse do
    start @target
  end

  reverse
end

#end_animationObject

Override this method to do something at the end of the animation.



187
188
# File 'lib/ray/animation.rb', line 187

def end_animation
end

#loop!Object

Registers an event making the animation repeat itself over and over



154
155
156
157
158
# File 'lib/ray/animation.rb', line 154

def loop!
  on :animation_end, self do
    start @target
  end
end

#pauseObject

Pauses the animation.



115
116
117
118
119
120
# File 'lib/ray/animation.rb', line 115

def pause
  @pause_time = Time.now
  @running    = false

  pause_animation
end

#pause_animationObject

Override this if you need to do something when the animation is paused (for instance, paussing animations used by this object).



192
193
# File 'lib/ray/animation.rb', line 192

def pause_animation
end

#paused?true, false

Returns True if the animation is paused.

Returns:

  • (true, false)

    True if the animation is paused



228
# File 'lib/ray/animation.rb', line 228

def paused?; not @pause_time.nil?; end

#progressionFloat?

Returns The progression of the animation, between 0 and 1. Nil if the animation isn’t running.

Returns:

  • (Float, nil)

    The progression of the animation, between 0 and 1. Nil if the animation isn’t running.



201
202
203
204
205
206
207
208
209
210
# File 'lib/ray/animation.rb', line 201

def progression
  if running?
    if @duration.zero?
      1.0
    else
      ret = (Time.now - @start_time) / duration
      ret > 1 ? 1.0 : ret
    end
  end
end

#resumeObject

Resumes from the pause. The time when the animation should end is also updated.



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ray/animation.rb', line 124

def resume
  return unless @pause_time

  # Reset @start_time to compute the progression more easily.
  @end_time   = Time.now + duration - (@pause_time - @start_time)
  @start_time = @end_time - duration
  @pause_time = nil

  @running = true

  resume_animation
end

#resume_animationObject

Override this if you need to do something when the animation is resumed



196
197
# File 'lib/ray/animation.rb', line 196

def resume_animation
end

#running?true, false

Returns True if the animation is running.

Returns:

  • (true, false)

    True if the animation is running



225
# File 'lib/ray/animation.rb', line 225

def running?; @running ; end

#setup(*args) ⇒ Object

Override this in subclasses. Will be called by the helper method (when target is still nil).

Raises:

  • (NotImplementedError)


105
106
107
# File 'lib/ray/animation.rb', line 105

def setup(*args)
  raise NotImplementedError
end

#setup_targetObject

Method called when the animation is started. Override it if needed.



111
112
# File 'lib/ray/animation.rb', line 111

def setup_target
end

#start(on) ⇒ Object

Starts the animation.

Parameters:

  • on (Object)

    The target of the animation

Returns:

  • self



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ray/animation.rb', line 90

def start(on)
  @target = on

  @start_time = Time.now
  @end_time   = @start_time + duration

  @running = true

  setup_target

  self
end

#updateObject

Updates the target if the animation is running. May also end the animation.



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ray/animation.rb', line 139

def update
  if running?
    update_target

    if Time.now >= @end_time
      @running  = false
      @end_time = nil

      end_animation
      raise_event :animation_end, self if raiser_runner
    end
  end
end

#update_targetObject

Override this method to apply changes to the target.

Raises:

  • (NotImplementedError)


182
183
184
# File 'lib/ray/animation.rb', line 182

def update_target
  raise NotImplementedError
end