Class: DXRuby::Tiled::Layer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, map) ⇒ Layer

Returns a new instance of Layer.



7
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
# File 'lib/dxruby_tiled/layer.rb', line 7

def initialize(data, map)
  @map = map
  
  @name       = data[:name]
  @width      = data[:width ]     || map.width
  @height     = data[:height]     || map.height
  @opacity    = data[:opacity]    || 1.0
  @visible    = data[:visible] != false
  @offset_x   = data[:offsetx]    || 0
  @offset_y   = data[:offsety]    || 0
  @properties = data[:properties] || {}
  
  case data[:encoding]
  when "base64"
    tmp = Base64.decode64(data[:data])
    case data[:compression]
    when "gzip" # unsupported
    when "zlib"
      tmp = Zlib::Inflate.inflate(tmp).unpack("l*")
    else
      tmp = tmp.unpack("l*")
    end
  else
    tmp = data[:data]
  end
  @data = tmp.each_slice(@width).to_a
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#heightObject (readonly)

Returns the value of attribute height.



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

def height
  @height
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#offset_xObject

Returns the value of attribute offset_x.



5
6
7
# File 'lib/dxruby_tiled/layer.rb', line 5

def offset_x
  @offset_x
end

#offset_yObject

Returns the value of attribute offset_y.



5
6
7
# File 'lib/dxruby_tiled/layer.rb', line 5

def offset_y
  @offset_y
end

#opacityObject

Returns the value of attribute opacity.



5
6
7
# File 'lib/dxruby_tiled/layer.rb', line 5

def opacity
  @opacity
end

#propertiesObject (readonly)

Returns the value of attribute properties.



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

def properties
  @properties
end

#visibleObject

Returns the value of attribute visible.



5
6
7
# File 'lib/dxruby_tiled/layer.rb', line 5

def visible
  @visible
end

#widthObject (readonly)

Returns the value of attribute width.



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

def width
  @width
end

Instance Method Details

#[](x, y) ⇒ Object



35
36
37
38
39
# File 'lib/dxruby_tiled/layer.rb', line 35

def [](x, y)
  x = @map.x_loop ? x % @width  : x < 0 ? @width  : x
  y = @map.y_loop ? y % @height : y < 0 ? @height : y
  return @data.fetch(y, []).fetch(x, 0)
end

#[]=(x, y, value) ⇒ Object



41
42
43
44
45
# File 'lib/dxruby_tiled/layer.rb', line 41

def []=(x, y, value)
  x = @map.x_loop ? x % @width  : x < 0 ? @width  : x
  y = @map.y_loop ? y % @height : y < 0 ? @height : y
  @data[y][x] = value
end

#include?(x, y) ⇒ Boolean Also known as: member?

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/dxruby_tiled/layer.rb', line 47

def include?(x, y)
  return (@map.x_loop || (0...@width).include?(x)) &&
          (@map.y_loop || (0...@height).include?(y))
end