Class: Ray::Animation::SpriteAnimation

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

Overview

Sprite animations are used to animate a position change in a sprite sheet.

It is written so that each step of the animation (including the latest one) has the same duration.

Instance Attribute Summary collapse

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 Attribute Details

#current_valueObject (readonly)

Returns the value of attribute current_value.



61
62
63
# File 'lib/ray/animation/sprite_animation.rb', line 61

def current_value
  @current_value
end

#initial_valueObject (readonly)

Returns the value of attribute initial_value.



63
64
65
# File 'lib/ray/animation/sprite_animation.rb', line 63

def initial_value
  @initial_value
end

#negative_xObject (readonly)

Returns the value of attribute negative_x.



65
66
67
# File 'lib/ray/animation/sprite_animation.rb', line 65

def negative_x
  @negative_x
end

#negative_yObject (readonly)

Returns the value of attribute negative_y.



65
66
67
# File 'lib/ray/animation/sprite_animation.rb', line 65

def negative_y
  @negative_y
end

#x_stepsObject (readonly)

Returns the value of attribute x_steps.



64
65
66
# File 'lib/ray/animation/sprite_animation.rb', line 64

def x_steps
  @x_steps
end

#y_stepsObject (readonly)

Returns the value of attribute y_steps.



64
65
66
# File 'lib/ray/animation/sprite_animation.rb', line 64

def y_steps
  @y_steps
end

Instance Method Details

#-@Object



52
53
54
55
56
57
58
59
# File 'lib/ray/animation/sprite_animation.rb', line 52

def -@
  last_value = @initial_value + variation(@x_steps - 1, @y_steps - 1)

  sprite_animation(:from     => @initial_value,
                   :to       => last_value,
                   :loop     => @loop,
                   :duration => @duration)
end

#end_animationObject



44
45
46
# File 'lib/ray/animation/sprite_animation.rb', line 44

def end_animation
  target.sheet_pos = @initial_value if @loop
end

#setup(opts) ⇒ Object

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :from (Ray::Vector2)

    First sprite position

  • :to (Ray::Vector2)

    Last sprite position

  • :loop (true, false)

    Whether to reset to the first step at the end of the animation.

  • :duration (Float)

    Duration of the animation, in seconds.



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

def setup(opts)
  self.duration = opts[:duration]

  @initial_value = opts[:from].to_vector2.dup

  delta = opts[:to].to_vector2 - opts[:from].to_vector2

  @x_steps = delta.x.to_i.abs + 1
  @y_steps = delta.y.to_i.abs + 1

  @negative_x = delta.x < 0
  @negative_y = delta.y < 0

  @loop = opts[:loop]
end

#update_targetObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ray/animation/sprite_animation.rb', line 32

def update_target
  progress = progression

  x_prog = (progress * @x_steps).floor
  y_prog = (progress * @y_steps).floor

  x_prog = @x_steps - 1 if x_prog == @x_steps
  y_prog = @y_steps - 1 if y_prog == @y_steps

  target.sheet_pos = @initial_value + variation(x_prog, y_prog)
end

#variation(x, y) ⇒ Object



48
49
50
# File 'lib/ray/animation/sprite_animation.rb', line 48

def variation(x, y)
  Ray::Vector2[@negative_x ? -x : x, @negative_y ? -y : y]
end