Class: Potty::Sprite

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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)

Raises:

  • (ArgumentError)


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

#fpsObject (readonly)

Returns the value of attribute fps.



10
11
12
# File 'lib/potty/sprite.rb', line 10

def fps
  @fps
end

#framesObject (readonly)

Returns the value of attribute frames.



10
11
12
# File 'lib/potty/sprite.rb', line 10

def frames
  @frames
end

#modeObject (readonly)

Returns the value of attribute mode.



10
11
12
# File 'lib/potty/sprite.rb', line 10

def mode
  @mode
end

#nameObject (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_countObject



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

#heightObject

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

#widthObject

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