Class: DXRuby::Tiled::Tileset

Inherits:
Object
  • Object
show all
Defined in:
lib/dxruby_tiled/tileset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, map) ⇒ Tileset

Returns a new instance of Tileset.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dxruby_tiled/tileset.rb', line 8

def initialize(data, map)
  @data = data
  @map  = map
  
  @firstgid    = data[:firstgid]  || 1
  @name        = data[:name]
  @tile_width  = data[:tilewidth]  || map.tile_width
  @tile_height = data[:tileheight] || map.tile_height
  @spacing     = data[:spacing]    || 0
  @margin      = data[:margin]     || 0
  @tile_count  = data[:tilecount]
  @columns     = data[:columns]
  @tile_offset = data[:tileoffset] # unsupported

  @properties  = data[:properties] || {}
  
  image = @map.load_image(data[:image])
  if data[:transparentcolor]
    color = data[:transparentcolor].sub("#", "").scan(/../).map{|c| c.to_i(16) }
    image.set_color_key(color)
  end
  image_width  = data[:imagewidth]  || image.width
  image_height = data[:imageheight] || image.height
  
  @tile_images = []
  i = 0
  col = 1
  x = @margin
  y = @margin
  loop do
    if x + @tile_width > image_width || (@columns && col > @columns)
      x = @margin
      y += @tile_height + @spacing
      col = 1
    end
    break if y + @tile_height > image_height
    
    @tile_images[i] = image.slice(x, y, @tile_width, @tile_height)
    x += @tile_width + @spacing
    i += 1
    col += 1
    break if @tile_count && i >= @tile_count
  end
  @tile_count = i unless @tile_count
  image.dispose()
  
  @animations = {}
  (data[:tiles] || {}).each_pair do |key, value|
    next unless value.has_key?(:animation)
    anim_time  = []
    anim_image = []
    time = 0
    value[:animation].each do |anim|
      anim_time.push(time)
      anim_image.push(@tile_images[anim[:tileid]])
      time += anim[:duration]
    end
    anim_time.push(time)
    @animations[@firstgid + key.to_s.to_i] = { time: anim_time, image: anim_image }
  end
end

Instance Attribute Details

#animationsObject (readonly)

Returns the value of attribute animations.



4
5
6
# File 'lib/dxruby_tiled/tileset.rb', line 4

def animations
  @animations
end

#columnsObject (readonly)

Returns the value of attribute columns.



4
5
6
# File 'lib/dxruby_tiled/tileset.rb', line 4

def columns
  @columns
end

#firstgidObject (readonly)

Returns the value of attribute firstgid.



4
5
6
# File 'lib/dxruby_tiled/tileset.rb', line 4

def firstgid
  @firstgid
end

#marginObject (readonly)

Returns the value of attribute margin.



4
5
6
# File 'lib/dxruby_tiled/tileset.rb', line 4

def margin
  @margin
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/dxruby_tiled/tileset.rb', line 4

def name
  @name
end

#propertiesObject (readonly)

Returns the value of attribute properties.



4
5
6
# File 'lib/dxruby_tiled/tileset.rb', line 4

def properties
  @properties
end

#spacingObject (readonly)

Returns the value of attribute spacing.



4
5
6
# File 'lib/dxruby_tiled/tileset.rb', line 4

def spacing
  @spacing
end

#tile_countObject (readonly)

Returns the value of attribute tile_count.



4
5
6
# File 'lib/dxruby_tiled/tileset.rb', line 4

def tile_count
  @tile_count
end

#tile_heightObject (readonly)

Returns the value of attribute tile_height.



4
5
6
# File 'lib/dxruby_tiled/tileset.rb', line 4

def tile_height
  @tile_height
end

#tile_imagesObject (readonly)

Returns the value of attribute tile_images.



4
5
6
# File 'lib/dxruby_tiled/tileset.rb', line 4

def tile_images
  @tile_images
end

#tile_offsetObject (readonly)

Returns the value of attribute tile_offset.



4
5
6
# File 'lib/dxruby_tiled/tileset.rb', line 4

def tile_offset
  @tile_offset
end

#tile_widthObject (readonly)

Returns the value of attribute tile_width.



4
5
6
# File 'lib/dxruby_tiled/tileset.rb', line 4

def tile_width
  @tile_width
end