Class: DXRuby::Tiled::Map

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, data_dir) ⇒ Map

Returns a new instance of Map.



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
68
69
# File 'lib/dxruby_tiled/map.rb', line 11

def initialize(data, data_dir)
  @data_dir = data_dir
  
  @version         = data[:version]
  @orientation     = case data[:orientation]
    when "isometric"
      IsometricLayer
    when "staggered"
      StaggeredLayer
    when "hexagonal"
      HexagonalLayer
    else
      OrthogonalLayer
    end
  @width             = data[:width]         || 100
  @height            = data[:height]        || 100
  @tile_width        = data[:tilewidth]     || 32
  @tile_height       = data[:tileheight]    || 32
  @hex_side_length   = data[:hexsidelength] || 0
  @stagger_axis_y    = data[:staggeraxis]  != "x"
  @stagger_index_odd = data[:staggerindex] != "even"
  @next_object_id    = data[:nextobjectid]  || 1
  @properties        = data[:properties]    || {}
  @x_loop            = !!@properties[:x_loop]
  @y_loop            = !!@properties[:y_loop]
  @renderorder_x     = false
  @renderorder_y     = false
  case data[:renderorder]
  when "left-down"
    @renderorder_x = true
  when "right-up"
    @renderorder_y = true
  when "left-up"
    @renderorder_x = true
    @renderorder_y = true
  end
  @background_color = nil
  if data[:backgroundcolor]
    @background_color = data[:backgroundcolor].sub("#", "").scan(/../).map do |color|
      color.to_i(16)
    end
  end
  
  @layers = data[:layers].map do |layer|
    case layer[:type]
    when "tilelayer"
      @orientation.new(layer, self)
    when "objectgroup"
      ObjectGroup.new(layer, self)
    when "imagelayer"
      ImageLayer.new(layer, self)
    end
  end
  def @layers.name(name)
    return self.find{|layer| layer.name == name}
  end
  
  @tilesets = Tilesets.new(data[:tilesets], self)
end

Instance Attribute Details

#background_colorObject

Returns the value of attribute background_color.



9
10
11
# File 'lib/dxruby_tiled/map.rb', line 9

def background_color
  @background_color
end

#data_dirObject (readonly)

Returns the value of attribute data_dir.



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

def data_dir
  @data_dir
end

#heightObject (readonly)

Returns the value of attribute height.



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

def height
  @height
end

#hex_side_lengthObject (readonly)

Returns the value of attribute hex_side_length.



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

def hex_side_length
  @hex_side_length
end

#layersObject (readonly)

Returns the value of attribute layers.



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

def layers
  @layers
end

#next_object_idObject (readonly)

Returns the value of attribute next_object_id.



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

def next_object_id
  @next_object_id
end

#orientationObject (readonly)

Returns the value of attribute orientation.



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

def orientation
  @orientation
end

#propertiesObject (readonly)

Returns the value of attribute properties.



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

def properties
  @properties
end

#renderorder_xObject (readonly)

Returns the value of attribute renderorder_x.



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

def renderorder_x
  @renderorder_x
end

#renderorder_yObject (readonly)

Returns the value of attribute renderorder_y.



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

def renderorder_y
  @renderorder_y
end

#stagger_axis_yObject (readonly)

Returns the value of attribute stagger_axis_y.



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

def stagger_axis_y
  @stagger_axis_y
end

#stagger_index_oddObject (readonly)

Returns the value of attribute stagger_index_odd.



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

def stagger_index_odd
  @stagger_index_odd
end

#tile_heightObject (readonly)

Returns the value of attribute tile_height.



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

def tile_height
  @tile_height
end

#tile_widthObject (readonly)

Returns the value of attribute tile_width.



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

def tile_width
  @tile_width
end

#tilesetsObject (readonly)

Returns the value of attribute tilesets.



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

def tilesets
  @tilesets
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

#widthObject (readonly)

Returns the value of attribute width.



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

def width
  @width
end

#x_loopObject (readonly)

Returns the value of attribute x_loop.



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

def x_loop
  @x_loop
end

#y_loopObject (readonly)

Returns the value of attribute y_loop.



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

def y_loop
  @y_loop
end

Class Method Details

.name(name) ⇒ Object



64
65
66
# File 'lib/dxruby_tiled/map.rb', line 64

def @layers.name(name)
  return self.find{|layer| layer.name == name}
end

Instance Method Details

#draw(x, y, target = DXRuby::Window) ⇒ Object



71
72
73
74
75
# File 'lib/dxruby_tiled/map.rb', line 71

def draw(x, y, target = DXRuby::Window)
  target.draw_box_fill(0, 0, target.width, target.height, @background_color) if @background_color
  tilesets.animation()
  @layers.each{|layer| layer.draw(x, y, target) if layer.visible }
end

#load_image(filename) ⇒ Object



77
78
79
# File 'lib/dxruby_tiled/map.rb', line 77

def load_image(filename)
  return DXRuby::Image.load(File.join(@data_dir, filename))
end

#pixel_heightObject



85
86
87
# File 'lib/dxruby_tiled/map.rb', line 85

def pixel_height()
  return @orientation.pixel_height(self)
end

#pixel_widthObject



81
82
83
# File 'lib/dxruby_tiled/map.rb', line 81

def pixel_width()
  return @orientation.pixel_width(self)
end