Class: Ray::Animation::CircularMotion

Inherits:
Ray::Animation show all
Defined in:
lib/ray/animation/circular_motion.rb

Overview

The target of this animation will move on a circular orbit.

Registered as #circular_motion.

Instance Attribute Summary

Attributes inherited from Ray::Animation

#duration, #end_time, #target

Instance Method Summary collapse

Methods inherited from Ray::Animation

#+, #-, #bounce!, #initialize, #loop!, #pause, #pause_animation, #paused?, #progression, register_for, #resume, #resume_animation, #running?, #setup_target, #start, #update

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

This class inherits a constructor from Ray::Animation

Instance Method Details

#-@CircularMotion

Returns An animation cancelling self.

Returns:



49
50
51
52
53
54
# File 'lib/ray/animation/circular_motion.rb', line 49

def -@
  circular_motion(:from => (@to_angle / Math::PI) * 180,
                  :to => (@from_angle / Math::PI) * 180,
                  :duration => duration, :center => @center,
                  :radius => @radius)
end

#end_animationObject



41
42
43
44
45
46
# File 'lib/ray/animation/circular_motion.rb', line 41

def end_animation
  x = @center.x + (Math.cos(@to_angle) * @radius)
  y = @center.y - (Math.sin(@to_angle) * @radius)

  target.pos = [x, y]
end

#setup(opts) ⇒ Object

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :center (Ray::Vector2)

    Center of the orbit

  • :radius (Float)

    Radius of the circle followed by the target.

  • :angle (Float)

    Required unless :from and :to are set. Last value for the angle.

  • :from (Float)

    First value for the angle.

  • :to (Float)

    Last value for the angle.

  • :duration (Float)

    Duration in seconds.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ray/animation/circular_motion.rb', line 17

def setup(opts)
  @center = opts[:center].to_vector2
  @radius = opts[:radius]

  if opts[:angle]
    @from_angle = 0
    @to_angle   = (opts[:angle] / 180) * Math::PI
  else
    @from_angle = (opts[:from] / 180) * Math::PI
    @to_angle   = (opts[:to]   / 180) * Math::PI
  end

  self.duration = opts[:duration]
end

#update_targetObject



32
33
34
35
36
37
38
39
# File 'lib/ray/animation/circular_motion.rb', line 32

def update_target
  angle = @from_angle + progression * (@from_angle - @to_angle)

  x = @center.x + (Math.cos(angle) * @radius)
  y = @center.y - (Math.sin(angle) * @radius)

  target.pos = [x, y]
end