Class: RichEngine::Animation
- Inherits:
-
Object
- Object
- RichEngine::Animation
- Defined in:
- lib/rich_engine/animation.rb
Overview
Plays a sequence of string frames (sprites) at a fixed frames-per-second.
Instance Attribute Summary collapse
-
#fg ⇒ Symbol, ...
Default foreground color used when drawing.
- #fps ⇒ Array<String>, ...
- #frame_index ⇒ Array<String>, ... readonly
- #frames ⇒ Array<String>, ... readonly
Instance Method Summary collapse
-
#current_frame ⇒ String
The frame string currently shown.
-
#draw(canvas, x:, y:, fg: nil) ⇒ void
Renders the current frame to the canvas.
-
#initialize(frames:, fps: 12, loop: true, fg: :white) ⇒ Animation
constructor
Builds an animation from a sequence of string frames.
-
#loop? ⇒ Boolean
Whether the animation wraps after the last frame.
-
#pause! ⇒ void
Pauses playback on the current frame.
-
#play! ⇒ void
Resumes playback.
-
#playing? ⇒ Boolean
Whether the animation is currently advancing frames.
-
#reset! ⇒ void
Rewinds to the first frame and restarts the frame timer.
-
#stop! ⇒ void
Stops playback and rewinds to the first frame.
-
#update(elapsed_time) ⇒ void
Advances the internal timer; call once per frame with the elapsed time.
Constructor Details
#initialize(frames:, fps: 12, loop: true, fg: :white) ⇒ Animation
Builds an animation from a sequence of string frames.
37 38 39 40 41 42 43 44 45 |
# File 'lib/rich_engine/animation.rb', line 37 def initialize(frames:, fps: 12, loop: true, fg: :white) @frames = frames @fps = fps @loop = loop @fg = fg @frame_index = 0 @playing = true @stepper = RichEngine::Timer.every(seconds: frame_interval) end |
Instance Attribute Details
#fg ⇒ Symbol, ...
Returns default foreground color used when drawing.
24 25 26 |
# File 'lib/rich_engine/animation.rb', line 24 def fg @fg end |
#fps ⇒ Array<String>, ...
20 21 22 |
# File 'lib/rich_engine/animation.rb', line 20 def fps @fps end |
#frame_index ⇒ Array<String>, ... (readonly)
20 21 22 |
# File 'lib/rich_engine/animation.rb', line 20 def frame_index @frame_index end |
#frames ⇒ Array<String>, ... (readonly)
20 21 22 |
# File 'lib/rich_engine/animation.rb', line 20 def frames @frames end |
Instance Method Details
#current_frame ⇒ String
Returns the frame string currently shown.
93 94 95 |
# File 'lib/rich_engine/animation.rb', line 93 def current_frame @frames[@frame_index] end |
#draw(canvas, x:, y:, fg: nil) ⇒ void
This method returns an undefined value.
Renders the current frame to the canvas.
117 118 119 |
# File 'lib/rich_engine/animation.rb', line 117 def draw(canvas, x:, y:, fg: nil) canvas.draw_sprite(current_frame, x: x, y: y, fg: fg || @fg) end |
#loop? ⇒ Boolean
Returns whether the animation wraps after the last frame.
57 |
# File 'lib/rich_engine/animation.rb', line 57 def loop? = @loop |
#pause! ⇒ void
This method returns an undefined value.
Pauses playback on the current frame.
73 74 75 |
# File 'lib/rich_engine/animation.rb', line 73 def pause! @playing = false end |
#play! ⇒ void
This method returns an undefined value.
Resumes playback.
80 81 82 |
# File 'lib/rich_engine/animation.rb', line 80 def play! @playing = true end |
#playing? ⇒ Boolean
Returns whether the animation is currently advancing frames.
60 |
# File 'lib/rich_engine/animation.rb', line 60 def = @playing |
#reset! ⇒ void
This method returns an undefined value.
Rewinds to the first frame and restarts the frame timer.
87 88 89 90 |
# File 'lib/rich_engine/animation.rb', line 87 def reset! @frame_index = 0 @stepper.interval = frame_interval end |
#stop! ⇒ void
This method returns an undefined value.
Stops playback and rewinds to the first frame.
65 66 67 68 |
# File 'lib/rich_engine/animation.rb', line 65 def stop! @playing = false reset! end |
#update(elapsed_time) ⇒ void
This method returns an undefined value.
Advances the internal timer; call once per frame with the elapsed time.
101 102 103 104 105 106 107 |
# File 'lib/rich_engine/animation.rb', line 101 def update(elapsed_time) return unless @playing return if @frames.size <= 1 @stepper.update(elapsed_time) @stepper.when_ready { advance_frame } end |