Class: DXRuby::Tiled::Map

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

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

def initialize(data, data_dir)
  @data_dir = data_dir
  
  @version         = data[:version]
  @tiledversion    = data[:tiledversion]
  @orientation     =
    case data[:orientation]
    when "isometric" then IsometricLayer
    when "staggered" then StaggeredLayer
    when "hexagonal" then 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]    || {}
  @loop              = !!@properties[:loop]
  @renderorder_x     = !data[:renderorder].to_s.match("left")
  @renderorder_y     = !data[:renderorder].to_s.match("top")
  @background_color = nil
  if data[:backgroundcolor]
    @background_color = data[:backgroundcolor].sub("#", "").scan(/../).map{ |c| c.to_i(16) }
  end
  
  @tilesets = Tilesets.new(data[:tilesets], self)
  @layers = GroupLayer.new({ layers: data[:layers] }, 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

#loopObject (readonly)

Returns the value of attribute loop.



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

def loop
  @loop
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

#tiledversionObject (readonly)

Returns the value of attribute tiledversion.



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

def tiledversion
  @tiledversion
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

Instance Method Details

#delayed_disposeObject



69
70
71
# File 'lib/dxruby_tiled/map.rb', line 69

def delayed_dispose
  @tilesets.delayed_dispose
end

#disposeObject



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

def dispose
  @tilesets.dispose
end

#disposed?Boolean

Returns:

  • (Boolean)


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

def disposed?
  @tilesets.disposed?
end

#load_image(filename, transparentcolor = nil, data_dir = @data_dir) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/dxruby_tiled/map.rb', line 77

def load_image(filename, transparentcolor = nil, data_dir = @data_dir)
  image = DXRuby::Image.load(File.join(data_dir, filename))
  if transparentcolor
    color = transparentcolor.sub("#", "").scan(/../).map { |c| c.to_i(16) }
    image.set_color_key(color)
  end
  image
end

#load_template(filename, encoding = Encoding::UTF_8, data_dir = @data_dir) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/dxruby_tiled/map.rb', line 96

def load_template(filename, encoding = Encoding::UTF_8, data_dir = @data_dir)
  filepath = File.join(data_dir, filename)
  case File.extname(filename)
  when ".tx", ".xml"
    DXRuby::Tiled::TMXLoader.tx_to_hash(DXRuby::Tiled::TMXLoader.read_xmlfile(filepath, encoding))
  else
    DXRuby::Tiled.read_jsonfile(filepath, encoding)
  end
end

#load_tileset(filename, encoding = Encoding::UTF_8, data_dir = @data_dir) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/dxruby_tiled/map.rb', line 86

def load_tileset(filename, encoding = Encoding::UTF_8, data_dir = @data_dir)
  filepath = File.join(data_dir, filename)
  case File.extname(filename)
  when ".tsx", ".xml"
    DXRuby::Tiled::TMXLoader.tsx_to_hash(DXRuby::Tiled::TMXLoader.read_xmlfile(filepath, encoding))
  else
    DXRuby::Tiled.read_jsonfile(filepath, encoding)
  end
end

#next_object_idObject



60
61
62
63
# File 'lib/dxruby_tiled/map.rb', line 60

def next_object_id
  @next_object_id += 1
  return @next_object_id - 1
end

#pixel_heightObject



56
57
58
# File 'lib/dxruby_tiled/map.rb', line 56

def pixel_height
  @orientation.pixel_height(self)
end

#pixel_widthObject



52
53
54
# File 'lib/dxruby_tiled/map.rb', line 52

def pixel_width
  @orientation.pixel_width(self)
end

#render(x, y, target = DXRuby::Window, z = 0) ⇒ Object Also known as: draw

Raises:

  • (DXRuby::DXRubyError)


44
45
46
47
48
49
# File 'lib/dxruby_tiled/map.rb', line 44

def render(x, y, target = DXRuby::Window, z = 0)
  raise DXRuby::DXRubyError, "disposed object" if self.disposed?
  @tilesets.animate
  target.draw_box_fill(0, 0, target.width, target.height, @background_color, z) if @background_color
  @layers.render(x, y, target, z)
end