Class: AniRuby::Animation
- Inherits:
-
Object
- Object
- AniRuby::Animation
- 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
-
#cursor ⇒ Integer
The current frame index of the animation.
-
#frames ⇒ AniRuby::Frames
The collection of frames this animation uses.
-
#loop ⇒ Boolean
The loop parameter.
Drawing collapse
-
#draw(x, y, z = 0, scale_x = 1, scale_y = 1, color = Gosu::Color::WHITE, mode = :default) ⇒ Object
Draw the animation.
-
#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).
-
#update ⇒ Object
Update the animation, advancing the frame counter.
Utility collapse
-
#current_frame ⇒ AniRuby::Frame
Get the current frame.
-
#done? ⇒ Boolean
Is the animation finished?.
-
#duration(ms) ⇒ Object
Set the duration for all frames in the animation.
-
#pause ⇒ Object
Pause the animation.
-
#paused? ⇒ Boolean
Is the animation paused?.
-
#reset ⇒ Object
(also: #reset!)
Set the animation to the beginning frame.
-
#resume ⇒ Object
Resume the animation.
Instance Method Summary collapse
-
#frame_expired? ⇒ Boolean
Has the current frame’s duration expired?.
-
#height ⇒ Integer
(also: #h)
Get the height of the current frame’s image.
-
#initialize(spritesheet, frame_w, frame_h, *durations, retro: false, loop: true) ⇒ Animation
constructor
Create a new animation.
-
#width ⇒ Integer
(also: #w)
Get the width of the current frame’s image.
Constructor Details
#initialize(spritesheet, frame_w, frame_h, *durations, retro: false, loop: true) ⇒ Animation
Create a new animation
26 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 53 54 55 56 57 58 59 |
# File 'lib/aniruby/animation.rb', line 26 def initialize(spritesheet, frame_w, frame_h, *durations, retro: false, loop: true) @frame_w = frame_w @frame_h = frame_h @loop = loop @pause = false @cursor = 0 @step = 1 @frames = AniRuby::Frames.new(Gosu::Image.load_tiles(spritesheet, @frame_w, @frame_h, retro: retro)) # Default to 0.1 if the duration is negative durations.map! { |dur| dur.negative? ? 0.1 : dur } # 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
#cursor ⇒ Integer
Returns The current frame index of the animation.
8 9 10 |
# File 'lib/aniruby/animation.rb', line 8 def cursor @cursor end |
#frames ⇒ AniRuby::Frames
Returns The collection of frames this animation uses.
6 7 8 |
# File 'lib/aniruby/animation.rb', line 6 def frames @frames end |
#loop ⇒ Boolean
Returns The loop parameter.
10 11 12 |
# File 'lib/aniruby/animation.rb', line 10 def loop @loop end |
Instance Method Details
#current_frame ⇒ AniRuby::Frame
Get the current frame
203 204 205 |
# File 'lib/aniruby/animation.rb', line 203 def current_frame @frames[@cursor % @frames.count] end |
#done? ⇒ Boolean
This method will return true in intervals if the animation loops
Is the animation finished?
185 186 187 188 189 |
# File 'lib/aniruby/animation.rb', line 185 def done? return true if @cursor == @frames.count - 1 false end |
#draw(x, y, z = 0, scale_x = 1, scale_y = 1, color = Gosu::Color::WHITE, mode = :default) ⇒ Object
Draw the animation
(see also #draw_rot)
104 105 106 107 108 109 110 111 112 |
# File 'lib/aniruby/animation.rb', line 104 def draw(x, y, z = 0, scale_x = 1, scale_y = 1, color = Gosu::Color::WHITE, mode = :default) frame = @frames[@cursor] frame.sprite.draw(x, y, z, scale_x, scale_y, color, mode) 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 also #draw)
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/aniruby/animation.rb', line 128 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[@cursor] frame.sprite.draw_rot(x, y, z, angle, center_x, center_y, scale_x, scale_y, color, mode) end |
#duration(ms) ⇒ Object
Set the duration for all frames in the animation
175 176 177 178 179 |
# File 'lib/aniruby/animation.rb', line 175 def duration(ms) @frames.each { |frame| frame.duration = ms } self end |
#frame_expired? ⇒ Boolean
Has the current frame’s duration expired?
210 211 212 213 214 215 216 217 |
# File 'lib/aniruby/animation.rb', line 210 def frame_expired? now = Gosu.milliseconds / 1000.0 @last_frame ||= now if (now - @last_frame) > @frames[@cursor].duration @last_frame = now end end |
#height ⇒ Integer Also known as: h
Get the height of the current frame’s image
73 74 75 |
# File 'lib/aniruby/animation.rb', line 73 def height @frames[@cursor].height end |
#pause ⇒ Object
Pause the animation
(see also #resume)
148 149 150 151 152 |
# File 'lib/aniruby/animation.rb', line 148 def pause @pause = true self end |
#paused? ⇒ Boolean
Is the animation paused?
194 195 196 197 198 |
# File 'lib/aniruby/animation.rb', line 194 def paused? return true if @pause false end |
#reset ⇒ Object Also known as: reset!
Set the animation to the beginning frame
164 165 166 167 168 |
# File 'lib/aniruby/animation.rb', line 164 def reset @cursor = 0 self end |
#resume ⇒ Object
Resume the animation
(see also #pause)
157 158 159 160 161 |
# File 'lib/aniruby/animation.rb', line 157 def resume @pause = false self end |
#update ⇒ Object
Update the animation, advancing the frame counter. Note that this won’t do do anything if the animation is paused or has finished
83 84 85 86 87 88 89 90 91 |
# File 'lib/aniruby/animation.rb', line 83 def update return unless frame_expired? && !paused? if !done? @cursor += @step elsif done? && @loop @cursor = 0 end end |
#width ⇒ Integer Also known as: w
Get the width of the current frame’s image
64 65 66 |
# File 'lib/aniruby/animation.rb', line 64 def width @frames[@cursor].width end |