def from_dict(cls, dct, base_path=None): """Import from a dict compatible with Tiled's JSON plugin Use e.g. a JSON or YAML library to read such a dict from a file. :param dct: Dictionary with data :param base_path: Base path of the file, for loading linked resources """ if dct.pop('version', 1) != 1: raise ValueError('tmxlib only supports Tiled JSON version 1') self = cls( size=(dct.pop('width'), dct.pop('height')), tile_size=(dct.pop('tilewidth'), dct.pop('tileheight')), orientation=dct.pop('orientation', 'orthogonal'), ) if base_path: self.base_path = base_path background_color = dct.pop('backgroundcolor', None) if background_color: self.background_color = fileio.from_hexcolor(background_color) self.properties = dct.pop('properties') self.tilesets = [ tileset.Tileset.from_dict(d, base_path) for d in dct.pop('tilesets') ] self.layers = [ layer.Layer.from_dict(d, self) for d in dct.pop('layers') ] self.properties.update(dct.pop('properties', {})) return self
def from_dict(cls, dct, base_path=None): """Import from a dict compatible with Tiled's JSON plugin Use e.g. a JSON or YAML library to read such a dict from a file. :param dct: Dictionary with data :param base_path: Base path of the file, for loading linked resources """ if dct.pop('version', 1) != 1: raise ValueError('tmxlib only supports Tiled JSON version 1') self = cls( size=(dct.pop('width'), dct.pop('height')), tile_size=(dct.pop('tilewidth'), dct.pop('tileheight')), orientation=dct.pop('orientation', 'orthogonal'), ) if base_path: self.base_path = base_path background_color = dct.pop('backgroundcolor', None) if background_color: self.background_color = fileio.from_hexcolor(background_color) self.properties = dct.pop('properties') self.tilesets = [ tileset.Tileset.from_dict(d, base_path) for d in dct.pop('tilesets')] self.layers = [ layer.Layer.from_dict(d, self) for d in dct.pop('layers')] self.properties.update(dct.pop('properties', {})) return self
def from_dict(cls, dct, map): """Import from a dict compatible with Tiled's JSON plugin""" helpers.assert_item(dct, 'type', 'objectgroup') helpers.assert_item(dct, 'width', map.width) helpers.assert_item(dct, 'height', map.height) helpers.assert_item(dct, 'x', 0) helpers.assert_item(dct, 'y', 0) color = dct.pop('color', None) if color: color = fileio.from_hexcolor(color) self = cls( map=map, name=dct.pop('name'), visible=dct.pop('visible', True), opacity=dct.pop('opacity', 1), color=color, ) self.properties.update(dct.pop('properties', {})) for obj in dct.pop('objects', {}): self.append(mapobject.MapObject.from_dict(obj, self)) return self
def from_dict(cls, dct): """Import from a dict compatible with Tiled's JSON plugin""" dct.pop('firstgid', None) html_trans = dct.pop('transparentcolor', None) if html_trans: trans = fileio.from_hexcolor(html_trans) else: trans = None self = cls( name=dct.pop('name'), tile_size=(dct.pop('tilewidth'), dct.pop('tileheight')), image=image.open( dct.pop('image'), size=(dct.pop('imagewidth'), dct.pop('imageheight')), trans=trans, ), margin=dct.pop('margin', 0), spacing=dct.pop('spacing', 0), ) self.properties.update(dct.pop('properties', {})) for number, properties in dct.pop('tileproperties', {}).items(): self[int(number)].properties.update(properties) for number, attrs in dct.pop('tiles', {}).items(): attrs = dict(attrs) probability = attrs.pop('probability', None) if probability is not None: self[int(number)].probability = probability terrain_indices = attrs.pop('terrain', None) if terrain_indices is not None: self[int(number)].terrain_indices = terrain_indices assert not attrs for terrain in dct.pop('terrains', []): terrain = dict(terrain) self.terrains.append_new(terrain.pop('name'), self[int(terrain.pop('tile'))]) assert not terrain tileoffset = dct.pop('tileoffset', None) if tileoffset: self.tile_offset = tileoffset['x'], tileoffset['y'] return self
def from_dict(cls, dct): """Import from a dict compatible with Tiled's JSON plugin Use e.g. a JSON or YAML library to read such a dict from a file. """ if dct.pop('version', 1) != 1: raise ValueError('tmxlib only supports Tiled JSON version 1') self = cls( size=(dct.pop('width'), dct.pop('height')), tile_size=(dct.pop('tilewidth'), dct.pop('tileheight')), orientation=dct.pop('orientation', 'orthogonal'), ) background_color = dct.pop('backgroundcolor', None) if background_color: self.background_color = fileio.from_hexcolor(background_color) self.properties = dct.pop('properties') self.tilesets = [ tileset.ImageTileset.from_dict(d) for d in dct.pop('tilesets')] self.layers = [ layer.Layer.from_dict(d, self) for d in dct.pop('layers')] self.properties.update(dct.pop('properties', {})) return self
def from_dict(cls, dct, base_path=None): """Import from a dict compatible with Tiled's JSON plugin""" html_trans = dct.pop('transparentcolor', None) if html_trans: trans = fileio.from_hexcolor(html_trans) else: trans = None self = cls( name=dct.pop('name'), tile_size=(dct.pop('tilewidth'), dct.pop('tileheight')), image=image.open( dct.pop('image'), size=(dct.pop('imagewidth'), dct.pop('imageheight')), trans=trans, ), margin=dct.pop('margin', 0), spacing=dct.pop('spacing', 0), ) if base_path: self.image.base_path = base_path self._fill_from_dict(dct, base_path) return self
def from_dict(cls, dct): """Import from a dict compatible with Tiled's JSON plugin Use e.g. a JSON or YAML library to read such a dict from a file. """ if dct.pop('version', 1) != 1: raise ValueError('tmxlib only supports Tiled JSON version 1') self = cls( size=(dct.pop('width'), dct.pop('height')), tile_size=(dct.pop('tilewidth'), dct.pop('tileheight')), orientation=dct.pop('orientation', 'orthogonal'), ) background_color = dct.pop('backgroundcolor', None) if background_color: self.background_color = fileio.from_hexcolor(background_color) self.properties = dct.pop('properties') self.tilesets = [ tileset.ImageTileset.from_dict(d) for d in dct.pop('tilesets') ] self.layers = [ layer.Layer.from_dict(d, self) for d in dct.pop('layers') ] self.properties.update(dct.pop('properties', {})) return self