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
-
#frames ⇒ AniRuby::Frames
The collection of frames this animation uses.
-
#loop ⇒ Boolean
The loop parameter.
-
#position ⇒ Integer
The current frame index of the animation.
Instance Method Summary collapse
-
#current_frame ⇒ AniRuby::Frame
Get the current frame.
-
#done? ⇒ Boolean
Is the animation finished?.
-
#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).
-
#duration(ms) ⇒ Object
Set the duration for all frames in the animation.
-
#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, retro = false, loop = true, *durations) ⇒ Animation
constructor
Create a new animation.
-
#pause ⇒ Object
Pause the animation.
-
#paused? ⇒ Boolean
Is the animation paused?.
-
#reset ⇒ Object
Set the animation to the beginning frame.
-
#resume ⇒ Object
Resume the animation.
-
#update ⇒ Object
Update the animation, advancing the frame counter.
-
#width ⇒ Integer
(also: #w)
Get the width of the current frame’s image.
Constructor Details
#initialize(spritesheet, frame_w, frame_h, retro = false, loop = true, *durations) ⇒ 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 |
# File 'lib/aniruby/animation.rb', line 26 def initialize(spritesheet, frame_w, frame_h, retro = false, loop = true, *durations) @frame_w = frame_w @frame_h = frame_h @loop = loop @position = 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
#frames ⇒ AniRuby::Frames
Returns The collection of frames this animation uses.
7 8 9 |
# File 'lib/aniruby/animation.rb', line 7 def frames @frames end |
#loop ⇒ Boolean
Returns The loop parameter.
11 12 13 |
# File 'lib/aniruby/animation.rb', line 11 def loop @loop end |
#position ⇒ Integer
Returns The current frame index of the animation.
9 10 11 |
# File 'lib/aniruby/animation.rb', line 9 def position @position end |
Instance Method Details
#current_frame ⇒ AniRuby::Frame
Get the current frame
192 193 194 |
# File 'lib/aniruby/animation.rb', line 192 def current_frame @frames[@position % @frames.count] end |
#done? ⇒ Boolean
This method will return true in intervals if the animation loops
Is the animation finished?
174 175 176 177 178 |
# File 'lib/aniruby/animation.rb', line 174 def done? true if @position == @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 #draw_rot)
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/aniruby/animation.rb', line 94 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) @position = 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)
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/aniruby/animation.rb', line 120 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 @position = 0 if @loop && done? end |
#duration(ms) ⇒ Object
Set the duration for all frames in the animation
164 165 166 167 168 |
# File 'lib/aniruby/animation.rb', line 164 def duration(ms) @frames.each { |frame| frame.duration = ms } self end |
#frame_expired? ⇒ Boolean
Has the current frame’s duration expired?
197 198 199 200 201 202 203 204 |
# File 'lib/aniruby/animation.rb', line 197 def frame_expired? now = Gosu.milliseconds / 1000.0 @last_frame ||= now if (now - @last_frame) > @frames[@position].duration @last_frame = now end end |
#height ⇒ Integer Also known as: h
Get the height of the current frame’s image
69 70 71 |
# File 'lib/aniruby/animation.rb', line 69 def height @frames[@position].height end |
#pause ⇒ Object
Pause the animation
(see #resume)
139 140 141 142 143 |
# File 'lib/aniruby/animation.rb', line 139 def pause @pause = true self end |
#paused? ⇒ Boolean
Is the animation paused?
183 184 185 186 187 |
# File 'lib/aniruby/animation.rb', line 183 def paused? return true if @pause false end |
#reset ⇒ Object
Set the animation to the beginning frame
155 156 157 158 159 |
# File 'lib/aniruby/animation.rb', line 155 def reset @position = 0 self end |
#resume ⇒ Object
Resume the animation
(see #pause)
148 149 150 151 152 |
# File 'lib/aniruby/animation.rb', line 148 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
77 78 79 80 81 |
# File 'lib/aniruby/animation.rb', line 77 def update return if done? || paused? @position += 1 if frame_expired? end |
#width ⇒ Integer Also known as: w
Get the width of the current frame’s image
60 61 62 |
# File 'lib/aniruby/animation.rb', line 60 def width @frames[@position].width end |