Class: AniRuby::Animation

Inherits:
Object
  • Object
show all
Defined in:
lib/aniruby/animation.rb

Overview

Has a AniRuby::Frames colletion, with a simple counter to keep track of current frame plus looping and pausing functionality

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spritesheet, frame_w, frame_h, retro = false, loop = true, *durations) ⇒ Animation

Create a new animation

Parameters:

  • spritesheet (String)

    Path to the spritesheet file

  • frame_w (Integer)

    The width of each individual frame

  • frame_h (Integer)

    The height of each individual frame

  • retro (Boolean) (defaults to: false)

    If true, the animation will not be interpolated when scaled

  • loop (Boolean) (defaults to: true)

    If true, the animation will loop indefinitely

  • durations (Float)

    The duration of the frames in MS (0.5 is half a second, 1.0 a second, etc). If there’s more than one duration provided they will be mapped to each frame of the animation. The default for each frame is 0.1.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/aniruby/animation.rb', line 27

def initialize(spritesheet, frame_w, frame_h, retro = false, loop = true, *durations)
  @frame_w = frame_w
  @frame_h = frame_h

  @loop = loop

  @current_frame = 0
  @pause = false

  @frames = AniRuby::Frames.new(Gosu::Image.load_tiles(spritesheet,
                                                       @frame_w,
                                                       @frame_h,
                                                       retro: retro))

  # TODO: Maybe I could shorten this, adding an extra argument to
  #       AniRuby::Frames
  if durations.one?
    @frames.each { |frame| frame.duration = durations[0]}
  else
    @frames.each_with_index do |frame, idx|
      # Set each frame to the duration provided, if there's no provide
      # duration for all frames then we'll leave it at the default
      frame.duration = durations[idx] unless durations[idx].nil?
    end
  end
end

Instance Attribute Details

#current_frameInteger

Returns The current frame index of the animation.

Returns:

  • (Integer)

    The current frame index of the animation



9
10
11
# File 'lib/aniruby/animation.rb', line 9

def current_frame
  @current_frame
end

#framesAniRuby::Frames

Returns The collection of frames this animation uses.

Returns:



7
8
9
# File 'lib/aniruby/animation.rb', line 7

def frames
  @frames
end

#loopBoolean

Returns The loop status of the animation.

Returns:

  • (Boolean)

    The loop status of the animation



11
12
13
# File 'lib/aniruby/animation.rb', line 11

def loop
  @loop
end

Instance Method Details

#done?Boolean

Note:

This method will return true in intervals if the animation loops

Is the animation finished?

Returns:

  • (Boolean)


159
160
161
# File 'lib/aniruby/animation.rb', line 159

def done?
  true if @current_frame == @frames.count - 1
end

#draw(x, y, z = 0, scale_x = 1, scale_y = 1, color = Gosu::Color::WHITE, mode = :default) ⇒ Object

Draw the animation

(see #draw_rot)

Parameters:

  • x (Integer)

    The X coordinate

  • y (Integer)

    The Y coordinate

  • z (Integer) (defaults to: 0)

    The Z order

  • scale_x (Float) (defaults to: 1)

    The horizontal scale factor

  • scale_y (Float) (defaults to: 1)

    The vertical scale factor

  • color (Gosu::Color) (defaults to: Gosu::Color::WHITE)

    The color to usw when drawing

  • mode (:default, :additive) (defaults to: :default)

    The blending mode



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/aniruby/animation.rb', line 87

def draw(x, y, z = 0,
         scale_x = 1,
         scale_y = 1,
         color = Gosu::Color::WHITE,
         mode = :default)
  frame = @frames[@current_frame]

  frame.sprite.draw(x, y, z, scale_x, scale_y, color, mode)

  @current_frame = 0 if @loop && done?
end

#draw_rot(x, y, z = 0, angle = 0, center_x = 0.5, center_y = 0.5, scale_x = 1, scale_y = 1, color = Gosu::Color::WHITE, mode = :default) ⇒ Object

Draw the animation rotated, with its rotational center at (x, y).

(see #draw)

Parameters:

  • x (Integer)

    The X coordinate

  • y (Integer)

    The Y coordinate

  • z (Integer) (defaults to: 0)

    The Z order

  • angle (Float) (defaults to: 0)

    The angle. in degrees

  • center_x (Float) (defaults to: 0.5)

    the relative horizontal rotation origin

  • center_y (Float) (defaults to: 0.5)

    the relative vertical rotation origin

  • scale_x (Float) (defaults to: 1)

    The horizontal scale factor

  • scale_y (Float) (defaults to: 1)

    The vertical scale factor

  • color (Gosu::Color) (defaults to: Gosu::Color::WHITE)

    The color to usw when drawing

  • mode (:default, :additive) (defaults to: :default)

    The blending mode



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/aniruby/animation.rb', line 113

def draw_rot(x, y, z = 0,
             angle = 0,
             center_x = 0.5,
             center_y = 0.5,
             scale_x = 1,
             scale_y = 1,
             color = Gosu::Color::WHITE,
             mode = :default)
  frame = @frames[@current_frame]

  frame.sprite.draw_rot(x, y, z, angle, center_x, center_y, scale_x, scale_y, color, mode)

  # Loop the animation
  @current_frame = 0 if @loop && done?
end

#duration(ms) ⇒ Object

Set the duration for all frames in the animation

Parameters:

  • ms (Float)

    The new duration in milliseconds



151
152
153
# File 'lib/aniruby/animation.rb', line 151

def duration(ms)
  @frames.each { |frame| frame.duration = ms}
end

#frame_expired?Boolean

Has the current frame’s duration expired?

Returns:

  • (Boolean)


180
181
182
183
184
185
186
187
# File 'lib/aniruby/animation.rb', line 180

def frame_expired?
  now = Gosu.milliseconds / 1000.0
  @last_frame ||= now

  if (now - @last_frame) > @frames[@current_frame].duration
    @last_frame = now
  end
end

#get_current_frameAniRuby::Frame

Get the current frame

Returns:



175
176
177
# File 'lib/aniruby/animation.rb', line 175

def get_current_frame
  @frames[@current_frame % @frames.count]
end

#heightInteger

Get the height of the current frame’s image

Returns:

  • (Integer)


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

def height
  @frames[@current_frame].sprite.height
end

#pauseObject

Pause the animation

(see #resume)



132
133
134
# File 'lib/aniruby/animation.rb', line 132

def pause
  @pause = true
end

#paused?Boolean

Is the animation paused?

Returns:

  • (Boolean)


166
167
168
169
170
# File 'lib/aniruby/animation.rb', line 166

def paused?
  return true if @pause

  false
end

#resetObject

Set the animation to the beginning frame



144
145
146
# File 'lib/aniruby/animation.rb', line 144

def reset
  @current_frame = 0
end

#resumeObject

Resume the animation

(see #pause)



139
140
141
# File 'lib/aniruby/animation.rb', line 139

def resume
  @pause = false
end

#updateObject

Update the animation, advancing the frame counter. Note that this won’t do do anything if the animation is paused or has finished



70
71
72
73
74
# File 'lib/aniruby/animation.rb', line 70

def update
  return if done? || paused?

  @current_frame += 1 if frame_expired?
end

#widthInteger

Get the width of the current frame’s image

Returns:

  • (Integer)


57
58
59
# File 'lib/aniruby/animation.rb', line 57

def width
  @frames[@current_frame].sprite.width
end