lib/demiurge/tmx.rb
def sprites_from_tmx(filename)
      cache_entry = { "format": "tmx" }

      # This recursively loads things like tileset .tsx files, so we
      # change to the root dir.
      Dir.chdir(@root_dir) do
        tmx_map = Tmx.load filename
        filename.sub!(/\.tmx\Z/, ".tmx.json")
        cache_entry["map"] = MultiJson.load(tmx_map.export_to_string(:filename => filename, :format => :json))
      end

      tiles = cache_entry["map"]

      # Add entries to the top-level cache entry, but not inside the "map" structure
      cache_entry["tmx_name"] = File.basename(filename).split(".")[0]
      cache_entry["name"] = tiles["name"] || cache_entry["tmx_name"]
      cache_entry["filename"] = filename
      cache_entry["dir"] = filename.split("/")[0..-2].join("/")
      cache_entry["animations"] = animations_from_tilesets tiles["tilesets"]
      cache_entry["objects"] = tiles["layers"].flat_map { |layer| layer["objects"] || [] }
      cache_entry["tile_layers"] = tiles["layers"].select { |layer| layer["type"] == "tilelayer" }

      # Copy most-used properties into top-level cache entry
      ["width", "height", "tilewidth", "tileheight"].each do |property|
        cache_entry[property] = tiles[property]
      end

      cache_entry
    end
# Load a TMX file and JSONify it. This includes its various
# tilesets, such as embedded tilesets and TSX files.  Do not
# assume this TMX file obeys any particular additional conventions
# beyond basic TMX format.
#
# The returned cache entry has some known fields and may have
# others - this is basically an internal format.
#
# @example ``` ruby
# {
#   "width" => 40,               # The width of the TMX map in tiles
#   "height" => 35,              # The height of the TMX map in tiles
#   "tilewidth" => 32,           # The width of a tile in pixels
#   "tileheight" => 32,          # The height of a tile in pixels
#   "tmx_name" => "main_board",  # The TMX filename, minus the extensions
#   "map" => { ... },  # The whole TMX file converted to TMX JSON format
#   "name" => "main_board",  # The name from the TMX file, falling back to the filename
#   "filename" => "../tmx/main_board.tmx", # The filename including path and extension(s)
#   "dir" => "../tmx"        # Just the directory part of the filename
#   "animations" => { ... }  # TMX tile animations extracted from tile properties in an internal format
#   "objects" => [ ... ]     # Extracted TMX objects
# }
# ```
#
# @param filename [String] The path to the file to parse as TMX
# @return [Hash] The TMX cache entry in the format given above
# @since 0.1.0
Load a TMX file and JSONify it. This includes its various
tilesets, such as embedded tilesets and TSX files.  Do not
assume this TMX file obeys any particular additional conventions
beyond basic TMX format.

The returned cache entry has some known fields and may have
others - this is basically an internal format.
Load a TMX file and JSONify it . This includes its various tilesets such as embedded tilesets and TSX files . Do not assume this TMX file obeys any particular additional conventions beyond basic TMX format . The returned cache entry has some known fields and may have others - this is basically an internal format .
def sprites_from_tmx(filename)
      cache_entry = { "format": "tmx" }
      Dir.chdir(@root_dir) do
        tmx_map = Tmx.load filename
        filename.sub!(/\.tmx\Z/, ".tmx.json")
        cache_entry["map"] = MultiJson.load(tmx_map.export_to_string(:filename => filename, :format => :json))
      end
      tiles = cache_entry["map"]
      cache_entry["tmx_name"] = File.basename(filename).split(".")[0]
      cache_entry["name"] = tiles["name"] || cache_entry["tmx_name"]
      cache_entry["filename"] = filename
      cache_entry["dir"] = filename.split("/")[0..-2].join("/")
      cache_entry["animations"] = animations_from_tilesets tiles["tilesets"]
      cache_entry["objects"] = tiles["layers"].flat_map { |layer| layer["objects"] || [] }
      cache_entry["tile_layers"] = tiles["layers"].select { |layer| layer["type"] == "tilelayer" }
      ["width", "height", "tilewidth", "tileheight"].each do |property|
        cache_entry[property] = tiles[property]
      end
      cache_entry
    end
def sprites_from_tmx ( filename ) cache_entry = { "format" : "tmx" } Dir . chdir ( @root_dir ) do tmx_map = Tmx . load filename filename . sub! ( / \. tmx \Z / , ".tmx.json" ) cache_entry [ "map" ] = MultiJson . load ( tmx_map . export_to_string ( :filename => filename , :format => :json ) ) end tiles = cache_entry [ "map" ] cache_entry [ "tmx_name" ] = File . basename ( filename ) . split ( "." ) [ 0 ] cache_entry [ "name" ] = tiles [ "name" ] || cache_entry [ "tmx_name" ] cache_entry [ "filename" ] = filename cache_entry [ "dir" ] = filename . split ( "/" ) [ 0 .. - 2 ] . join ( "/" ) cache_entry [ "animations" ] = animations_from_tilesets tiles [ "tilesets" ] cache_entry [ "objects" ] = tiles [ "layers" ] . flat_map { | layer | layer [ "objects" ] || [ ] } cache_entry [ "tile_layers" ] = tiles [ "layers" ] . select { | layer | layer [ "type" ] == "tilelayer" } [ "width" , "height" , "tilewidth" , "tileheight" ] . each do | property | cache_entry [ property ] = tiles [ property ] end cache_entry end
Load a TMX file and JSONify it.
Load a TMX file and JSONify it .
# This recursively loads things like tileset .tsx files, so we # change to the root dir. # Add entries to the top-level cache entry, but not inside the "map" structure # Copy most-used properties into top-level cache entry
The TMX cache entry in the format given above
The TMX cache entry in the format given above
Hash
filename
The path to the file to parse as TMX
The path to the file to parse as TMX
example
``` ruby { "width" => 40, # The width of the TMX map in tiles "height" => 35, # The height of the TMX map in tiles "tilewidth" => 32, # The width of a tile in pixels "tileheight" => 32, # The height of a tile in pixels "tmx_name" => "main_board", # The TMX filename, minus the extensions "map" => { ... }, # The whole TMX file converted to TMX JSON format "name" => "main_board", # The name from the TMX file, falling back to the filename "filename" => "../tmx/main_board.tmx", # The filename including path and extension(s) "dir" => "../tmx" # Just the directory part of the filename "animations" => { ... } # TMX tile animations extracted from tile properties in an internal format "objects" => [ ... ] # Extracted TMX objects } ```
` ` ` ruby { " width " = > 40 # The width of the TMX map in tiles " height " = > 35 # The height of the TMX map in tiles " tilewidth " = > 32 # The width of a tile in pixels " tileheight " = > 32 # The height of a tile in pixels " tmx_name " = > " main_board " # The TMX filename minus the extensions " map " = > { ... } # The whole TMX file converted to TMX JSON format " name " = > " main_board " # The name from the TMX file falling back to the filename " filename " = > " .. / tmx / main_board . tmx " # The filename including path and extension ( s ) " dir " = > " .. / tmx " # Just the directory part of the filename " animations " = > { ... } # TMX tile animations extracted from tile properties in an internal format " objects " = > [ ... ] # Extracted TMX objects } ` ` `
since
None