Class: Spritely::ImageSet

Inherits:
Object
  • Object
show all
Defined in:
lib/spritely/image_set.rb

Overview

Each image in the sprite maps to an instance of ImageSet that stores the image data, width, height, and outer positioning.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options) ⇒ ImageSet

Returns a new instance of ImageSet.



10
11
12
13
14
15
16
# File 'lib/spritely/image_set.rb', line 10

def initialize(path, options)
  @path = path
  @options = options
  @data = File.read(path)
  @width, @height = data[0x10..0x18].unpack('NN')
  @left = 0
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/spritely/image_set.rb', line 8

def data
  @data
end

#heightObject (readonly)

Returns the value of attribute height.



8
9
10
# File 'lib/spritely/image_set.rb', line 8

def height
  @height
end

#leftObject (readonly)

Returns the value of attribute left.



8
9
10
# File 'lib/spritely/image_set.rb', line 8

def left
  @left
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/spritely/image_set.rb', line 8

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/spritely/image_set.rb', line 8

def path
  @path
end

#topObject

Returns the value of attribute top.



7
8
9
# File 'lib/spritely/image_set.rb', line 7

def top
  @top
end

#widthObject (readonly)

Returns the value of attribute width.



8
9
10
# File 'lib/spritely/image_set.rb', line 8

def width
  @width
end

Instance Method Details

#imagesObject



22
23
24
# File 'lib/spritely/image_set.rb', line 22

def images
  @images ||= []
end

#nameObject



18
19
20
# File 'lib/spritely/image_set.rb', line 18

def name
  File.basename(path, ".png")
end

#outer_heightObject



26
27
28
# File 'lib/spritely/image_set.rb', line 26

def outer_height
  spacing_above + height + spacing_below
end

#position_in!(collection_width) ⇒ Object

When positioned in the sprite, we must take into account whether the image is configured to repeat, or is positioned to the right-hand side of the sprite map.



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/spritely/image_set.rb', line 49

def position_in!(collection_width)
  if repeated?
    left_position = 0
    while left_position < collection_width
      add_image!(left_position)
      left_position += width
    end
  elsif right?
    add_image!(@left = collection_width - width)
  else
    add_image!(0)
  end
end

#repeated?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/spritely/image_set.rb', line 38

def repeated?
  options[:repeat] == 'true'
end

#right?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/spritely/image_set.rb', line 42

def right?
  options[:position] == 'right'
end

#spacing_aboveObject



30
31
32
# File 'lib/spritely/image_set.rb', line 30

def spacing_above
  options[:spacing_above].to_i
end

#spacing_belowObject



34
35
36
# File 'lib/spritely/image_set.rb', line 34

def spacing_below
  (options[:spacing_below] || options[:spacing]).to_i
end