Class: Metro::ImplicitAnimation

Inherits:
OnUpdateOperation show all
Defined in:
lib/metro/animation/implicit_animation.rb

Overview

Note:

The actor object must respond to setter methods that match the specified attributes (e.g. x, y).

An Implicit Animation is an animation without all the work. This little animation will take care of figuring out moving an actor from one position to another, the rotation, the alpha, etc.

Here an animation is created that will move the player to the position (final_x,final_y), specified in the :to hash that is provided, over the interval of 80 steps. Additionally the movement is done with an easing in.

The context provided is the context that the ‘on_complete’ block is executed. In this case, upon completition, transition the scene from the current one to the main scene.

Examples:

Creating an explicit animation that moves a player


animation = ImplicitAnimation.new actor: player,
  to: { x: final_x, y: final_y },
  interval: 80,
  easing: :ease_in,
  context: scene

animation.on_complete do
  transition_to :main
end

Defined Under Namespace

Classes: AnimationStep

Instance Attribute Summary collapse

Attributes inherited from OnUpdateOperation

#complete_block, #current_step, #step_block

Instance Method Summary collapse

Methods inherited from OnUpdateOperation

#complete!, #completed?, #initialize, #next_step, #on_complete, #on_step, #step_interval, #update

Constructor Details

This class inherits a constructor from Metro::OnUpdateOperation

Instance Attribute Details

#easingObject (readonly)

Returns the type of easing that the implicit animation should employ. By default it uses linear but can be overridden when the easing is configured.

Returns:

  • the type of easing that the implicit animation should employ. By default it uses linear but can be overridden when the easing is configured.



46
47
48
# File 'lib/metro/animation/implicit_animation.rb', line 46

def easing
  @easing
end

Instance Method Details

#after_initializeObject

Additional initializion is required to calculate the attributes that are going to be animated and to determine each of their deltas.



52
53
54
55
56
57
# File 'lib/metro/animation/implicit_animation.rb', line 52

def after_initialize
  to.each do |attribute,final|
    start = actor.send(attribute)
    animations.push build_animation_step(attribute,start,final)
  end
end

#animationsObject



37
38
39
# File 'lib/metro/animation/implicit_animation.rb', line 37

def animations
  @animations ||= []
end

#build_animation_step(attribute, start, final) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/metro/animation/implicit_animation.rb', line 59

def build_animation_step(attribute,start,final)
  step = AnimationStep.new
  step.actor = actor
  step.attribute = attribute
  step.deltas = easing_for(easing).calculate(start.to_f,final.to_f,interval.to_f)
  step
end

#delta_for_step(attribute) ⇒ Object

Returns the delta for the attribute for the given step.

Returns:

  • the delta for the attribute for the given step



95
96
97
# File 'lib/metro/animation/implicit_animation.rb', line 95

def delta_for_step(attribute)
  deltas[attribute].at(current_step)
end

#easing_for(name) ⇒ Object

Returns the correct easing based on the specified name. When the name provided does not match anything then default to linear easing.

Returns:

  • the correct easing based on the specified name. When the name provided does not match anything then default to linear easing.



79
80
81
# File 'lib/metro/animation/implicit_animation.rb', line 79

def easing_for(name)
  Metro::Easing.easing_for(name)
end

#execute_stepObject

The ImplicitAnimation overrides the Animation#execute_step and updates the attributes of the actor based upon the value of the current animation step.



88
89
90
# File 'lib/metro/animation/implicit_animation.rb', line 88

def execute_step
  animations.each {|step| step.execute_step(current_step) }
end