Class: Potty::Sprite
- Inherits:
-
Object
- Object
- Potty::Sprite
- Defined in:
- lib/potty/sprite.rb
Overview
A named sequence of multiline string frames. Pure data ā holds no curses state and no timing state; an Animator drives playback.
Each frame is a multiline string. Lines may differ in length and a frame may contain trailing blank lines (preserved via split(ānā, -1)).
Instance Attribute Summary collapse
-
#fps ⇒ Object
readonly
Returns the value of attribute fps.
-
#frames ⇒ Object
readonly
Returns the value of attribute frames.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #frame(index) ⇒ Object
- #frame_count ⇒ Object
-
#frame_lines(index) ⇒ Object
Lines of a frame, preserving trailing blank lines.
-
#height ⇒ Object
Rows in the tallest frame.
-
#initialize(name, frames:, fps: 8, mode: :loop) ⇒ Sprite
constructor
name - identifier used to select this sprite on an Animator frames - array of multiline strings, one per animation frame fps - default playback rate in frames per second mode - :loop (wrap forever) or :once (stop on the last frame).
-
#width ⇒ Object
Columns in the widest line across all frames.
Constructor Details
#initialize(name, frames:, fps: 8, mode: :loop) ⇒ Sprite
name - identifier used to select this sprite on an Animator frames - array of multiline strings, one per animation frame fps - default playback rate in frames per second mode - :loop (wrap forever) or :once (stop on the last frame)
16 17 18 19 20 21 22 23 24 |
# File 'lib/potty/sprite.rb', line 16 def initialize(name, frames:, fps: 8, mode: :loop) raise ArgumentError, 'frames must not be empty' if frames.nil? || frames.empty? raise ArgumentError, "mode must be :loop or :once, got #{mode.inspect}" unless %i[loop once].include?(mode) @name = name.to_sym @frames = frames.dup.freeze @fps = fps @mode = mode end |
Instance Attribute Details
#fps ⇒ Object (readonly)
Returns the value of attribute fps.
10 11 12 |
# File 'lib/potty/sprite.rb', line 10 def fps @fps end |
#frames ⇒ Object (readonly)
Returns the value of attribute frames.
10 11 12 |
# File 'lib/potty/sprite.rb', line 10 def frames @frames end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
10 11 12 |
# File 'lib/potty/sprite.rb', line 10 def mode @mode end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/potty/sprite.rb', line 10 def name @name end |
Instance Method Details
#frame(index) ⇒ Object
30 31 32 |
# File 'lib/potty/sprite.rb', line 30 def frame(index) @frames[index] end |
#frame_count ⇒ Object
26 27 28 |
# File 'lib/potty/sprite.rb', line 26 def frame_count @frames.size end |
#frame_lines(index) ⇒ Object
Lines of a frame, preserving trailing blank lines.
35 36 37 |
# File 'lib/potty/sprite.rb', line 35 def frame_lines(index) (@frames[index] || '').split("\n", -1) end |
#height ⇒ Object
Rows in the tallest frame.
40 41 42 |
# File 'lib/potty/sprite.rb', line 40 def height @frames.map { |f| f.split("\n", -1).size }.max end |
#width ⇒ Object
Columns in the widest line across all frames.
45 46 47 |
# File 'lib/potty/sprite.rb', line 45 def width @frames.flat_map { |f| f.split("\n", -1).map(&:length) }.max end |